[英]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?我已经尝试了什么?
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 errorimwrite("mask.png", mask)
Looks like this .imwrite("mask.png", mask)
保存图像看起来像这样。 Pretty small I know.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.