Ask Your Question

Finnish's profile - activity

2019-07-09 06:17:10 -0600 received badge  Famous Question (source)
2017-11-27 01:21:41 -0600 received badge  Notable Question (source)
2017-09-07 07:43:09 -0600 received badge  Famous Question (source)
2017-05-03 13:52:26 -0600 received badge  Popular Question (source)
2017-04-25 13:31:32 -0600 received badge  Popular Question (source)
2016-02-05 09:27:45 -0600 received badge  Notable Question (source)
2016-01-09 07:25:25 -0600 commented answer Calculation of TM_CCOEFF_NORMED for template matching

@pklab Thanks it is clear but I dont see where "J = I(x,y) - AVG(I)" comes from, where does it say on the documentation or formula we have to subtract AVG(I) or AVG(U) to get U,J?

2016-01-06 09:57:47 -0600 commented question Where is the blurry gray image from template matching?

@berak Thanks a lot!

2016-01-06 09:18:24 -0600 commented question Where is the blurry gray image from template matching?

@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!

2016-01-06 09:00:12 -0600 commented question Where is the blurry gray image from template matching?

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

2016-01-06 07:08:06 -0600 commented question Where is the blurry gray image from template matching?

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

2016-01-06 07:06:01 -0600 commented question Where is the blurry gray image from template matching?

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

2016-01-06 06:03:14 -0600 asked a question Where is the blurry gray image from template matching?

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?

2016-01-06 05:53:01 -0600 commented question Calculation of TM_CCOEFF_NORMED for template matching

@thdrksdfthmn I don't have a choice, it must be this way for now, I just want to understand the formula...

2016-01-06 05:39:10 -0600 asked a question Calculation of TM_CCOEFF_NORMED for template matching

This template image below gives always 1.0:

Flat template

best matching result in every source image. @pklab said here it is because the formula of TM_CCOEFF_NORMED returns 0/0 because the template image is constant color and opencv returns 1.0 to avoid match exception, but I still don't get it. I would understand better with an example, lets say template image = 3x4 pixels and source image = 10x12 pixels, could someone put the values in the formula please and show 0/0?

2015-11-24 09:08:01 -0600 received badge  Enthusiast
2015-11-23 07:17:59 -0600 commented answer Template Matching is wrong with specific Reference image

Thanks again! I am searching for a park polygon in the map or lake which are fully green and blue... Could you please add the mathematical operations again, maybe on your answer, how does the result become zero... for my better understanding.

2015-11-20 07:47:14 -0600 commented answer Template Matching is wrong with specific Reference image

I see so how can I overcome this obstacle? Changing the match method for every specific image will be very troublesome, for the specific case when I use "TM_CCORR_NORMED" the score is 0.93 which is misleading and when I use "TM_SQDIFF_NORMED" I get 0.79, which is promising because it is a bad match, but in general I would like to use "TM_CCOEFF_NORMED" for all images... Also one more question is the template image and source image converted to grayscale and then the math operations take place? Or there is no grayscale conversion?

2015-11-20 05:34:00 -0600 commented answer Template Matching is wrong with specific Reference image

@pklab thanks for updating the answer, my code I assume is correct because out of 100 images I always find the images and have a good match score, it cant be coincidence, just only this specific full green reference image, always finds at 0,0 and with score 1.0 when using TM_COEFF_NORMED, please feel free to give it a try yourself. Not only this specific green image but on full red, blue etc. colors the story is the same.

2015-11-19 05:01:55 -0600 commented answer Template Matching is wrong with specific Reference image

I made some tests and TM_COEFF_NORMED was giving the best results so I stick with it, but now you are saying TM_COEFF is Normalized Cross Correlation, so when I use TM_COEFF_NORMED, am I using Normalized Normalized Cross Correlation? Although the 2 images are very different, why does it give 1.0 the best match? and why always on first top left corner? Also if I do normalizing after matching after matching like

 Imgproc.matchTemplate(img, templ, result, match_method);
Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());

Does it become 3 times Normalized? In case of adding the last line of normalization code, I get matchscore 0.0, which satisfies me but with the last line it is always either 1 or 0... nothing else, and sometimes it gives 0 for good matches

2015-08-08 18:11:18 -0600 received badge  Popular Question (source)
2015-05-04 10:24:31 -0600 commented answer Reading Image for Template Matching for Java

@berak may I draw your attention to this question please?

2015-05-04 08:17:01 -0600 commented question Template Matching is wrong with specific Reference image

@thdrksdfthmn result is a Mat not very relevant... with the matchscore being "1.0"

2014-12-09 14:09:20 -0600 marked best answer Detecting image in another image? (Image Comparison)

I would like to find a small image lets say 30x30 in a big image say 300x300. This is done by template matching and Im programming with java, I found from stackoverflow a Java version of the cpp code for template matching. It works, it finds the template image and then highlights it in source image. On purpose I search template image in a source image which it doesn't exist, the program still highlights some area on the out file. I know that it is making the best match it can. But I need to know if the image is found or not, a boolean which says true or false. Thus I should set a threshold value, what is a good value? I read it should be between 0 and 1 but I made 3 tests with 3 different source images which included the template image and got results of : 4.54... , 0, -1.86... for MinVal, am I checking the correct value? I would be glad if you can enlighten me on this and Im open for other methods as well!

Also I made these tests always with square/rectangle images, they always worked but when I cropped a circle or custom shape from source img and then searched for it, then the match is always at a wrong place... When I use a gif file for the template I get the following error:

OpenCV Error: Assertion failed ((img.depth() == CV_8U || img.depth() == CV_32F) && img.type() == templ.type()) in cv::matchTemplate, file ..\..\..\..\opencv\modules\imgproc\src\templmatch.cpp, line 249
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: ..\..\..    \..\opencv\modules\imgproc\src\templmatch.cpp:249: error: (-215) (img.depth() == CV_8U || img.depth() == CV_32F) && img.type() == templ.type() in function cv::matchTemplate
]
at org.opencv.imgproc.Imgproc.matchTemplate_0(Native Method)
at org.opencv.imgproc.Imgproc.matchTemplate(Imgproc.java:7621)
at MatchingDemo.run(MatchingDemo.java:30)
at TemplateMatching.main(TemplateMatching.java:7)

Which file types are supported which are not?

Here is the source code Im using:

MatchingDemo.java:

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

    double minlocvalue = 7;
    double maxlocvalue = 7;

    double minminvalue = 7;
    double maxmaxvalue = 7;


    // / 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;
        minminvalue = mmr.minVal; // test 
    } else ...
(more)
2014-11-27 05:38:37 -0600 commented question [ATTENTION] Regarding topics and answers not appearing right away, but requesting moderation

Such a shame someone attacking on an opensource project, I hope you get replies back from Alexander asap, can we at least access our questions waiting for moderation?

2014-11-26 10:03:57 -0600 commented question Reading Image for Template Matching for Java

@berak I already know that and this one http://docs.opencv.org/doc/tutorials/imgproc/histograms/template_matching/template_matching.html but there are just formulas, no explanations.

2014-11-26 09:47:41 -0600 commented question Reading Image for Template Matching for Java

@berak there are 6 matching methods for Template Matching I dont know which one to choose, which one shall I prefer? According to what cases etc. Im looking for a page where explains all of them detailed, do you have any links by chance explaining the methods?

2014-11-26 08:25:48 -0600 asked a question Template Matching is wrong with specific Reference image

Until now I have always gotten reasonable results withc matching score, Im using "Imgproc.TM_CCOEFF_NORMED" method for template matching below are the results.

Reference image:

image description

Outcome 1:

image description

MinMaxLocResult mmr = Core.minMaxLoc(result);
maxMatchScore = mmr.maxVal;

Value of maxMatchScore is always 1.0

Outcome 2:

image description

MinMaxLocResult mmr = Core.minMaxLoc(result);
maxMatchScore = mmr.maxVal;

Value of maxMatchScore is always 1.0

1.0 is the best match value, which means the image is %100 identical, but it isn't and on other reference images I get other values, so the program is working but only with this specific reference image I'm getting 1.0 also no matter what in file(source image) I give, it always finds the match on the first search upper left corner. A little bit annoying, I would be glad for your suggestions and comments for improvement because TM_CCOEFF_NORMED gives always the first searched square/rectangle as best match with score 1.0, this cant be correct, on the other hand I also tried TM_CCORR_NORMED and TM_SQDIFF_NORMED they gave different match scores, this is promising but still TM_CCORR_NORMED gave good matching score which is still unexpected from my side. I would be glad if someone can explain me the difference between the matching methods or give a link to already an existing page where these methods are being discussed, on opencv docs/tutorials there are only the formulas available but no detailed explanation. In the end I would like to know when to use which match method for what kind of image to get the best results.

Here is some more code:

    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 
    Imgproc.matchTemplate(img, templ, result, match_method);
    //  Localizing the best match with minMaxLoc
    MinMaxLocResult mmr = Core.minMaxLoc(result);

If I add normalization after the matching with

     Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());

Then I become matchscore 0.0, which satisfies me and what I would expect, but this way gives only 1 or 0 nothing else in between, I would accept this if it was always giving correct results but sometimes it gives 0.0 for very good matches as well, i.e. when I cut a part from an image and try to find the cut part in the original image, then it gives 0.0, which should be the case... Also having values like 0.8734543, 0.234356, 0.450985948, helps me a lot to understand and analyze the comparisons, I have threshold of 0.9, any value above 0.9 I accept as correct, found.

2014-10-31 09:01:49 -0600 asked a question Template Matching for Round Images in Java

Hi there, I dont always have square/rectangle images, sometimes I should match round images as well. Below are 2 images for example. Ball is the template image and the second is Source where the template should be searched. I can make the background of template transparent, but this gives error, making it white decreases the match score, because as you see on the Source image there is no white around the ball. These are just 2 examples pictures. Do you have suggestions solutions?

Ball Ball Field

2014-10-30 11:25:16 -0600 commented answer Reading Image for Template Matching for Java

@berak do you have any suggestions for the png problem? Or maybe the problem is not png something else... but works when the source is jpg in DB and works when the source is png/jpg at local computer!

2014-10-30 11:17:33 -0600 commented answer Reading Image for Template Matching for Java

@berak Sorry wrote the @ wrongly and cant edit the comment now :S

2014-10-30 11:05:43 -0600 commented answer Java API : loading image from any Java InputStream

OpenCV Error: Assertion failed ((img.depth() == CV_8U || img.depth() == CV_32F) && img.type() == templ.type()) in cv::matchTemplate, file ........\opencv\modules\imgproc\src\templmatch.cpp, line 249 Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: ........\opencv\modules\imgproc\src\templmatch.cpp:249: error: (-215) (img.depth() == CV_8U || img.depth() == CV_32F) && img.type() == templ.type() in function cv::matchTemplate ] at org.opencv.imgproc.Imgproc.matchTemplate_0(Native Method) at org.opencv.imgproc.Imgproc.matchTemplate(Imgproc.java:7621) at MatchingDemo.run(MatchingDemo.java:104) On line 104 : Imgproc.matchTemplate(img, templ, result, match_method); I would be glad if you can help.

2014-10-30 11:05:09 -0600 commented answer Java API : loading image from any Java InputStream

@AlexanderPacha Today I realized it wasnt fully working. When the inFile(Source) is a jpg on Mysql Database it works but when it is a png on Database I get the error below (Not enough space). PS: On my local computer both jpg and png work :( without the inputStream stuff.

2014-10-30 11:01:12 -0600 commented answer Reading Image for Template Matching for Java

OpenCV Error: Assertion failed ((img.depth() == CV_8U || img.depth() == CV_32F) && img.type() == templ.type()) in cv::matchTemplate, file ........\opencv\modules\imgproc\src\templmatch.cpp, line 249 Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: ........\opencv\modules\imgproc\src\templmatch.cpp:249: error: (-215) (img.depth() == CV_8U || img.depth() == CV_32F) && img.type() == templ.type() in function cv::matchTemplate ] at org.opencv.imgproc.Imgproc.matchTemplate_0(Native Method) at org.opencv.imgproc.Imgproc.matchTemplate(Imgproc.java:7621) at MatchingDemo.run(MatchingDemo.java:104) On line 104 : Imgproc.matchTemplate(img, templ, result, match_method);

2014-10-30 10:59:34 -0600 commented answer Reading Image for Template Matching for Java

@break Im sorry if you got disappointed but in Stackoverflow when you copy someone else's answer and then post it as yours, you get a BIG "boO", I wouldnt like to take credits for what someone else did. What I did was just copy paste his code and then it worked, I haven't fixed or modified it. Today I realized it wasnt fully working. When the inFile(Source) is a jpg on Database it works but when it is a png on Database I get the error below (Not enough space). PS: On my local computer both jpg and png work :( without the inputStream stuff.

2014-10-24 11:23:59 -0600 answered a question Reading Image for Template Matching for Java

I found the answer here kudos to this guy, he is my hero :) Works perfectly, if you want the images in original color and not in grayscale you should change Highgui.IMREAD_GRAYSCALE with Highgui.IMREAD_UNCHANGED for more info check

2014-10-23 11:25:33 -0600 commented question Reading Image for Template Matching for Java

@berak Okay I will in 23 hours :) thanks a lot for your efforts!

2014-10-23 11:23:59 -0600 commented answer Java API : loading image from any Java InputStream

You are my hero, I was looking for this for 2 days...

2014-10-23 11:10:11 -0600 commented question Reading Image for Template Matching for Java
2014-10-23 10:45:39 -0600 commented question Reading Image for Template Matching for Java

@berak this is the new error I got,

libpng error: PNG input buffer is incomplete OpenCV Error: Assertion failed (corrsize.height <= img.rows + templ.rows - 1 && corrsize.width <= img.cols + templ.cols - 1) in cv::crossCorr, file ........\opencv\modules\imgproc\src\templmatch.cpp, line 70 Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: ........\opencv\modules\imgproc\src\templmatch.cpp:70: error: (-215) corrsize.height <= img.rows + templ.rows - 1 && corrsize.width <= img.cols + templ.cols - 1 in function cv::crossCorr ] at org.opencv.imgproc.Imgproc.matchTemplate_0(Native Method) at org.opencv.imgproc.Imgproc.matchTemplate(Imgproc.java:7621) at MatchingDemo.run(MatchingDemo.java:74) Line 74: Imgproc.matchTemplate(img, templ, reslt, matchmetod);

2014-10-23 10:19:49 -0600 commented question Reading Image for Template Matching for Java

@break what is numBytesRead ? where do I get it?