0

我有两个字符“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;
} 
4

1 回答 1

0

你需要改变:

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

至:

IplImage* gray1[2];
IplImage* grayThresh1[2];
于 2012-04-17T20:18:52.203 回答