메뉴 건너뛰기

OBG

Programming

Python
2014.01.16 00:49

OpenCV 이용한 템플릿 매칭

MoA
조회 수 1255 추천 수 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 1755
187 C/C++ Essential C 링크 너울 2011.08.31 303
186 Library ExcelFormat Library Naya 2012.08.02 874
185 C/C++ extern "C" 에 관하여 MoA 2013.07.28 321
184 Python FastAPI 톺아보기 - 부제: python 백엔드 봄은 온다 OBG 2023.01.25 120
183 Tool/etc Flash CS5 and Version Control MoA 2013.10.11 351
182 C/C++ fopen 함수가 Multi Thread 에서 안전한가? MoA 2013.07.28 591
181 C/C++ fwrite(), fread() MoA 2013.07.28 622
180 API/MFC GetLastInputInfo 함수 MoA 2013.12.06 437
179 LLM Getting Started with Sentiment Analysis using Python OBG 2024.04.11 41
178 Site GOF 디자인패턴 정리 MoA 2013.07.28 355
177 서버 Golang Tutorial for Node.js Developers, Part I.: Getting started OBG 2022.06.16 135
176 Library Google의 C++ 라이브러리 Naya 2012.08.02 439
175 C/C++ Google의 C++ 라이브러리 MoA 2013.07.28 483
174 Site Great summary cheat sheet (OpenCV) MoA 2013.01.04 409
173 Library High-speed Charting Control MoA 2013.07.28 375
172 LLM How LLMs Work ? Explained in 9 Steps — Transformer Architecture OBG 2024.04.11 26
171 Web How to send dynamic charts with a Slack bot OBG 2022.05.31 140
170 Tool/etc How To Set Up Multi-Factor Authentication for SSH on Ubuntu 20.04 OBG 2023.01.17 101
169 Tool/etc How to stop programmers to copy the code from GitHub when they leave the company? OBG 2024.01.02 119
168 Tool/etc HuggingFace 공동창업자가 추천하는 AI 분야 입문 서적 OBG 2024.05.24 36
Board Pagination Prev 1 ... 2 3 4 5 6 7 8 9 10 ... 15 Next
/ 15
위로