OpenCV Template Matching Drawing Rectangle Around Match

折月煮酒 提交于 2019-12-08 01:56:59

问题


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);

回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!