无法通过cvMatchTemplate获得结果?

时间:2012-04-17 09:04:14

标签: visual-c++ opencv

我有两个字符“U”的图像。我使用cvMatchTemplate函数比较它们的相似性。然后,我使用了cvMinMaxLoc 用于查找像素的最大值和最小值及其各自的位置。 但是当我比较它们时,它返回一个字符为“Q”的最大值,最大值和最小值相同。我不明白这里有什么问题??? 图像的大小相同。 我的代码是:

#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;
int main()
{
    char text[]={'Q','U'};
       IplImage * tmp[2];       
       tmp[0]=cvLoadImage("D:\\Standards\\u.bmp",1);
       tmp[1]=cvLoadImage("D:\\Standards\\q.bmp",1);

       IplImage* src;
IplImage* dst;
IplImage* gray;
IplImage* grayThresh;
IplImage* gray1[1];
IplImage* grayThresh1[1];
int threshold = 120, maxValue = 255;
int thresholdType = CV_THRESH_BINARY;

src = cvLoadImage("D:\\OU.bmp", 1);
gray = cvCreateImage( cvSize(src->width,src->height), IPL_DEPTH_8U, 1 );
cvCvtColor(src, gray, CV_BGR2GRAY );
grayThresh = cvCloneImage( gray );
cvThreshold(gray, grayThresh, threshold, maxValue, thresholdType);

for(int i=0;i<=1;i++){
gray1[i] = cvCreateImage( cvSize(tmp[i]->width,tmp[i]->height), IPL_DEPTH_8U, 1 );
cvCvtColor(tmp[i], gray1[i], CV_BGR2GRAY );
grayThresh1[i] = cvCloneImage( gray1[i] );
cvThreshold(gray1[i], grayThresh1[i], threshold, maxValue, thresholdType);
}
CvPoint     minloc, maxloc;
       double       minval, maxval;

       int iwidth = grayThresh1[0]->width - grayThresh->width + 1;
       int iheight =grayThresh1[0]->height - grayThresh->height + 1;
       IplImage *res=cvCreateImage( cvSize(iwidth,iheight),32,1);

       for(int i=0;i<=1;i++){
     cvMatchTemplate(grayThresh,grayThresh1[i],res,CV_TM_CCORR_NORMED);
     cvMinMaxLoc( res, &minval, &maxval, &minloc, &maxloc, 0 );
     cout<<text[i]<<" maxval="<<maxval<<" minval="<<minval<<endl;}

cvDestroyWindow( "grayThresh" );
cvDestroyWindow( "src" );
cvDestroyWindow( "dst" );
cvDestroyWindow( "grayThresh1" );
cvReleaseImage( &src );     
cvReleaseImage( &grayThresh );
cvReleaseImage( &dst );
for(int i=0;i<=1;i++)
cvReleaseImage( &grayThresh1[i] );
return 0;
} 

1 个答案:

答案 0 :(得分:0)

您需要更改:

IplImage* gray1[1];
IplImage* grayThresh1[1];

为:

IplImage* gray1[2];
IplImage* grayThresh1[2];