Quantcast
Channel: OpenCV Q&A Forum - RSS feed
Viewing all articles
Browse latest Browse all 1117

how to find all the objects in the picture with one template using opencv and java for android

$
0
0
i tried to do a code that find all objects using a template .The logCat don't give me any errors but the application stopped this is the code .Please help me public Mat matchTemplate(String inFile, String templateFile,String outFile, int match_method) { Log.i(TAG, "Running Template Matching"); Mat img = Imgcodecs.imread(inFile); Mat templ = Imgcodecs.imread(templateFile); // / Create the result matrix int result_cols = img.cols() - templ.cols() + 1; int result_rows = img.rows() - templ.rows() + 1; Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1); Log.i(TAG,"error debug: "+result.empty()); // / Do the Matching Normalize and Perform the template matching operation Imgproc.matchTemplate(img, templ, result, match_method); // Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat()); Imgproc.threshold(result, result,0.8,1,Imgproc.THRESH_TOZERO); // / Localizing the best match with minMaxLoc. We localize the minimum and maximum values in the result matrix R by using minMaxLoc. Point matchLoc; Point maxLoc; Point minLoc; MinMaxLocResult mmr; boolean iterate = true; while(true){ // / Localizing the best match with minMaxLoc mmr = Core.minMaxLoc(result); matchLoc = mmr.maxLoc; if(mmr.maxVal >=0.9) { // iterate = false; // / Show me what you got Imgproc.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(),matchLoc.y + templ.rows()), new Scalar(0,0,0)); } else break; //No more results within tolerance, break search } // Save the visualized detection. Log.i(TAG, "Writing: " + outFile); Imgcodecs.imwrite(outFile, img); return img; }

Viewing all articles
Browse latest Browse all 1117

Trending Articles