[英]How to get the exact match Using Template Matching in OpenCV (in Android)?
我是OpenCV的新手。 我正在做一個告訴用戶給定圖像是否給定模板的應用程序。 我用了這段代碼。 它確實完美匹配。 但是它不會檢測給定圖像是否沒有匹配的模板。 在我閱讀時,它達到了最高值,並向我顯示了錯誤的結果(在圖像中的其他地方顯示)。 我讀了有關minVal和maxVal的文章。 但是我什至不明白它的作用。 因為我調試了代碼。 每次minVal為0.0且maxVal為255.0(即使圖像包含或不包含模板,我的意思是每次)。 我被困在這里。 請幫助我僅顯示正確的結果。
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);
}
該行:
Core.normalize(mResult, mResult8u, 0, 255, Core.NORM_MINMAX, CvType.CV_8U);
標准化結果。 它使任何范圍等於0-255。 如果要使用實際的相關值,請使用非標准化的mResult。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.