2

I've seen some questions here that is related to my error such as this and this and I know that I can't execute Imgproc.matchTemplate() method if the image and the template don't have the same datatype. But I'm still confused on how to know what type of Mat I'm using.

Below is my code which I adapted from example here:

for (int i = 0; i < 24; i++) {
    arrDraw[i] = getResources().getIdentifier("let" + i, "drawable", getPackageName());
}

Mat mImage = input.submat(bigRect);
for (int i = 0; i < 24; i++) {
    Mat mTemplate = Utils.loadResource(this, arrDraw[i], Highgui.CV_LOAD_IMAGE_COLOR);
    Mat mResult = new Mat(mImage.rows(), mImage.cols(), CvType.CV_32FC1);
    Imgproc.matchTemplate(mImage, mTemplate, mResult, match_method);
    Core.normalize(mResult, mResult, 0, 1, Core.NORM_MINMAX, -1, new Mat());
    ... // further process
}

So basically what I'm trying to do is take a mImage from submat of inputFrame and do match template process with 24 other pictures and decide which has the best value (either lowest or highest). Yet the error shows this.

OpenCV Error: Assertion failed ((img.depth() == CV_8U || img.depth() == CV_32F) && img.type() == templ.type()) in void cv::matchTemplate(cv::InputArray, cv::InputArray, cv::OutputArray, int), file /home/reports/ci/slave_desktop/50-SDK/opencv/modules/imgproc/src/templmatch.cpp, line 249

I tried to initialize the mImage and mTemplate first with the same type but still no luck. Any advice? Thanks before.

Community
  • 1
  • 1
Affan Widyan
  • 77
  • 1
  • 10

1 Answers1

2

The error is telling you that image and template have different types.

Assertion failed  ... img.type() == templ.type() ....

I'd be willing to bet (a small amount) that mTemplate is CV_8UC3 BGR ordered.

From your code posted, it's not possible to tell what mImage's type is though if it's extracted from a camera frame, and if you did something like :

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
     Mat inputFrame = inputFrame.rgba();
     ....
}

then it's likely to be CV_8UC4 BGRA ordered. Which is not the same type.

Also, I'm not sure what the behaviour of submat() is one a 3D or 4D input matrix, I think it's designed to operate only on 2D matrices so you may find that it returns either a 2D matrix (CV_8UC2) or some undefined weirdness.

I'd suggest that you try dumping the type() and depth() or both image and template before your matchTemplate( ... ) call.

Dave Durbin
  • 3,562
  • 23
  • 33
  • Ah yes I did the `inputFrame.rgba();` for input matrix. So how could I try dumping those things? – Affan Widyan May 12 '15 at 08:27
  • 1
    Sorry if I was unclear, by dumping, I meant 'printing out the values' so you could see for sure what the type was and what the depth was. By checking the values you can see what the error is. If you want to convert the captured image into RGB (same space as the template) you cause `Imgproc.cvtColor( inputFrame , inputFrame, Imgproc.COLOR_RGBA2RGB)` – Dave Durbin May 12 '15 at 11:48
  • Funny, I tried to print each Mat's `depth()` and `type()` and their values are the same, which are `0` and `16` respectively. Then I also tried the `toString()` function and both gives the same type which is `CV_8UC3`. But thankfully using `cvtColor` function above solves the problem. Thanks a lot. – Affan Widyan May 18 '15 at 04:40
  • Glad to hear you have it working. If you're happy with my answer, can you please accept it ? Thx – Dave Durbin May 18 '15 at 07:40