Skip to content

Instantly share code, notes, and snippets.

@romichi
Created May 22, 2012 16:27
Show Gist options
  • Save romichi/2770093 to your computer and use it in GitHub Desktop.
Save romichi/2770093 to your computer and use it in GitHub Desktop.
openCVでテンプレートマッチング
# -*- coding:utf-8 -*-
import cv
number = cv.LoadImage('number.png') #比較対象画像
template = cv.LoadImage('five.png') #テンプレート
result = cv.CreateImage((number.width - template.width + 1, #結果を入れる領域の作成
number.height - template.height + 1), 32, 0)
cv.MatchTemplate(number, template, result, cv.CV_TM_SQDIFF) #テンプレートマッチ
loc = cv.MinMaxLoc(result)
#マッチング結果を囲う
cv.Rectangle(number, loc[2], (loc[2][0] + template.width, loc[2][1] + template.height),
(0, 51, 255), 1, 8, 0)
#結果の表示
cv.NamedWindow("number")
cv.ShowImage("number", number)
cv.WaitKey(0)
cv.DestroyWindow("number")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment