메뉴 건너뛰기

OBG

Programming

Python
2014.01.16 00:49

OpenCV 이용한 템플릿 매칭

MoA
조회 수 1251 추천 수 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 1725
127 LLM llama3 implemented from scratch OBG 2024.05.24 25
126 Deeplearning LLaMA: INT8 edition OBG 2023.03.09 78
125 API/MFC Legacy MFC 어플리케이션을 MFC feature pack으로 포팅 MoA 2013.07.30 539
124 Deeplearning Keras를 활용한 주식 가격 예측 OBG 2022.09.02 114
123 Site IT 세미나 유튜브 동영상 Naya 2012.09.10 252
122 API/MFC InvalidateRect 함수, flag성 메세지 MoA 2013.07.25 378
121 API/MFC HWND와 HINSTANCE MoA 2013.07.28 344
120 Tool/etc HuggingFace 공동창업자가 추천하는 AI 분야 입문 서적 OBG 2024.05.24 32
119 Tool/etc How to stop programmers to copy the code from GitHub when they leave the company? OBG 2024.01.02 114
118 Tool/etc How To Set Up Multi-Factor Authentication for SSH on Ubuntu 20.04 OBG 2023.01.17 99
117 Web How to send dynamic charts with a Slack bot OBG 2022.05.31 136
116 LLM How LLMs Work ? Explained in 9 Steps — Transformer Architecture OBG 2024.04.11 21
115 Library High-speed Charting Control MoA 2013.07.28 373
114 Site Great summary cheat sheet (OpenCV) MoA 2013.01.04 406
113 Library Google의 C++ 라이브러리 Naya 2012.08.02 439
112 C/C++ Google의 C++ 라이브러리 MoA 2013.07.28 478
111 서버 Golang Tutorial for Node.js Developers, Part I.: Getting started OBG 2022.06.16 129
110 Site GOF 디자인패턴 정리 MoA 2013.07.28 352
109 LLM Getting Started with Sentiment Analysis using Python OBG 2024.04.11 28
108 API/MFC GetLastInputInfo 함수 MoA 2013.12.06 434
Board Pagination Prev 1 ... 4 5 6 7 8 9 10 11 12 13 ... 15 Next
/ 15
위로