Ask Your Question
0

Where is the blurry gray image from template matching?

asked 2016-01-06 06:03:14 -0600

Finnish gravatar image

I have a working java code for template matching as below:

import org.opencv.core.Core;
import org.opencv.core.Core.MinMaxLocResult;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;


class MatchingDemo {
public void run(String inFile, String templateFile, String outFile, int match_method) {
    System.out.println("\nRunning Template Matching");

    Mat img = Highgui.imread(inFile);
    Mat templ = Highgui.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
    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));

    // Save the visualized detection.
    System.out.println("Writing "+ outFile);
    Highgui.imwrite("pics/colored_original.png", img);
    Highgui.imwrite("pics/newresult2.png", result);

 }
}

I want to see the gray blurry Matching Result picture as on OpenCV documentation

The code is almost identical with template matching documentation here

The variable should be "result" right? and "newresult2.png" but all I see is a black image and nothing else but the "colored_original.png" comes out very good, the match area is correct. What am I doing wrong or what is missing?

edit retag flag offensive close merge delete

Comments

you will have to convertTo(CV_8U) before saving, and probably aplly some scale/offset as well

berak gravatar imageberak ( 2016-01-06 06:09:23 -0600 )edit

@berak "result.convertTo(result, CvType.CV_8U);" ? Did not work... could you please give some code example?

Finnish gravatar imageFinnish ( 2016-01-06 07:06:01 -0600 )edit

@berak also I dont see convertTo() on the tutorial...

Finnish gravatar imageFinnish ( 2016-01-06 07:08:06 -0600 )edit

the tutorial uses imshow(), which can handle CV_32F images nicely, since have to write to disk, you have to convert before.

"Did not work" what did not work, and what is the error ?

berak gravatar imageberak ( 2016-01-06 07:28:50 -0600 )edit

@berak When I used "result.convertTo(result, CvType.CV_8U);" the result image was still black and not changed.

Finnish gravatar imageFinnish ( 2016-01-06 09:00:12 -0600 )edit

that's why you need scaling there, the values in the result image are probably in the [0..1] range.

try like: result.convertTo(result, CvType.CV_8U, 255);

berak gravatar imageberak ( 2016-01-06 09:05:16 -0600 )edit

@berak now it worked, can you please tell me why 255 and not another value? How can I further manipulate the result? What would be its original form?`also can you please check my final question! pretty please!

Finnish gravatar imageFinnish ( 2016-01-06 09:18:24 -0600 )edit

CV_8U images are in the [0..255] range, so you multiply your [0..1] result image with 255 to get there.

berak gravatar imageberak ( 2016-01-06 09:37:34 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-01-06 09:57:47 -0600

Finnish gravatar image

@berak Thanks a lot!

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-01-06 06:03:14 -0600

Seen: 442 times

Last updated: Jan 06 '16