简体   繁体   English

OpenCV:断言失败“dst.data == widget->original_image->data.ptr in function 'cvImageWidgetSetImage'”

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

IMPORTANT: I fixed the problem.重要提示:我解决了这个问题。 Solution at the end.最后解决。

What do I try to achieve?我想达到什么目的? Display an image with OpenCV cv::imshow method.使用 OpenCV cv::imshow方法显示图像。 ( imshow Documentation ) ( imshow 文档)

The image which is a 3x3 matrix is created like such:作为 3x3 矩阵的图像创建如下:

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);为了显示图像,我调用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链接到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.将矩阵更改为仅包含正浮点值(0 到 1),以防出现负输入问题。 Initialization: Mat mask(3, 3, CV_32F, new float[9]{0, 1, 0, 1, 0, 1, 0, 1, 0});初始化: 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.编写小的 OpenCV 程序来运行这两行。 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 .通过imwrite("mask.png", mask)保存图像看起来像这样 Pretty small I know.我知道很小。 I scaled the values to be in range of 0 to 255 since that what png needs.我将值缩放到 0 到 255 的范围内,因为这是 png 需要的。 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.解决方案:我将CV_32F类型的矩阵转换为CV_8U矩阵并将所有值缩放到 0 到 255 的范围内。这解决了问题。

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.以防万一其他人遇到同样的错误或心理障碍,我仍然在这里发布。

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.解决方案:我将 CV_32F 类型的矩阵转换为 CV_8U 矩阵并将所有值缩放到 0 到 255 的范围内。这解决了问题。

Edit: As stated by Nikolaj Fogh it is also possible to revert to OpenCV Version 3.4.3.编辑:正如 Nikolaj Fogh 所说,也可以恢复到 OpenCV 版本 3.4.3。 I did not test it myself.我自己没有测试。

For completeness, this is how to implement Philipp's solution (using his program's variables):为了完整起见,这是如何实现 Philipp 的解决方案(使用他的程序变量):

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.然后dist_8U可以与所示imshow在OpenCV中4.0.0。 Tested on a raspberry pi.在树莓派上测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM