Reputation: 382
When I run the following code:
Mat i = Imgcodecs.imread(inFile);
Mat image = new Mat(i.rows(), i.cols(), CvType.CV_8U);
i.convertTo(image, CvType.CV_8U, 1.0/255);
System.out.println(image.type() + ", " + CvType.CV_8U);
Mat t = Imgcodecs.imread(templateFile);
Imgproc.Canny(t, t, 50, 200);
Mat template = new Mat(t.rows(), t.cols(), CvType.CV_8U);
t.convertTo(template, CvType.CV_8U, 1.0/255);
System.out.println(template.type() + ", " + CvType.CV_8U);
System.out.println((image.depth() == CvType.CV_8U) + ", " + (image.type() == template.type()) + ", " + image.dims());
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_32FC1);
Imgproc.matchTemplate(image, template, result, match_method);
I get the following output and runtime error on the line with matchTemplate():
16, 0
0, 0
true, false, 2
OpenCV Error: Assertion failed ((depth == CV_8U || depth == CV_32F) && type == _templ.type() && _img.dims() <= 2) in matchTemplate, file /Users/rajs/Vision/opencv/modules/imgproc/src/templmatch.cpp, line 1062
Exception in thread "AWT-EventQueue-0" CvException [org.opencv.core.CvException: cv::Exception: /Users/rajs/Vision/opencv/modules/imgproc/src/templmatch.cpp:1062: error: (-215) (depth == CV_8U || depth == CV_32F) && type == _templ.type() && _img.dims() <= 2 in function matchTemplate
As can be seen from the output, the reason why the boolean statement is failing is due to the type of the template and that of the image not matching. But, why don't they match after I call the convertTo() method?
Upvotes: 0
Views: 771
Reputation: 382
Nevermind, I figured it out. Apparently you have to call:
Imgproc.cvtColor(t, t, Imgproc.COLOR_BGR2GRAY);
before you convert.
Upvotes: 1