Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

Can someone help me to match mutiple objects with one template and this is the code (matching one template with one object )

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


    // / Do the Matching and Normalize
    Imgproc.matchTemplate(img, templ, result, match_method);
    Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());

    // / Localizing the best match with minMaxLoc
    MinMaxLocResult mmr = Core.minMaxLoc(result);

    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
    Imgproc.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(),matchLoc.y + templ.rows()), new Scalar(255, 255,255));

    // Save the visualized detection.
    Log.i(TAG, "Writing: " + outFile);
    Imgcodecs.imwrite(outFile, img);
    return img;

}

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

Can someone help i tried to do a code that find all objects using a template .The logCat don't give me to match mutiple objects with one template and any errors but the application stopped this is the code (matching one template with one object ).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 Normalize
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
    MinMaxLocResult mmr = Core.minMaxLoc(result);

    Point matchLoc;
    if (match_method == Imgproc.TM_SQDIFF
            || match_method == Imgproc.TM_SQDIFF_NORMED)  matchLoc = mmr.maxLoc;


        if(mmr.maxVal >=0.9)
     {
        matchLoc = mmr.minLoc;
    } else {
        matchLoc = mmr.maxLoc;
    }

//  iterate = false;




      // / Show me what you got
     Imgproc.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(),matchLoc.y + templ.rows()), new Scalar(255, 255,255));

       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;

}