로그인

검색

Python
2014.01.16 00:49

OpenCV 이용한 템플릿 매칭

MoA
조회 수 3918 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 게시글 수정 내역 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 게시글 수정 내역 댓글로 가기 인쇄

템플릿 매칭(Template Matching) = 큰 이미지에서 특정 작은 이미지를 찾는 알고리즘


import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('messi5.jpg',0)
img2 = img.copy()
template = cv2.imread('template.jpg',0)
w, h = template.shape[::-1]

# All the 6 methods for comparison in a list
methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR',
            'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']

for meth in methods:
    img = img2.copy()
    method = eval(meth)

    # Apply template Matching
    res = cv2.matchTemplate(img,template,method)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

    # If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
    if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
        top_left = min_loc
    else:
        top_left = max_loc
    bottom_right = (top_left[0] + w, top_left[1] + h)

    cv2.rectangle(img,top_left, bottom_right, 255, 2)

    plt.subplot(121),plt.imshow(res,cmap = 'gray')
    plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
    plt.subplot(122),plt.imshow(img,cmap = 'gray')
    plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
    plt.suptitle(meth)

    plt.show()

https://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_imgproc/py_template_matching/py_template_matching.html

?

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
공지 Tool/etc Programming 게시판 관련 2 MoA 2014.11.01 15452
198 API/MFC DoModal Dialog 기초 MoA 2013.07.28 2437
197 Reversing Dumpbin.exe 사용 Naya 2012.10.21 2584
196 Python Embedding Python in C/C++ MoA 2013.12.23 3064
195 C/C++ Essential C 링크 너울 2011.08.31 3477
194 Library ExcelFormat Library Naya 2012.08.02 3735
193 C/C++ extern "C" 에 관하여 MoA 2013.07.28 3315
192 Python FastAPI 톺아보기 - 부제: python 백엔드 봄은 온다 OBG 2023.01.25 2843
191 Tool/etc Flash CS5 and Version Control MoA 2013.10.11 2913
190 C/C++ fopen 함수가 Multi Thread 에서 안전한가? MoA 2013.07.28 3389
189 C/C++ fwrite(), fread() MoA 2013.07.28 3220
188 Tool/etc GDB Dashboard OBG 2025.01.14 1806
187 API/MFC GetLastInputInfo 함수 MoA 2013.12.06 3337
186 LLM Getting Started with Sentiment Analysis using Python OBG 2024.04.11 2418
185 Tool/etc GitHut Copilot - Agent 모드 공개 OBG 2025.02.14 1856
184 Site GOF 디자인패턴 정리 MoA 2013.07.28 2750
183 서버 Golang Tutorial for Node.js Developers, Part I.: Getting started OBG 2022.06.16 2251
182 Library Google의 C++ 라이브러리 Naya 2012.08.02 2987
181 C/C++ Google의 C++ 라이브러리 MoA 2013.07.28 2997
180 Site Great summary cheat sheet (OpenCV) MoA 2013.01.04 3048
179 Library High-speed Charting Control MoA 2013.07.28 2830
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 15 Next
/ 15