Processor

【OpenCV】 템플릿 매칭(Template Matching)

작성자 임베디드코리아 작성일26-04-16 22:15 조회107회 댓글0건
< *  템플릿 매칭(Template Matching)  * >

◆ 템플릿 매칭은 원본 이미지에서 템플릿 이미지와 일치하는 영역을 찾는 알고리즘 이다.
◆ 원본 이미지 위에 템플릿 이미지를 놓고 조금씩 이동해가며 이미지 끝에 도달할 때 까지 비교해 찾아간다.
◆ 이 방식을 통해, 템플릿 이미지와 동일하거나, 가장 유사한 영역을 원본 이미지에서 검출한다.


----< 예제 : Template_Matching.py  >-------------------------------------------------------------------
import cv2

src = cv2.imread("Image/hats.png", cv2.IMREAD_GRAYSCALE)
templit = cv2.imread("Image/hat.png", cv2.IMREAD_GRAYSCALE)
dst = cv2.imread("hats.png")

result = cv2.matchTemplate(src, templit, cv2.TM_SQDIFF_NORMED)

minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(result)
x, y = minLoc
h, w = templit.shape

dst = cv2.rectangle(dst, (x, y), (x +  w, y + h) , (0, 0, 255), 1)
cv2.imshow("dst", dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
--------------------------------------------------------------------------------------------------------