__2017-12-11 如一模式识别研究

如一模式识别研究

基本数学知识>>Opencv Python版学习笔记(三)模板匹配

转自: http://blog.csdn.net/gjy095/article/details/9203755

模板匹配:模板匹配是通过目标图片在待匹配图片进行遍历,通过选择一定的匹配方式能够得到每个起始像素点的匹配值,最终匹配值最大的位置就是候选匹配位置,也就达到了匹配查找的效果

本例是通过鼠标选取图像中的一块矩形区域,遍历原图像后得到一个匹配值矩阵,将矩阵转换成0-255的灰度图像形式显示出来,实验结果图如下

图中最亮的点即为最佳的匹配位置,从中也可以看出即为原图中矩形区域中心点

程序和注释如下:

[python] view plaincopy

#decoding:utf-8

#!/usr/bin/env python

'''''

mouse_and_match.py [-i path | --input path: default ./]

Demonstrate using a mouse to interact with an image:

Read in the images in a directory one by one

Allow the user to select parts of an image with a mouse

When they let go of the mouse, it correlates (using matchTemplate) that patch with the image.

ESC to exit

'''

import numpy as np

from math import *

import sys

import os

import glob

import argparse

import cv2 as cv

drag_start = None#全局变量取方块鼠标拖拽时使用

sel = (0,0,0,0)#全局变量 长方形左上颌右下定点坐标存储

def onmouse(event, x, y, flags, param):#鼠标事件响应函数

global drag_start, sel

if event == cv.EVENT_LBUTTONDOWN:#左键按下时记录当前初始坐标,并初始化矩形sel

drag_start = x, y

sel = 0,0,0,0

elif event == cv.EVENT_LBUTTONUP:#鼠标左键叹弹起时响应

if sel[2] > sel[0] and sel[3] > sel[1]:#判断右下角坐标是否大于左上角

patch = gray[sel[1]:sel[3],sel[0]:sel[2]]#取矩形区域内像素作为patch图像

result = cv.matchTemplate(gray,patch,cv.TM_CCOEFF_NORMED)#返回遍历后匹配值矩阵,这里选择归一化相关系数匹配

result = np.abs(result)**3

val, result = cv.threshold(result, 0.01, 0, cv.THRESH_TOZERO)#将低于0。01的值赋值为0

result8 = cv.normalize(result,None,0,255,cv.NORM_MINMAX,cv.CV_8U)#将result转化到0-255区间

cv.imshow("result", result8)

drag_start = None

elif drag_start:

#print flags

if flags & cv.EVENT_FLAG_LBUTTON:#取当前坐标与初始坐标较小的为矩形坐标左上,较大的为右下

minpos = min(drag_start[0], x), min(drag_start[1], y)

maxpos = max(drag_start[0], x), max(drag_start[1], y)

sel = minpos[0], minpos[1], maxpos[0], maxpos[1]

img = cv.cvtColor(gray, cv.COLOR_GRAY2BGR)

cv.rectangle(img, (sel[0], sel[1]), (sel[2], sel[3]), (0,255,255), 1)

cv.imshow("gray", img)

else:

print "selection is complete"

drag_start = None

if __name__ == '__main__':

parser = argparse.ArgumentParser(description='Demonstrate mouse interaction with images')

#命令行输入参数 mouse_and_match.py -i 输入图像路径 默认为E:/imagetest

parser.add_argument("-i","--input", default='E:/imagetest', help="Input directory.")

args = parser.parse_args()

path = args.input#获取图像路径参数

cv.namedWindow("gray",1)

cv.setMouseCallback("gray", onmouse)

'''''Loop through all the images in the directory'''

for infile in glob.glob( os.path.join(path, '*.*') ):#遍历文件夹下的图片文件

ext = os.path.splitext(infile)[1][1:] #get the filename extenstion

if ext == "png" or ext == "jpg" or ext == "bmp" or ext == "tiff" or ext == "pbm":

print infile

img=cv.imread(infile,1)

if img == None:

continue

sel = (0,0,0,0)

drag_start = None

gray=cv.cvtColor(img, cv.COLOR_BGR2GRAY)

cv.imshow("gray",gray)

if (cv.waitKey() & 255) == 27:

break

cv.destroyAllWindows()

评论留言区

:
  

作者: 游客 ; *
评论内容: *
带*号为必填项目

如一模式识别更新提示

matlab在图像处理方面的应用有更新

如一模式识别 友情链接

关于本站作者     chinaw3c     mozilla