我正在尝试使用javaCV进行匹配模板。问题是,如果我使用cvSetImageROI
设置区域然后输出不正确,它会在结果输出区域中的任何位置显示矩形,但如果未使用cvSetImageROI
则结果是正确的,它显示所需的输出。
以下是代码
public static void main(String c[]) {
IplImage src = cvLoadImage("D:\\opencvtest\\1.jpg", 0);
IplImage tmp = cvLoadImage("D:\\opencvtest\\close.png", 0);
IplImage result = cvCreateImage(cvSize(src.width() -tmp.width()+1,src.height()-tmp.height() + 1),IPL_DEPTH_32F,1);
cvZero(result);
// cvSetImageROI(src, new CvRect(370, 175, 624, 398));
//cvSetImageROI(result, new CvRect(370, 175, 624 - tmp.width() + 1, 398 - tmp.height() + 1));
cvMatchTemplate(src, tmp, result, CV_TM_CCORR_NORMED);
double[] min_val = new double[2];
double[] max_val = new double[2];
CvPoint minLoc = new CvPoint();
CvPoint maxLoc = new CvPoint();
cvMinMaxLoc(result, min_val, max_val, minLoc, maxLoc,null);
CvPoint point = new CvPoint();
point.x(maxLoc.x() + tmp.width());
point.y(maxLoc.y() + tmp.height());
cvRectangle(src, maxLoc, point, CvScalar.GREEN, 2, 8, 0);
cvShowImage("Lena Image", src);
cvWaitKey(0);
cvReleaseImage(src);
cvReleaseImage(tmp);
cvReleaseImage(result);
}