Ask Your Question
0

matching one template with multiple object with opencv and java for android

asked 2016-04-04 11:34:55 -0600

nadia gravatar image

updated 2016-04-05 09:01:52 -0600

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;

}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-05-15 05:33:00 -0600

Pranav Buradkar gravatar image

The application seems to be crashing because of the infinite loop. As you are looking for new match by Core.minMaxLoc(result) in same result matrix its always returning the first occurrence.

So whenever you found a match you need to remove that match from the result and look for the next match. I removed the match by using filled rectangleCV_FILLED or (-1) in result matrix.

while(true)
{
    mmr = Core.minMaxLoc(result);
    matchLoc = mmr.maxLoc;
    if(mmr.maxVal >=0.9)
    {
        Core.rectangle(img, matchLoc, 
            new Point(matchLoc.x + templ.cols(),matchLoc.y + templ.rows()), 
            new    Scalar(0,255,0));
        Core.rectangle(result, matchLoc, 
            new Point(matchLoc.x + templ.cols(),matchLoc.y + templ.rows()), 
            new    Scalar(0,255,0),-1);
        //break;
    }
    else
    {
        break; //No more results within tolerance, break search
    }
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-04-04 11:34:55 -0600

Seen: 4,514 times

Last updated: May 15 '16