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
#画面の横方向座標
#x
#width
#X
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.tan((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)

x=np.linspace(0,X,num=X - 1)
p = (A * np.tan((f * 2.0 * np.pi * x)/X) + 2 * A)
ax3.plot(x,p)    

f:id:bitop:20180115053703p:plain

f:id:bitop:20180126061701p:plain

f:id:bitop:20180126061746p:plain