개발 공부

4강 이미지 회전 본문

AI/AI_Python_OPENCV 기초

4강 이미지 회전

아이셩짱셩 2018. 8. 7. 21:29

import numpy as np

import cv2


#기울기 등을 변경해서 여러 형식으로 테스트하기 위해 함수를 만듭니다.

def rotate(image, angle, center = None, scale = 1.0):

   # 다음은 이미지의 높이와 넓이 정보를 가져 옵니다. [:2]는 [0:2]와 같으면 0부터 1까지 이미지 배열값을 말합니다. h는 높이를 받는 변수이며 w는 넓이를 받는 변수입니다.

   (h, w) = image.shape[:2]


   # If the center is None, initialize it as the center of

   # the image

   if center is None:

      center = (w / 2, h / 2)


#사용하는 함수는 cv2.getRotationMatrix2D(중심값, 기울기, scale). 스케일은 크기 비율을 생각하면 됩니다. 1.0이면 원본 크기를 말합니다.

   M = cv2.getRotationMatrix2D(center, angle, scale)

   rotated = cv2.warpAffine(image, M, (w, h))


   # Return the rotated image

   return rotated


# Load the image and show it

image = cv2.imread('p2.jpg')

cv2.imshow("Original", image)


# Grab the dimensions of the image and calculate the center

# of the image

(h, w) = image.shape[:2]

center = (w / 2, h / 2)


# Rotate our image by 45 degrees

rotated = rotate(image, 45)

cv2.imshow("Rotated by 45 Degrees", rotated)


# Rotate our image by -90 degrees

rotated = rotate(image, -90)

cv2.imshow("Rotated by -90 Degrees", rotated)


# Finally, let's use our helper function in imutils.py to

# rotate the image by 180 degrees (flipping it upside down)

rotated = rotate(image, 180)

cv2.imshow("Rotated by 180 Degrees", rotated)

cv2.waitKey(0)



#위 아래 바꾸기, 앞 뒤 바꾸기의 경우는 다음을 사용하면 쉽다.

#1을 넣으면 꼬리와 머리가 뒤바뀐다. 0은 거꾸로로 된다. -1은 1과 0이 한꺼번에 작용된다.

#flipped = cv2.flip(image, 1)

#cv2.imshow("Flipped Horizontally", flipped)



'AI > AI_Python_OPENCV 기초' 카테고리의 다른 글

6강  (0) 2018.08.07
5강  (0) 2018.08.07
3강  (0) 2018.08.07
2강 선 원 박스 그리기  (0) 2018.08.07
1강  (0) 2018.08.07
Comments