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