#画像をx軸またはy軸に移動させる
■コード
translate.py
import cv2
import numpy as np
#read image
img = cv2.imread("gotg_rocket.jpg")
#shift image to left(negative value) or right(positive value)
tx = 100
#shift image to up(negative value) or down(positive value)
ty = 100
#transformation matrix(need to be float value)
M = np.float32([[1,0,tx],[0,1,ty]])
#width and height of image
h = img.shape[0]
w = img.shape[1]
#translate image
translated = cv2.warpAffine(img,M,(w,h))
#show image
cv2.imshow("Original",img)
cv2.imshow("Translated", translated)
cv2.waitKey(0)
#画像を回転させる
■コード
rotate.py
import cv2
#read image
img = cv2.imread("gotg_rocket.jpg")
#width and height of image
h = img.shape[0]
w = img.shape[1]
#get center of image
center = (w//2,h//2)
#rotation angle(in degree)
angle = 40
#scale rate(if don't want to change the size of image, set to 1)
scale = 1.0
#transformation matrix
M = cv2.getRotationMatrix2D(center,angle,scale)
#rotate image
rotated = cv2.warpAffine(img,M,(w,h))
#show image
cv2.imshow("Original",img)
cv2.imshow("Rotated", rotated)
cv2.waitKey(0)
#画像のサイズを変更させる
■コード
resize.py
import cv2
#read image
img = cv2.imread("gotg_rocket.jpg")
#resize width
w = 300
#resize height(keeping the scale rate)
h = int(300/img.shape[1]*img.shape[0])
#resize image
resized = cv2.resize(img,(w,h))
#show image
cv2.imshow("Original",img)
cv2.imshow("Resized", resized)
cv2.waitKey(0)
#画像を反転させる
■コード
flip.py
import cv2
#read image
img = cv2.imread("gotg_rocket.jpg")
#flip image (-1:vertical & horizontal, 0: vertically, 1:horizontal)
flipped = cv2.flip(img,0)
#show image
cv2.imshow("Original",img)
cv2.imshow("Flipped", flipped)
cv2.waitKey(0)