2013-08-22 82 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