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

技5 拡大/縮小

5-1 拡大

"""
cv2.INTER_NEAREST 最近傍補間
"""

img_src = cv2.imread("data/img_4-1.bmp")

img_dst = cv2.resize(img_src,None,fx=1.0, fy=1.3,interpolation=cv2.INTER_NEAREST)
#描画する
cv2.imshow("img_src",img_src)
cv2.imshow("img_dst",img_dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

立て方向に1.3倍

f:id:bitop:20180218070629p:plain

f:id:bitop:20180306061510p:plain

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

4-9 位置あわせするときのズレ補正「平行移動(垂直)」

img_src = cv2.imread("data/img_4-8.bmp")

Y = img_src.shape[0]
X = img_src.shape[1]
"""
mat = np.float32(
[[1,0,0], 
[0,1,10]])  ここ(10の数値)にプラスの数値をいれると下にズレる、マイナスをいれると上にズレる

"""
mat = np.float32(
[[1,0,0],
[0,1,-10]])


img_dst = cv2.warpAffine(img_src,mat,(X,Y))
#描画する
cv2.imshow("img_src",img_src)
cv2.imshow("img_dst",img_dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

f:id:bitop:20180305054859p:plain

f:id:bitop:20180305055156p:plain

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

4-8 位置あわせするときのズレ補正「平行移動(水平)」

img_src = cv2.imread("data/img_4-8.bmp")

Y = img_src.shape[0]
X = img_src.shape[1]
"""
mat = np.float32(
[[1,0,-10], ここにプラスの数値をいれると右にスレる、マイナスをいれると左にズレる
[0,1,0]])

"""
mat = np.float32(
[[1,0,-10],
[0,1,0]])


img_dst = cv2.warpAffine(img_src,mat,(X,Y))
#描画する
cv2.imshow("img_src",img_src)
cv2.imshow("img_dst",img_dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

f:id:bitop:20180305054859p:plain

f:id:bitop:20180305054932p:plain

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

4-7 鏡に映したように変換する「左右反転映像」

img_src = cv2.imread("data/img_4-1.bmp")

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

f:id:bitop:20180218070629p:plain

f:id:bitop:20180304054127p:plain

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

4-6 湖面に映る風景のように変換する上下反転画像

img_src = cv2.imread("data/img_4-1.bmp")

img_dst = cv2.flip(img_src,0)

描画する

cv2.imshow("img_src",img_src) cv2.imshow("img_dst",img_dst) cv2.waitKey(0) cv2.destroyAllWindows()

f:id:bitop:20180218070629p:plain

f:id:bitop:20180303110917p:plain

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

4-5 垂直方向に揺らめかせる「サイン揺らぎ(垂直)」

img_src = cv2.imread("data/img_4-5.bmp")

#振幅
A = 50
#dst画像用意
img_dst = np.zeros_like(img_src)

Y = img_src.shape[0]
X = img_src.shape[1]

for y in range(Y):
    for x in range(X):
        x2 = int(x + A*np.sin(np.pi*y/180))
        if x2 < 959:
            img_dst[y,x2] = img_src[y,x]

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

f:id:bitop:20180227114202p:plain

f:id:bitop:20180227114246p:plain

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

4-4 水平方向に揺らめかせる「サイン揺らぎ(水平)」

img_src = cv2.imread("data/img_4-4.bmp")

#振幅
A = 50
#dst画像用意
img_dst = np.zeros_like(img_src)

Y = img_src.shape[0]
X = img_src.shape[1]

for y in range(Y):
    for x in range(X):
        y2 = int(y + A*np.sin(f*np.pi*x/180))
        if y2 < 639:
            img_dst[y2,x] = img_src[y,x]

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

ちょっと違ったが

f:id:bitop:20180225084803p:plain

f:id:bitop:20180225084830p:plain