OpenCV - Assign Operator “Not Working”?

I have one function for calculating the convolution (to check the settings for filter2D ), I think that the body of the function is not important, so here is just the header and end:

 template<typename T> cv::Mat conv(const cv::Mat &input, const cv::Mat &kernel) { cv::Mat output(input); // or should I rather use output( input.rows, input.cols, input.depth())? ... return output; } cv::Mat result = conv( input, kernel); 

At this moment, I have completely useless results in the result (this is not even random data, they have a strange pattern that repeats every time I run the function).

When I rewrite a function:

 template<typename T> void conv(const cv::Mat &input, cv::Mat &output, const cv::Mat &kernel) { ... } cv::Mat result(input); conv( input, result, kernel); 

Everything works fine, and the results matrix contains exactly what you need.

So my question is : what is wrong with the first approach? Am I doing something wrong? Why doesn't the operator / return from function work?

* Note: OpenCv version: extra / opencv 2.3.1_a-3 (archlinux package) *

Something similar happened to me when I downloaded external data from opencv storage and the data was lost until I used data( loaded.clone())

+4
source share
1 answer

Well, it seems that filter2d , or whatever you do, does not work "in place", that is, when the input and output are the same. With your first line in the function,

 cv::Mat output(input); // or should I rather use output( input.rows, input.cols, input.depth())? 

you make an output point with the same data as the input! This is not a clone, this is another link!

What you want to do is written in your comment. Another option may be (depending on your code) so that the release is completely uninitialized, since usually C ++ OpenCV functions initialize their output matrices for you if they are empty.

Note that your conv() , even when providing the correct results , will always destroy your input matrix in this way. OpenCV does not consider const in the internal data metadata. Yes, this is a bad design.

+1
source

Source: https://habr.com/ru/post/1394493/


All Articles