Opencv

7-3-2 OpenCV 3.3의 기초 사용법-II

coding art 2019. 12. 21. 12:49
728x90

OpenCV 3.3.0 버전의 기초적인 사용법을 알아보았다. 한편 imutils 라이브러리 모듈이 설치되어 있는 가상환경에 들어가서 이미지 처리 작업을 하면 OpenCV 와 결합된 imutils 라이브러리가 제공하는 이미지 회전을 포함하는 추가적인 중요 기능을 사용할 수 있게 된다. OpenCV에서 이미지를 회전시키기 위해서는 getRotationMatrix2D 명령에 의해서 회전 매트릭스를 계산 후 warpAffine 명령에 의해 이미지의 왜곡 및 회전이 이루어진다. getRotationMatrix2D 명령에서 필요한 데이터는 이미지의 중심 좌표, 회전각도 및 최종 이미지의 축척 비율로 구성된다.

 

 

 

한편 imutils 라이브러리 모듈에서 제공하는 imutils.rotate 명령을 사용하면 한 줄로 코딩이 가능하다.

 

 

그밖에도 이미지 회전 시 이미지가 잘려 나가지 않고 전체가 나타나는 imutils.rotate_bound 명령이 있음을 참고하자.

 

이미지 처리 과정에서 이미지의 세부에 지나칠 정도로 또렸한 high frequency noise 가 있을 수 있으므로 이러한 현상을 약화시키기 위한 즉 이미지를 일부러 흐릿하게 만들기 위한 기능으로서 GaussianBlur 명령이 있다. 이 명령은 NXN 커늘을 사용하여 Convolution 작업을 실시하는데 이 기법 자체는 CNN(Convolutionary Neural Network) 알고리듬에서 사용하는 기법이지만 이미지 처리에서도 사용이 된다.

 

 

이미지를 바탕으로 하여 그 위에 도형 즉 원(cv2.circle), 사각형(cv2.rectangle) 및 직선(cv2.line)을 작도해 보자. 이 예제를 만들어 보기 위해서 원 이미지를 파이썬 명령 .copy()를 사용하여 복사한 이미지를 사용하도록 하며 파이썬 또는 Numpy 에서의 좌표는 (높이, )=(h, w)=(y, x) 형식이었지만 OpenCV에서의 좌표는 수학에서처럼 (, 높이)=(w, h)=(x, y) 형식으로 주어진다. 이미지 파일에서의 원점은 이지지 좌상단이 됨에 유의하자.

 

 

3가지 경우에 대한 작도 결과를 참조하자.

 

 

이미지 상에 putText 명령을 사용하여 텍스트를 넣어 보자.

 

 

여기까지의 OpenCV 사용법을 정리해 보면 여타의 그래픽 라이브러리와 비슷한 내용임을 알 수 있다. 특히 원이나 사각형 작도 기능은 안면 인식에서 흔히 사용되며 사람인식은 주로 사각형 작도 기능을 사용한다. 아울러 도형 주변의 위치에 오브젝트 인식내용 확률값을 텍스트로 출력하기도 한다.

 

 

# python opencv_tutorial_01.py

 

# import the necessary packages

import imutils

import cv2

 

# load the input image and show its dimensions, keeping in mind that

# images are represented as a multi-dimensional NumPy array with

# shape no. rows (height) x no. columns (width) x no. channels (depth)

image = cv2.imread("jp.png")

(h, w, d) = image.shape

print("width={}, height={}, depth={}".format(w, h, d))

 

# display the image to our screen -- we will need to click the window

# open by OpenCV and press a key on our keyboard to continue execution

cv2.imshow("Image", image)

cv2.waitKey(0)

 

# access the RGB pixel located at x=50, y=100, keepind in mind that

# OpenCV stores images in BGR order rather than RGB

(B, G, R) = image[100, 50]

print("R={}, G={}, B={}".format(R, G, B))

 

# extract a 100x100 pixel square ROI (Region of Interest) from the

# input image starting at x=320,y=60 at ending at x=420,y=160

roi = image[60:160, 320:420]

cv2.imshow("ROI", roi)

cv2.waitKey(0)

 

# resize the image to 200x200px, ignoring aspect ratio

resized = cv2.resize(image, (200, 200))

cv2.imshow("Fixed Resizing", resized)

cv2.waitKey(0)

 

# fixed resizing and distort aspect ratio so let's resize the width

# to be 300px but compute the new height based on the aspect ratio

r = 300.0 / w

dim = (300, int(h * r))

resized = cv2.resize(image, dim)

cv2.imshow("Aspect Ratio Resize", resized)

cv2.waitKey(0)

 

# manually computing the aspect ratio can be a pain so let's use the

# imutils library instead

resized = imutils.resize(image, width=300)

cv2.imshow("Imutils Resize", resized)

cv2.waitKey(0)

 

# let's rotate an image 45 degrees clockwise using OpenCV by first

# computing the image center, then constructing the rotation matrix,

# and then finally applying the affine warp

center = (w // 2, h // 2)

M = cv2.getRotationMatrix2D(center, -45, 1.0)

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

cv2.imshow("OpenCV Rotation", rotated)

cv2.waitKey(0)

 

# rotation can also be easily accomplished via imutils with less code

rotated = imutils.rotate(image, -45)

cv2.imshow("Imutils Rotation", rotated)

cv2.waitKey(0)

 

# OpenCV doesn't "care" if our rotated image is clipped after rotation

# so we can instead use another imutils convenience function to help

# us out

rotated = imutils.rotate_bound(image, 45)

cv2.imshow("Imutils Bound Rotation", rotated)

cv2.waitKey(0)

 

# apply a Gaussian blur with a 11x11 kernel to the image to smooth it,

# useful when reducing high frequency noise

blurred = cv2.GaussianBlur(image, (11, 11), 0)

cv2.imshow("Blurred", blurred)

cv2.waitKey(0)

 

# draw a 2px thick red rectangle surrounding the face

output = image.copy()

cv2.rectangle(output, (320, 60), (420, 160), (0, 0, 255), 2)

cv2.imshow("Rectangle", output)

cv2.waitKey(0)

 

# draw a blue 20px (filled in) circle on the image centered at

# x=300,y=150

output = image.copy()

cv2.circle(output, (300, 150), 20, (255, 0, 0), -1)

cv2.imshow("Circle", output)

cv2.waitKey(0)

 

# draw a 5px thick red line from x=60,y=20 to x=400,y=200

output = image.copy()

cv2.line(output, (60, 20), (400, 200), (0, 0, 255), 5)

cv2.imshow("Line", output)

cv2.waitKey(0)

 

# draw green text on the image

output = image.copy()

cv2.putText(output, "OpenCV + Jurassic Park!!!", (10, 25), 

cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)

cv2.imshow("Text", output)

cv2.waitKey(0)