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);
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);
来源:https://stackoverflow.com/questions/15798691/opencv-template-matching-drawing-rectangle-around-match