您的位置:首页 > 编程语言 > Python开发

opencv for python 之 模板匹配

2012-12-14 17:25 645 查看
import cv2.cv as cv

#load image

filename = "../Video/cat.jpg"

image = cv.LoadImage(filename)

#create one window

win_name = "test"

cv.NamedWindow(win_name)

win2_name = "test2"

cv.NamedWindow(win2_name)

#take off one template

rect = (170,80,50,50)

cv.SetImageROI(image, rect)

template = cv.CloneImage(image)

cv.ShowImage(win_name, template)

 

cv.ResetImageROI(image)

W,H=cv.GetSize(image)

w,h=cv.GetSize(template)

width=W-w+1

height=H-h+1

result=cv.CreateImage((width,height),32,1)

result 是一个矩阵,存储了模板与源图像每一帧相比较后的相似值,

cv.MatchTemplate(image,template, result,cv.CV_TM_SQDIFF)

下面的操作将从矩阵中找到相似值最小的点,从而定位出模板位置

(min_x,max_y,minloc,maxloc)=cv.MinMaxLoc(result)

(x,y)=minloc

cv.Rectangle(image,(int(x),int(y)),(int(x)+w,int(y)+h),(255,255,255),1,0)

cv.ShowImage(win2_name, image)

cv.WaitKey()

模板匹配结果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Python