Affine Transformation
영상 평행 이동, 회전, 크기 변환, 전단 변환
직선은 그대로 직선, 직선 간의 길이 비율, 평행 관계 유지
ex. 직사각형 → 평행사변형
6개의 파라미터
어파인 변환 행렬 구하는 함수
영상을 어파인 변환
warpAffine(src, dst, M, dsize, flags =INTER_LINEAR, borderMode=BORDER_CONSTRANT, const Scalar& borderValue=Scalar());
M : 2*3 어파인 변환 행렬
dsize : 결과 영상 크기
flags : 보간법 알고리즘
rows = src.shape[0]
cols = src.shape[1]
src_pts = np.array([[0, 0],
[cols - 1, 0],
[cols - 1, rows - 1]]).astype(np.float32)
dst_pts = np.array([[50, 50],
[cols - 100, 100],
[cols - 50, rows - 50]]).astype(np.float32)
affine_mat = cv2.getAffineTransform(src_pts, dst_pts)
dst = cv2.warpAffine(src, affine_mat, (0, 0))
Translation Transformation, shift
(150,100) 만큼 이동
affine_mat = np.array([[1, 0, 150],
[0, 1, 100]]).astype(np.float32)
dst = cv2.warpAffine(src, affine_mat, (0, 0))
rows = src.shape[0]
cols = src.shape[1]
mx = 0.3
affine_mat = np.array([[1, mx, 0],
[0, 1, 0]]).astype(np.float32)
dst = cv2.warpAffine(src, affine_mat, (int(cols + rows * mx), rows))
Sx, Sy > 1 : 영상 확대, < 1 : 영상 축소
resize(src, dst, dsize, fx = 0, fy = 0, interpolation = INTER_LINEAR)
보간법
Rotation Transformation
특정 좌표 기준 → 영상 원하는 각도만큼 회전
삼각함수 이용
getRotationMatrix2D(center, angle, scale)
center : 회전 중심 좌표
angle : 회전 각도, 양수(반시계), 음수(시계)
scale : 회전 후 추가적으로 확대,축소할 비율 (1 = 크기 변환 X)
→ 2*3 어파인 변환 행렬 리턴
cp = (src.shape[1] / 2, src.shape[0] / 2) # 영상 중심 기준
affine_mat = cv2.getRotationMatrix2D(cp, 20, 1) # 반시계 20도
dst = cv2.warpAffine(src, affine_mat, (0, 0))
flipcode > 0 (좌우), = 0 (상하), < 0 (둘 다)
for flip_code in [1, 0, -1]:
dst = cv2.flip(src, flip_code)
def on_mouse(event, x, y, flags, param):
global cnt, src_pts
if event == cv2.EVENT_LBUTTONDOWN:
if cnt < 4:
src_pts[cnt, :] = np.array([x, y]).astype(np.float32)
cnt += 1
cv2.circle(src, (x, y), 5, (0,0,255), -1)
cv2.imshow('src', src)
if cnt == 4:
w = 200
h = 300
dst_pts = np.array([[0, 0],
[w-1, 0],
[w-1, h-1],
[0, h-1]]).astype(np.float32)
pers_mat = cv2.getPerspectiveTransform(src_pts, dst_pts)
dst = cv2.warpPerspective(src, pers_mat, (w, h))
cv2.imshow('dst', dst)
cnt = 0
src_pts = np.zeros([4,2], dtype=np.float32)
src = cv2.imread('card.bmp')
if src is None:
print('Image load failed!')
exit()
cv2.namedWindow('src')
cv2.setMouseCallback('src', on_mouse)
cv2.imshow('src', src)
cv2.waitKey(0)
cv2.destroyAllWindows()