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

asked 2016-04-05 08:33:39 -0600

nadia gravatar image

updated 2016-04-05 08:35:05 -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 reopen merge delete

Closed for the following reason duplicate question by berak
close date 2016-04-05 08:54:17.694715

Comments

@nadia, please rather edit your previous question , than posting duplicates.

berak gravatar imageberak ( 2016-04-05 08:55:08 -0600 )edit

I edit it :) Can you help me please

nadia gravatar imagenadia ( 2016-04-05 09:02:44 -0600 )edit