//Template matching
//Usage:matchTemplate image template
/*
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <stdio.h>
int main(int argc,char** argv){
IplImage *src = cvLoadImage("src.png",1);
IplImage *temp1 = cvLoadImage("temp.png",1);
IplImage *ftmp[6];//ftmp will hold results
int i;
//ALLOCATE OUTPUT IMAGES:
int iwidth = src->width - temp1->width +1;
int iheight = src->height - temp1->height +1;
for(i=0;i<6;++i){
ftmp[i] = cvCreateImage(cvSize(iwidth,iheight),32,1);
}
//DO THE MATCHING OF THE TEMPLATE WITH THE IAMGE:
for(i=0;i<6;++i){
cvMatchTemplate(src,temp1,ftmp[i],i);
cvNormalize(ftmp[i],ftmp[i],1,0,CV_MINMAX);
}
//DISPLAY
cvNamedWindow("Template",0);
cvShowImage("Template",temp1);
cvNamedWindow("Image",0);
cvShowImage("Image",src);
cvNamedWindow("SQDIFF",0);
cvShowImage("SQDIFF",ftmp[0]);
cvNamedWindow("SQDIFF_NORMED",0);
cvShowImage("SQDIFF_NORMED",ftmp[1]);
cvNamedWindow("CCORR",0);
cvShowImage("CCORR",ftmp[2]);
cvNamedWindow("CCORR_NORMED",0);
cvShowImage("CCORR_NORMED",ftmp[3]);
cvNamedWindow("CCOEFF",0);
cvShowImage("CCOEFF",ftmp[4]);
cvNamedWindow("CCOEFF_NORMED",0);
cvShowImage("CCOEFF_NORMED",ftmp[5]);
//LET USER VIEW RESULTS:
cvWaitKey(0);
}
*/
//单目标匹配
#include <iostream>
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
using namespace std;
int main()
{
IplImage *src = cvLoadImage("2.png", 0);
IplImage *srcResult = cvLoadImage("2.png", 3); //用来显示
IplImage *templat = cvLoadImage("temp1.png", 0);
IplImage *result;
if(!src || !templat)
{
cout << "打开图像失败"<< endl;
return 0;
}
int srcW, srcH, templatW, templatH, resultH, resultW;
srcW = src->width;
srcH = src->height;
templatW = templat->width;
templatH = templat->height;
if(srcW < templatW || srcH < templatH)
{
cout <<"模板必须比原图像小" << endl;
return 0;
}
resultW = srcW - templatW + 1;
resultH = srcH - templatH + 1;
result = cvCreateImage(cvSize(resultW, resultH), 32, 1);
cvMatchTemplate(src, templat, result, CV_TM_SQDIFF);
double minValue, maxValue;
CvPoint minLoc, maxLoc;
cvMinMaxLoc(result, &minValue, &maxValue, &minLoc, &maxLoc);
cvRectangle(srcResult, minLoc, cvPoint(minLoc.x + templatW, minLoc.y+ templatH), cvScalar(0,0,255));
cvNamedWindow("srcResult", CV_WINDOW_AUTOSIZE);
cvNamedWindow("templat", CV_WINDOW_AUTOSIZE);
cvShowImage("srcResult", srcResult);
cvShowImage("templat", templat);
cvWaitKey(0);
cvReleaseImage(&result);
cvReleaseImage(&templat);
cvReleaseImage(&srcResult);
cvReleaseImage(&src);
return 0;
}
//多目标匹配
/*
#include <iostream>
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
using namespace std;
CvPoint getNextMinLoc(IplImage *result, CvPoint minLoc, int maxVaule, int templatW, int templatH)
{
// 先将第一个最小值点附近两倍模板宽度和高度的都设置为最大值防止产生干扰
int startX = minLoc.x - templatW;
int startY = minLoc.y - templatH;
int endX = minLoc.x + templatW;
int endY = minLoc.y + templatH;
if(startX < 0 || startY < 0)
{
startX = 0;
startY = 0;
}
if(endX > result->width - 1 || endY > result->height - 1)
{
endX = result->width - 1;
endY = result->height - 1;
}
int y, x;
for(y = startY; y < endY; y++)
{
for(x = startX; x < endX; x++)
{
cvSetReal2D(result, y, x, maxVaule);
}
}
// 然后得到下一个最小值并且返回
double new_minVaule, new_maxValue;
CvPoint new_minLoc, new_maxLoc;
cvMinMaxLoc(result, &new_minVaule, &new_maxValue, &new_minLoc, &new_maxLoc);
return new_minLoc;
}
int main()
{
IplImage *src = cvLoadImage("src.png", 0);
IplImage *srcResult = cvLoadImage("src.png", 3); //用来显示
IplImage *templat = cvLoadImage("temp.png", 0);
IplImage *result; // 用来存放结果
if(!src || !templat)
{
cout << "打开图片失败" << endl;
return 0;
}
int srcW, srcH, templatW, templatH, resultH, resultW;
srcW = src->width;
srcH = src->height;
templatW = templat->width;
templatH = templat->height;
if(srcW < templatW || srcH < templatH)
{
cout << "模板不能比原图小" << endl;
return 0;
}
resultW = srcW - templatW + 1;
resultH = srcH - templatH + 1;
result = cvCreateImage(cvSize(resultW, resultH), 32, 1); // 匹配方法计算的结果最小值为float
cvMatchTemplate(src, templat, result, CV_TM_SQDIFF);
double minValue, maxValue;
CvPoint minLoc, maxLoc;
cvMinMaxLoc(result, &minValue, &maxValue, &minLoc, &maxLoc);
cvRectangle(srcResult, minLoc, cvPoint(minLoc.x + templatW, minLoc.y+ templatH), cvScalar(0,0,255));
CvPoint new_minLoc;
// 计算下一个最小值
new_minLoc = getNextMinLoc(result, minLoc, maxValue, templatW, templatH);
cvRectangle(srcResult, new_minLoc, cvPoint(new_minLoc.x + templatW, new_minLoc.y+ templatH), cvScalar(0,0,255));
// 再下一个
new_minLoc = getNextMinLoc(result, new_minLoc, maxValue, templatW, templatH);
cvRectangle(srcResult, new_minLoc, cvPoint(new_minLoc.x + templatW, new_minLoc.y+ templatH), cvScalar(0,0,255));
//再下一个
new_minLoc = getNextMinLoc(result, minLoc, maxValue, templatW, templatH);
cvRectangle(srcResult, new_minLoc, cvPoint(new_minLoc.x + templatW, new_minLoc.y+ templatH), cvScalar(0,0,255));
// 再下一个
new_minLoc = getNextMinLoc(result, new_minLoc, maxValue, templatW, templatH);
cvRectangle(srcResult, new_minLoc, cvPoint(new_minLoc.x + templatW, new_minLoc.y+ templatH), cvScalar(0,0,255));
// 再下一个
new_minLoc = getNextMinLoc(result, new_minLoc, maxValue, templatW, templatH);
cvRectangle(srcResult, new_minLoc, cvPoint(new_minLoc.x + templatW, new_minLoc.y+ templatH), cvScalar(0,0,255));
// 再下一个
new_minLoc = getNextMinLoc(result, new_minLoc, maxValue, templatW, templatH);
cvRectangle(srcResult, new_minLoc, cvPoint(new_minLoc.x + templatW, new_minLoc.y+ templatH), cvScalar(0,0,255));
// 再下一个
new_minLoc = getNextMinLoc(result, new_minLoc, maxValue, templatW, templatH);
cvRectangle(srcResult, new_minLoc, cvPoint(new_minLoc.x + templatW, new_minLoc.y+ templatH), cvScalar(0,0,255));
cvNamedWindow("srcResult", 0);
cvNamedWindow("templat", 0);
cvShowImage("srcResult", srcResult);
cvShowImage("templat", templat);
cvWaitKey(0);
cvReleaseImage(&result);
cvReleaseImage(&templat);
cvReleaseImage(&srcResult);
cvReleaseImage(&src);
return 0;
}
*/
没有合适的资源?快使用搜索试试~ 我知道了~
基于opencv模板匹配的人脸识别
共39个文件
tlog:16个
png:6个
log:2个
4星 · 超过85%的资源 需积分: 50 111 下载量 198 浏览量
2016-10-17
17:59:40
上传
评论 3
收藏 3.76MB RAR 举报
温馨提示
基于opencv模板匹配的人脸识别
资源推荐
资源详情
资源评论
收起资源包目录
模板匹配.rar (39个子文件)
模板匹配
模板匹配.sdf 15.19MB
模板匹配.v11.suo 21KB
模板匹配.psess 3KB
模板匹配.sln 998B
模板匹配151030.vsp 215KB
模板匹配
temp.png 286B
matching.cpp 6KB
src.png 1KB
模板匹配.vcxproj 4KB
1.png 519B
模板匹配.vcxproj.filters 946B
2.png 62KB
temp1.png 38KB
Debug
link.7528-cvtres.read.1.tlog 2B
模板匹配.lastbuildstate 58B
vc110.idb 963KB
CL.write.1.tlog 238B
link.7528.write.1.tlog 2B
CL.read.1.tlog 14KB
模板匹配.Build.CppClean.log 1KB
cl.command.1.tlog 550B
link-cvtres.read.1.tlog 2B
link.7528-rc.read.1.tlog 2B
link.write.1.tlog 286B
matching.obj 213KB
link.7528-cvtres.write.1.tlog 2B
link-rc.write.1.tlog 2B
link-cvtres.write.1.tlog 2B
link.command.1.tlog 2KB
link-rc.read.1.tlog 2B
link.read.1.tlog 5KB
link.7528-rc.write.1.tlog 2B
vc110.pdb 1.18MB
link.7528.read.1.tlog 2B
模板匹配.log 2KB
3.png 17KB
Debug
模板匹配.ilk 390KB
模板匹配.exe 67KB
模板匹配.pdb 1.53MB
共 39 条
- 1
资源评论
- _HERO_2016-11-07谢谢你的分享
- weixin_386710062019-05-31人脸检测,非人脸识别
- 不听不看不说2017-08-04比较基础、简单,浅显易懂
岳麓吹雪
- 粉丝: 1672
- 资源: 19
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功