■ matchTemplate 함수를 사용해 템플리트 이미지를 찾는 방법을 보여준다.
▶ 예제 코드 (PY)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import cv2 imageNDArray = cv2.imread("source.jpg") grayscaleImageNDArray = cv2.cvtColor(imageNDArray, cv2.COLOR_BGR2GRAY) templateImageNDArray = cv2.imread("template.jpg") grayscaleTemplateImageNDArray = cv2.cvtColor(templateImageNDArray, cv2.COLOR_BGR2GRAY) imageWidth, imageHeight = grayscaleTemplateImageNDArray.shape[::-1] outputNDArray = cv2.matchTemplate(grayscaleImageNDArray, grayscaleTemplateImageNDArray, cv2.TM_CCOEFF_NORMED) minimumValue, maximumValue, minimumLocation, maximumLocation = cv2.minMaxLoc(outputNDArray) leftTopLocation = maximumLocation rightBottomLocation = (leftTopLocation[0] + imageWidth, leftTopLocation[1] + imageHeight) cv2.rectangle(imageNDArray, leftTopLocation, rightBottomLocation, 255, 2) cv2.imshow("image", imageNDArray) cv2.imwrite("target.jpg", imageNDArray) cv2.waitKey(0) cv2.destroyAllWindows() |