Interface2017年05月号新画像処理101を読む

2-4 さざなみのようなゆれエフェクト「正弦波+余弦波揺らぎ」

img_src = cv2.imread("data/Mandrill.png")
img_gray = cv2.cvtColor(img_src,cv2.COLOR_RGB2GRAY)

#周波数
f = 7.0
#振幅
A = 0.3
#画面の縦方向座標
#y
#eight
#Y
img_dst = np.zeros_like(img_gray)

Y = img_gray.shape[0]
X = img_gray.shape[1]
for y in range(Y):
    for x in range(X):
        p = (A * np.cos((f * 2.0 * np.pi * y)/Y) + 2 * A ) * img_gray[y,x]
        p = p + (A * np.sin((f * 2.0 * np.pi * x)/X) + 2 * A ) * img_gray[y,x]
        if p > 255:
            p = 255
        elif p < 0:
            p = 0
        img_dst[y,x] = p

#描画する
cv2.imshow("img_src",img_src)
cv2.imshow("img_dst",img_dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

#ヒストグラム
fig = plt.figure()
ax1 = fig.add_subplot(311) #総行数,総列数、サブプロット番号
ax2 = fig.add_subplot(312)
ax3 = fig.add_subplot(313)

color_list = ["blue","green","red"]
for i,j in enumerate(color_list):
    hist = cv2.calcHist([img_src],[i],None,[256],[0,256])
    ax1.plot(hist,color = j)
color_list = ["gray"]
for i,j in enumerate(color_list):
    hist = cv2.calcHist([img_dst],[i],None,[256],[0,256])
    ax2.plot(hist,color = j)

y=np.linspace(0,Y,num=Y - 1)
p1 = (A * np.cos((f * 2.0 * np.pi * y)/Y) + 2 * A)
p2 = (A * np.sin((f * 2.0 * np.pi * y)/X) + 2 * A )
ax3.plot(y,p1)    
ax3.plot(y,p2)    

f:id:bitop:20180115053703p:plain

f:id:bitop:20180128080259p:plain

f:id:bitop:20180128080333p:plain