benjamin button
benjamin button

Reputation: 185

OpenCV c++ Assertion

I've been struggling with some assertion in opencv project.

First I'm converting Mat objects to be sure about their types:

gray_im.convertTo(gray_im, CV_8U);
diff_im.convertTo(diff_im, CV_8U);

Then I do a subtraction. This line is where I got the assertion:

diff_im = gray_im - prev_im;

And here is the assertion:

OpenCV Error: Bad argument (When the input arrays in add/subtract/multiply/divide functions have different types, the output array type must be explicitly specified) in arithm_op, file /build/buildd/opencv-2.4.8+dfsg1/modules/core/src/arithm.cpp, line 1313

When I print info about images I'm using in subtraction;

diff_im. type: 5 rows: 600 cols 800 
gray_im. type: 5 rows: 600 cols 800 
prev_im. type: 0 rows: 600 cols 800 

I think that I'm explicitly specifying output array (and if I'm correct, here diff_im is output array, right?) by converting it to CV_8U. Also when I print the type information of diff_im in runtime, it says it is "5" which means that I have explicitly specified the type of "diff_im".

Am I wrong here? Any suggestions?

OpenCV version: 2.4.8 Thanks in advance.

Upvotes: 1

Views: 1095

Answers (1)

Kornel
Kornel

Reputation: 5354

gray_im has type 5 and prev_im has type 0. You may forgot to initialize prev_im correctly, i.e.:

prev_im = cv::Mat::zeros(gray_im.size(), gray_im.type());

Update #1:

Well, dst = src1 - src2 is equivalent to subtract(dst, src1, dst). The parameter dst is OutputArray and used for output function parameter only. The type of dst cannot be defined by operator- only subtract() provides possibility for that by its parameter dtype, see here.

If dtype is not given and both of the input parameters are arrays then src1.type() must be equal to src2.type(), see here.

So you should overwrite operator- as below:

cv::Mat src1(600, 800, CV_8U);
cv::Mat src2(600, 800, CV_32F);
cv::Mat dst;

//dst = src1 - src2; // --> this will give the same assert
cv::subtract(src1, src2, dst, cv::noArray(), CV_32F); // type of dst is CV_32F

Upvotes: 1

Related Questions