OpenCV Template Matching Drawing Rectangle Around Match
Asked Answered
A

1

2

I want to use template matching, i am utilizing a code that i found that does what i want where it keeps it in bitmap and get a return of bitmap, the problem is im not entirely sure how i can get to drawing in the rectangles. I am using only java, no native while creating an app for android. With the use of openCV which i am new at. I will get multiple matches so i would like to get drawn rectangles around those point and also be able to obtain a value for the locations of these matches.

mFind=new Mat(256, 192, CvType.CV_8UC4); 
Input = new Mat(256, 192, CvType.CV_8UC4); 

Mat mResult8u = new Mat(256, 192, CvType.CV_8UC4); 

mResult = new Mat(217, 153, CvType.CV_8UC4); 

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


Imgproc.matchTemplate(mFind, Input, mResult, Imgproc.TM_SQDIFF) ;
bmp3= Bitmap.createBitmap(mResult.cols(),  mResult.rows(),Bitmap.Config.ARGB_8888);
Core.normalize(mResult, mResult8u, 0, 255, Core.NORM_MINMAX, CvType.CV_8U);
Utils.matToBitmap(mResult8u, bmp3);
iv2.setImageBitmap(bmp3);
Autolysis answered 3/4, 2013 at 21:41 Comment(0)
T
0

Find the match in your mResult, paint the rect on the Input using Core.rectangle and write this into file.

// / Localizing the best match with minMaxLoc
MinMaxLocResult mmr = Core.minMaxLoc(mResult);

Point matchLoc;
if (match_method == Imgproc.TM_SQDIFF || match_method == Imgproc.TM_SQDIFF_NORMED) {
    matchLoc = mmr.minLoc;
} else {
    matchLoc = mmr.maxLoc;
}

// / Show me what you got
Core.rectangle(Input, matchLoc, new Point(matchLoc.x + templ.cols(),
        matchLoc.y + templ.rows()), new Scalar(0, 255, 0));

// Save the visualized detection.
System.out.println("Writing "+ outFile);
Highgui.imwrite(outFile, img);
Traweek answered 7/7, 2013 at 22:2 Comment(1)
Thanks for posting this (albeit 3 years ago :)). When using your approach, the rectangle drawn is much larger than the template image. How can one go about ensuring the correct rectangle boundary is drawn? Thanks!Halter

© 2022 - 2024 — McMap. All rights reserved.