OpenCV: Assertion failed " dst.data == widget->original_image->data.ptr in function 'cvImageWidgetSetImage' "

1.6k views Asked by At

IMPORTANT: I fixed the problem. Solution at the end.

What do I try to achieve? Display an image with OpenCV cv::imshow method. (imshow Documentation)

The image which is a 3x3 matrix is created like such:

Mat mask(3, 3, CV_32F, new float[9]{0, 1, 0, 1, -4, 1, 0, 1, 0});

To display the image I call imshow("mask", mask);

What is my problem? Like I mentioned in the title there is an exception thrown while trying to display the image. Complete Error Message:

terminate called after throwing an instance of 'cv::Exception' what():  
OpenCV(4.0.0-pre) /home/mrlab/Libraries/opencv_source/modules/highgui
/src/window_gtk.cpp:146: error: (-215:Assertion failed) 
dst.data == widget->original_image->data.ptr in function 'cvImageWidgetSetImage'

Link to window_gtk.cpp

What did I already try?

  1. Looking for the error on the internet. Maybe someone else already encountered the same problem. nope. nothing
  2. Changed the matrix to only contain positive floating point values (0 to 1) in case it has problems with negative input. Initialization: Mat mask(3, 3, CV_32F, new float[9]{0, 1, 0, 1, 0, 1, 0, 1, 0}); same error
  3. Calling the two methods in different locations in case there are changes made during my other code. same error
  4. Writing small OpenCV program to just run these two lines. same error
  5. Various combinations of the above mentioned ideas. same error
  6. Displaying other images I read from memory instead of creating them myself. worked perfectly fine
  7. Saving the image via imwrite("mask.png", mask) Looks like this. Pretty small I know. I scaled the values to be in range of 0 to 255 since that what png needs. works perfectly fine

Complete code around my corrupted lines:

void high_pass(){

  Mat src_f;
  // Fourier transform src_bw
  src_f = fourier(src_bw);

  // Create Laplace High Pass Kernel
  Mat mask(3, 3, CV_32F, new float[9]{0, 1, 0, 1, -4, 1, 0, 1, 0});
  // In case of using fp values (0 to 1) initialize like this:
  // Mat mask(3, 3, CV_32F, new float[9]{0, 1, 0, 1, 0, 1, 0, 1, 0});
  imshow("mask", mask);

  // Fourier transform kernel
  Mat mask_f = fourier_kernel(mask, src_f.size());

  Mat hp_filtered;
  // Apply filter
  mulSpectrums(src_f, mask_f, hp_filtered, DFT_ROWS);

  // Transform it back
  dst = fourier_inv(hp_filtered);

  // Swap quadrants after applying filter
  dst = swap_quadrants(dst);

  // Show result
  //imshow(WINDOW_NAME + "high pass", dst);
}

FYI: The last line threw the same exception which is why it is commented out. I ask the question with "mask" because it is easier.

After writing the question I had another idea.

Solution: I converted the CV_32F type matrix to a CV_8U matrix and scaled all values to be in range of 0 to 255. This solved the problem.

This is something I should have thought of first. For some reason it took me one hour to realize. Just in case someone else is encountering the same error or mental block I still post this here.

2

There are 2 answers

1
Philipp G. On BEST ANSWER

Solution: I converted the CV_32F type matrix to a CV_8U matrix and scaled all values to be in range of 0 to 255. This solved the problem.

Edit: As stated by Nikolaj Fogh it is also possible to revert to OpenCV Version 3.4.3. I did not test it myself.

0
calocedrus On

For completeness, this is how to implement Philipp's solution (using his program's variables):

cv::Mat dist_8U; // to store the scaled image with appropriate type
double Min,Max;
cv::minMaxLoc(rgb,&Min,&Max);
dist -= Min;
dist.convertTo(dist_8U,CV_8U,255.0/(Max-Min));

Then dist_8U can be shown with imshow in opencv 4.0.0. Tested on a raspberry pi.