Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

how to Reduce false detection of template matching

how to Reduce false detection of template matching ??

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

    try {
        image = inputFrame.rgba();
        templ = Utils.loadResource(MainActivity.this, TEMPLATE, Highgui.CV_LOAD_IMAGE_COLOR);
    } catch (Exception e) {
        Log.d("eeeeeeeee", e.getMessage());
    }
    Mat template = new Mat(templ.size(), CvType.CV_32F);
    Imgproc.cvtColor(templ, template, Imgproc.COLOR_BGR2RGBA);
    int match_method = Imgproc.TM_CCOEFF;

    // Create the result matrix
    int result_cols = image.cols() - template.cols() + 1;
    int result_rows = image.rows() - template.rows() + 1;
    Mat result = new Mat(result_rows, result_cols, CvType.CV_32F);

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

    // Localizing the best match with minMaxLoc
    Core.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;
    }

        Point pt2 = new Point(matchLoc.x + template.cols(), matchLoc.y + template.rows());
        Scalar color = new Scalar(0, 255, 255, 0);
        Core.rectangle(image, matchLoc, pt2, color);


    return image;
}
click to hide/show revision 2
retagged

how to Reduce false detection of template matching

how to Reduce false detection of template matching ??

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

    try {
        image = inputFrame.rgba();
        templ = Utils.loadResource(MainActivity.this, TEMPLATE, Highgui.CV_LOAD_IMAGE_COLOR);
    } catch (Exception e) {
        Log.d("eeeeeeeee", e.getMessage());
    }
    Mat template = new Mat(templ.size(), CvType.CV_32F);
    Imgproc.cvtColor(templ, template, Imgproc.COLOR_BGR2RGBA);
    int match_method = Imgproc.TM_CCOEFF;

    // Create the result matrix
    int result_cols = image.cols() - template.cols() + 1;
    int result_rows = image.rows() - template.rows() + 1;
    Mat result = new Mat(result_rows, result_cols, CvType.CV_32F);

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

    // Localizing the best match with minMaxLoc
    Core.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;
    }

        Point pt2 = new Point(matchLoc.x + template.cols(), matchLoc.y + template.rows());
        Scalar color = new Scalar(0, 255, 255, 0);
        Core.rectangle(image, matchLoc, pt2, color);


    return image;
}