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 {
matchLoc = mmr.maxLoc;
maxmaxvalue = mmr.minVal; // test
}
// / Show me what you got
Core.rectangle(img, 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(outFile, img);
System.out.println("MinVal "+ minminvalue);
System.out.println("MaxVal "+ maxmaxvalue);
System.out.println("xVal "+ matchLoc.x);
System.out.println("yVal "+ matchLoc.y);
}
}
TemplateMatching.java:
import org.opencv.imgproc.Imgproc;
public class TemplateMatching {
public static void main(String[] args) {
System.loadLibrary("opencv_java249");
new MatchingDemo().run("rabbit.jpg", "rab_head.png", "rab_out.jpg", Imgproc.TM_SQDIFF);
}
}
Output:
Running Template Matching Writing rab_out.jpg MinVal 4.547473508864641E-13 MaxVal 7.0 xVal 233.0 yVal 72.0
I assume MinVal is the threshold value I'm looking for? rabbit.jpg rab_head.png rab_out.jpg
These are my files and the output.