Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Template matching with mutiple templates

hi all, I'm working on the android application template matching. I get a problem to compare one image with multiple templates. is there here that can help me in resolving the issue. I would appreciate if anyone can help me. thank you

I write the following code that I use

public void matching() { Mat img = Highgui.imread("/mnt/sdcard/img_eq/img_eq.jpg", CvType.CV_8SC3); Mat templ = Highgui.imread("/mnt/sdcard/img_template/img_template.jpg", CvType.CV_8SC3);

    //Create the result matrix
    int match_method = Imgproc.TM_SQDIFF_NORMED;
    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_8SC3);

     //Do the Matching and Normalize
    Imgproc.matchTemplate(img, templ, result, match_method);
    int type = Imgproc.THRESH_TOZERO;
    Imgproc.threshold(result, result, 0.8, 1., type);
    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
    Core.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(),matchLoc.y + templ.rows()), new Scalar(255, 0, 0));

    // Save the visualized detection.
    Highgui.imwrite("/mnt/sdcard/img_result/img_result.jpg", img);

    Mat image = Highgui.imread("/mnt/sdcard/img_result/img_result.jpg");
    Mat android_image = Mat.zeros(image.cols(), image.rows(), CvType.CV_8SC3);

    Imgproc.cvtColor(image, android_image, Imgproc.COLOR_BGR2RGB);

    Bitmap bm = Bitmap.createBitmap(android_image.cols(),android_image.rows(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(android_image, bm);

    ImageView iv = (ImageView) findViewById(R.id.image);
    iv.setImageBitmap(bm);

}

Template matching with mutiple templates

hi all, I'm working on the android application template matching. I get a problem to compare one image with multiple templates. is there here that can help me in resolving the issue. I would appreciate if anyone can help me. thank you

I write the following code that I use

public void matching() { Mat img = Highgui.imread("/mnt/sdcard/img_eq/img_eq.jpg", CvType.CV_8SC3); Mat templ = Highgui.imread("/mnt/sdcard/img_template/img_template.jpg", CvType.CV_8SC3);

    Mat templ = Highgui.imread("/mnt/sdcard/img_template/img_template.jpg", CvType.CV_8SC3);

    //Create the result matrix
    int match_method = Imgproc.TM_SQDIFF_NORMED;
    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_8SC3);

     //Do the Matching and Normalize
    Imgproc.matchTemplate(img, templ, result, match_method);
    int type = Imgproc.THRESH_TOZERO;
    Imgproc.threshold(result, result, 0.8, 1., type);
    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
    Core.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(),matchLoc.y + templ.rows()), new Scalar(255, 0, 0));

    // Save the visualized detection.
    Highgui.imwrite("/mnt/sdcard/img_result/img_result.jpg", img);

    Mat image = Highgui.imread("/mnt/sdcard/img_result/img_result.jpg");
    Mat android_image = Mat.zeros(image.cols(), image.rows(), CvType.CV_8SC3);

    Imgproc.cvtColor(image, android_image, Imgproc.COLOR_BGR2RGB);

    Bitmap bm = Bitmap.createBitmap(android_image.cols(),android_image.rows(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(android_image, bm);

    ImageView iv = (ImageView) findViewById(R.id.image);
    iv.setImageBitmap(bm);

}

Template matching with mutiple templates

hi all, I'm working on the android application template matching. I get a problem to compare one image with multiple templates. is there here that can help me in resolving the issue. I would appreciate if anyone can help me. thank you

I write the following code that I use

public void matching() { Mat img = Highgui.imread("/mnt/sdcard/img_eq/img_eq.jpg", CvType.CV_8SC3);{

    Mat img = Highgui.imread("/mnt/sdcard/img_eq/img_eq.jpg", CvType.CV_8SC3);

    Mat templ = Highgui.imread("/mnt/sdcard/img_template/img_template.jpg", CvType.CV_8SC3);

    //Create the result matrix
    int match_method = Imgproc.TM_SQDIFF_NORMED;
    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_8SC3);

     //Do the Matching and Normalize
    Imgproc.matchTemplate(img, templ, result, match_method);
    int type = Imgproc.THRESH_TOZERO;
    Imgproc.threshold(result, result, 0.8, 1., type);
    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
    Core.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(),matchLoc.y + templ.rows()), new Scalar(255, 0, 0));

    // Save the visualized detection.
    Highgui.imwrite("/mnt/sdcard/img_result/img_result.jpg", img);

    Mat image = Highgui.imread("/mnt/sdcard/img_result/img_result.jpg");
    Mat android_image = Mat.zeros(image.cols(), image.rows(), CvType.CV_8SC3);

    Imgproc.cvtColor(image, android_image, Imgproc.COLOR_BGR2RGB);

    Bitmap bm = Bitmap.createBitmap(android_image.cols(),android_image.rows(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(android_image, bm);

    ImageView iv = (ImageView) findViewById(R.id.image);
    iv.setImageBitmap(bm);

}