TheWaveLad
TheWaveLad

Reputation: 1026

Initializing cv::Mat with data doesn't work

Following this example I tried to initialize an openCV Mat with the following values:

cv::Mat image = (cv::Mat_<int>(3,3) << 0, 255, 0, 0, 255, 0, 0, 255, 0);

However, my IDE complains with

Binary operator '<<' can't be applied to the expressions of type 'cv::Mat_<int>' and 'int'

and when compiling I get

OpenCV Error: The function/feature is not implemented (Unsupported combination of source format (=4), and buffer format (=5)) in getLinearRowFilter, file /build/buildd/opencv-2.4.8+dfsg1/modules/imgproc/src/filter.cpp, line 2857
terminate called after throwing an instance of 'cv::Exception'
  what():  /build/buildd/opencv-2.4.8+dfsg1/modules/imgproc/src/filter.cpp:2857: error: (-213) Unsupported combination of source format (=4), and buffer format (=5) in function getLinearRowFilter

I used openCV for Python for quite a while now, but I'm completely lost using it with C++.

Upvotes: 0

Views: 1093

Answers (1)

Berriel
Berriel

Reputation: 13661

You are probably trying to apply a function (or a filter) that uses getLinearRowFilter() (such as Sobel and others) and you are using a combination of types (input and output) that is not allowed.

You can check the allowed combinations here, where sdepth is the depth of the source (input) and ddepth is the depth of the destination (output):

// these are allowed, otherwise the error will be thrown
if( sdepth == CV_8U && ddepth == CV_32S )
if( sdepth == CV_8U && ddepth == CV_32F )
if( sdepth == CV_8U && ddepth == CV_64F )
if( sdepth == CV_16U && ddepth == CV_32F )
if( sdepth == CV_16U && ddepth == CV_64F )
if( sdepth == CV_16S && ddepth == CV_32F )
if( sdepth == CV_16S && ddepth == CV_64F )
if( sdepth == CV_32F && ddepth == CV_32F )
if( sdepth == CV_32F && ddepth == CV_64F )
if( sdepth == CV_64F && ddepth == CV_64F )

Based on the error, you are probably using a CV_32S (=4) input and a CV_32F (=5) output. Basically, you cannot use CV_32S (a Mat_<int>) as input in function that uses getLinearRowFilter().

To solve this, you can convert the input before using it. For example:

cv::Mat1i image = ...; // suppose this is your input (CV_32S == Mat_<int>)
cv::Mat1f output = ...; // suppose this is your output (CV_32F == Mat_<float>)

image.convertTo(image, CV_32F); // add this line right before the call
call_to_filter(image, output, ...); // your function (or filter) call
// e.g.: cv::Sobel(image, output, ...) will throw this error without convertion

Note: the information is inaccurate because not all relevant code is in the question.

Upvotes: 1

Related Questions