0

I am trying to apply the Sobel filter to my Mat which has data from an external source. The simplified code looks like the following:

// Create Mats
cv::Mat src = cv::Mat( cv::Size( ACQ_WIDTH , ACQ_HEIGHT ) , CV_8U );
cv::Mat dx;

// Point Mat to buffer data
src.data = (uchar*)(bufObject->address);

// Calculate x-gradient
cv::Sobel( src , dx , CV_16U , 1 , 0 , CV_SCHARR , 1 , 0 , BORDER_REPLICATE ) ;

But when I apply it, it gives be a ROI assertion error:

OpenCV Error: Assertion failed (roi.x >= 0 && roi.y >= 0 && roi.width >= 0 && roi.height >= 0 && roi.x + roi.width <= wholeSize.width && roi.y + roi.height <= wholeSize.height) in start, file /home/nvidia/Desktop/opencv-3.3.1/modules/imgproc/src/filter.cpp, line 173
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/nvidia/Desktop/opencv-3.3.1/modules/imgproc/src/filter.cpp:173: error: (-215) roi.x >= 0 && roi.y >= 0 && roi.width >= 0 && roi.height >= 0 && roi.x + roi.width <= wholeSize.width && roi.y + roi.height <= wholeSize.height in function start

Very strangely (at least in my opinion), when I clone the same matrix and I apply the filter to the clone, everything's fine!

// Create Mats
cv::Mat src = cv::Mat( cv::Size( ACQ_WIDTH , ACQ_HEIGHT ) , CV_8U );
cv::Mat dx;

// Point Mat to buffer data
src.data = (uchar*)(bufObject->address);

// Create clone
cv::Mat cl = src.clone() ;

// Calculate x-gradient
cv::Sobel( cl , dx , CV_16U , 1 , 0 , CV_SCHARR , 1 , 0 , BORDER_REPLICATE ) ;

The data source is ok: both imshow(src) and imshow (fm) correctly show the buffer, and as mentioned before, if using the clone instead of the original image everything works.

Since this is a time-sensitive application and this is a video-frame loop, I don't want to clone the source at every step. What am I missing?

UPDATE:

  • Both source and clone Mat give TRUE if checked with isContinuous() function.

  • total(), elemSize(), cols, rows, dims and flags give the same results for both source and clone

Giack
  • 1
  • 2
  • when you clone an image it's the same image except that the clone is supposed to be a continuous array even if the original was not. It's possible that the bufObject you're pointing at has a weird structure in memory that cv::Sobel doesn't like, but it's fine with the clone. – Ian Chu Feb 10 '21 at 16:25
  • Hello Ian, thanks for your answer. I edited the request, since I also thought about that, but a check with isContinuous() function gives TRUE for both the source mat and the clone. – Giack Feb 10 '21 at 16:35

0 Answers0