줘이리의 인생적기

CV - opencv(Python) Geometric Transform(설명필요) 본문

공부/Computer Vision(py)

CV - opencv(Python) Geometric Transform(설명필요)

줘이리 2020. 7. 23. 08:00
728x90

Geometric Transform에 대해 알아보겠습니다.

 

Spatial Transform

-Scaling

-Rotation

-Translation

-Skew

 

Affine Transform

-Linear tranform

-이동, 회전, 스케일 및 이들의 조합에 의한 변환

(설명 필요)

 

Warping

-Nonlinear transform

(설명 필요)

 

1번 이미지 잘라서 붙여넣기

 

2번 이미지 이동

 

3번 이미지 회전, 확대, 축소

 

4번 Affine transform

 

코드 원형 설명

def imageResize(image, dsize=None, fx=0.0, fy=0.0, interpolation=cv2.INTER_LINEAR):
	if dsize is None and fx == 0.0 and fy == 0.0:
    fx = 1.0
    fy = 1.0
	return cv2.resize(image, dsize=dsize, fx=fx, fy=fy, interpolation=interpolation)

def imageTranslation(image, size=None, dx=0.0, dy=0.0, flags=cv2.INTER_LINEAR):
    M = np.float32([[1, 0, dx], [0, 1, dy]])
    if size is None:
        rows, cols = image.shape[:2]
        size = (cols, rows)
    return cv2.warpAffine(image, M, size, flags=flags)

def imageRotation(image, center=None, angle=0.0, scale=1.0, size=None, flags=cv2.INTER_LINEAR):
    if center is None:
        rows, cols = image.shape[:2]
        center = (cols/2, rows/2)
    if size is None:
        rows, cols = image.shape[:2]
        size = (cols, rows)
    M = cv2.getRotationMatrix2D(center, angle, scale)
    return cv2.warpAffine(image, M, size, flags=flags)
    
def imageAffineTransformation(image, src_pts, dst_pts, size=None, flags=cv2.INTER_LINEAR):
    if size is None:
        rows, cols = image.shape[:2]
        size = (cols, rows)
    M = cv2.getAffineTransform(src_pts, dst_pts)
    return cv2.warpAffine(image, M, dsize=size, flags=flags)

 

 

1번 코드 설명

더보기
from OpenCV_Functions import *

#이미지 설정
imagePath = "<이미지 경로>"
image = imageRead(imagePath) 
imageShow("image", image)

#잘라내기
roi_x1 = 192
roi_y1 = 303
roi_x2 = 278
roi_y2 = 352
roi_rect = CutRectROI(image, roi_x1, roi_y1, roi_x2, roi_y2)
imageShow("cut_image", roi_rect)

height, width = roi_rect.shape[:2]
roi_resize_01 = imageResize(roi_rect, (int(width*1.5), int(height*1.5)))
roi_resize_02 = imageResize(roi_rect, fx = 1.5, fy = 1.5)

image2 = imageCopy(image)

#붙여넣기
roi_new_x1 = 16
roi_new_y1 = 330
image2 = PasteRectROI(roi_resize_01, roi_new_x1, roi_new_y1, image2)
imageShow("paste_image", image2)

roi_new_x1 = 219
roi_new_y1 = 338
image2 = PasteRectROI(roi_resize_02, roi_new_x1, roi_new_y1, image2)
imageShow("paste_image2", image2)

cv2.destroyAllWindows()

2번 코드 설명

더보기
from OpenCV_Functions import *

#이미지 설정
imagePath = "<이미지 경로>"
image = imageRead(imagePath) 
height, width = image.shape[:2]
print(height, width)

#이미지 이동
dx, dy = 100, 200
size = (width+dx, height+dy)
image_Translation_01 = imageTranslation(image, size=size, dx=dx, dy=dy)

imageShow("image", image)
imageShow("image_Translation", image_Translation_01)

cv2.destroyAllWindows()

 

3번 코드 설명

더보기
from OpenCV_Functions import *

def nothing(x):
    pass

#이미지 설정
imagePath = "<이미지 경로>"
image = imageRead(imagePath) 
backup = np.copy(image)

height, width = image.shape[:2]
center_x = int(width * 0.5)
center_y = int(height * 0.5)

cv2.namedWindow('imageRotation', cv2.WINDOW_GUI_EXPANDED)

#트랙바 설정
cv2.createTrackbar('Center_x', 'imageRotation', center_x, width, nothing)
cv2.createTrackbar('Center_y', 'imageRotation', center_y, height, nothing)
cv2.createTrackbar('angle: value-180', 'imageRotation', 180, 360, nothing)
cv2.createTrackbar('scale: value[\%]', 'imageRotation', 100, 200, nothing)
cv2.createTrackbar('size: value[\%]', 'imageRotation', 100, 200, nothing)

switch = '0:OFF\n1:On'
cv2.createTrackbar(switch, 'imageRotation', 1, 1, nothing)

while True:
    cv2.imshow('imageRotation', image)

    if cv2.waitKey(1) & 0xFF == 27:
        break
    x = cv2.getTrackbarPos('Center_x', 'imageRotation')
    y = cv2.getTrackbarPos('Center_y', 'imageRotation')
    angle = cv2.getTrackbarPos('angle: value-180', 'imageRotation')
    scale = cv2.getTrackbarPos('scale: value[\%]', 'imageRotation')
    size = cv2.getTrackbarPos('size: value[\%]', 'imageRotation')
    sw = cv2.getTrackbarPos(switch, 'imageRotation')
    modified_size = (int(width*size*0.01),int(height*size*0.01))
    if sw == 0:
        image = backup
    else:
        image = backup
        image = imageRotation(image, (x, y), angle-180, scale*0.01, modified_size)

cv2.destroyAllWindows()

 

4번 코드 설명

더보기
from OpenCV_Functions import *

#이미지 설정
imagePath = "<이미지 경로>"
image = imageRead(imagePath) 

#점 설정
src_pt1 = [195, 304]
src_pt2 = [273, 304]
src_pt3 = [280, 351]
dst_pt1 = [404, 194]
dst_pt2 = [698, 198]
dst_pt3 = [698, 386]

src_pts = np.float32([src_pt1, src_pt2, src_pt3])
dst_pts = np.float32([dst_pt1, dst_pt2, dst_pt3])

#원 그리기
image_point = drawCircle(image, tuple(src_pt1), 10, (255, 0, 0), -1)
image_point = drawCircle(image_point, tuple(src_pt2), 10, (0, 255, 0), -1)
image_point = drawCircle(image_point, tuple(src_pt3), 10, (0, 0, 255), -1)

#Affine 변환
roadAffine_01 = imageAffineTransformation(image_point, src_pts, dst_pts)
roadAffine_02 = imageAffineTransformation(roadAffine_01, src_pts, dst_pts, flags=cv2.WARP_INVERSE_MAP)
roadAffine_03 = imageAffineTransformation(roadAffine_01, dst_pts, src_pts)

#창 띄우기
imageShow("image_point", image_point)
imageShow("roadAffine_01", roadAffine_01)
imageShow("roadAffine_02", roadAffine_02)
imageShow("roadAffine_03", roadAffine_03)

cv2.destroyAllWindows()