2013-08-22 165 views
0

我是OpenCV的新手。我正在做一个应用程序,告诉用户给定的图像已经给出了模板或不。我用这个代码。它完美匹配。但它不检测给定的图像是否没有匹配的模板。当我读到它时,它会达到最高值并向我显示一个错误结果(显示图像中的其他位置)。我读了minVal和maxVal。但即使它做了什么,我仍然无法理解。因为我调试了代码。每次minVal为0.0,maxVal为255.0(偶数图像包含模板或不是,我的意思是每次。)。我被困在这里。请帮助我只显示正确的结果。如何获得完全匹配在OpenCV中使用模板匹配(在Android中)?

 Utils.bitmapToMat(bmp2, mFind); 
     Utils.bitmapToMat(bmp1, Input); 
     Utils.bitmapToMat(bmp3, mResult9u); 

     Imgproc.matchTemplate(mFind, Input, mResult, matcher); 

     Core.normalize(mResult, mResult8u, 0, 255, Core.NORM_MINMAX, CvType.CV_8U); 

     Point matchLoc; 

     MinMaxLocResult minMaxLoc = Core.minMaxLoc(mResult8u); 

     /// For SQDIFF and SQDIFF_NORMED, the best matches are lower values. For all the other methods, the higher the better 
     if (matcher == Imgproc.TM_SQDIFF || matcher == Imgproc.TM_SQDIFF_NORMED) 
     { 
      matchLoc = minMaxLoc.minLoc; 
     } 
     else 
     { 
      matchLoc = minMaxLoc.maxLoc; 
     } 
     float thresholdMatchForMin = 0.02f; 
     float thresholdMatchForMax = 0.08f; 
     //    minMaxLoc.minVal < thresholdMatchForMin || 
     if (minMaxLoc.maxVal > thresholdMatchForMax) 
     { 
      /// Show me what you got 
      Core.rectangle(mResult9u, matchLoc, 
       new Point(matchLoc.x + Input.cols(), matchLoc.y + Input.rows()), new Scalar(0, 255, 255, 0)); 
      Utils.matToBitmap(mFind, bmp2); 
      Utils.matToBitmap(mResult9u, bmp3); 
      Paint paint = new Paint(); 
      paint.setColor(Color.RED); 
      Canvas canvas = new Canvas(bmp2); 
      canvas.drawRect(new Rect((int) matchLoc.x, (int) matchLoc.y, (int) (matchLoc.x + Input.cols()), 
       (int) (matchLoc.y + Input.rows())), paint); 
     } 

回答

0

线:

Core.normalize(mResult, mResult8u, 0, 255, Core.NORM_MINMAX, CvType.CV_8U); 

规格化的结果。它使任何范围等于0-255。 如果你想要真正的相关值使用非标准化的mResult。

+0

谢谢。你的意思是传递mResult到Core.minMaxLoc()?我试过了。但是我得到了同样的结果。 – ssdehero

+0

你看看坐标matchLoc中的矩阵mResult有什么值吗? TM_SQDIFF_NORMED应在范围0-1中给出结果。 –

+0

nope。总是我得到0-255。我想我在这里错过了一些东西。但我不知道什么... – ssdehero