在一些OpenCV函数中发生分段错误。

huangapple go评论168阅读模式
英文:

Segmentation Fault in some OpenCV Functions

问题

在Ubuntu 20.04中,我使用从源代码构建的OpenCV 4.7.0,在调用C++中的高斯模糊或getStructuringElement时出现分段错误Segmentation fault (core dumped)

  1. #include <opencv2/imgcodecs.hpp>
  2. #include <opencv2/imgproc.hpp>
  3. #include <opencv2/highgui/highgui.hpp>
  4. // 一些代码用于:
  5. // 读取图像,
  6. // 将其转换为灰度并对其进行滤波(中值滤波)
  7. // type2str: https://stackoverflow.com/a/17820615
  8. std::string ty = type2str(filtered_img.type());
  9. printf("Matrix: %s %dx%d \n", ty.c_str(),
  10. filtered_img.cols, filtered_img.rows);
  11. // https://stackoverflow.com/a/19488679
  12. try
  13. {
  14. std::cout << "Before Gaussian Filter" << std::endl;
  15. cv::GaussianBlur(filtered_img, filtered_img,
  16. cv::Size(3, 3), 0);
  17. }
  18. catch (cv::Exception& e)
  19. {
  20. const char* err_msg = e.what();
  21. std::cout << "exception caught: "
  22. << err_msg << std::endl;
  23. }
  24. // 与`getStructuringElement`相同的问题
  25. try
  26. {
  27. cv::Mat dil_kernel = cv::getStructuringElement(dilation_type,
  28. cv::Size(2 * dial_k + 1, 2 * dial_k + 1),
  29. cv::Point(dial_k, dial_k));
  30. }
  31. catch (cv::Exception& e)
  32. {
  33. const char* err_msg = e.what();
  34. std::cout << "exception caught: " << err_msg << std::endl;
  35. }

输出:

  1. Matrix: 8UC1 371x442
  2. Before Gaussian Filter
  3. Segmentation fault (core dumped)

在将图像传递给高斯滤波器之前,我已经看过图像cv::imshow('img', filtered_img),似乎没问题,并且我已经将此图像传递给中值滤波器并且正常工作,您能告诉我如何解决此问题吗?谢谢。

英文:

I'm using OpenCV 4.7.0 built from source in Ubuntu 20.04 and I'm getting a segmentation fault Segmentation fault (core dumped) while calling a Gaussian Blur or getStructuringElement in c++:

  1. #include &lt;opencv2/imgcodecs.hpp&gt;
  2. #include &lt;opencv2/imgproc.hpp&gt;
  3. #include &lt;opencv2/highgui/highgui.hpp&gt;
  4. // some code to:
  5. // read the image,
  6. // convert it to grayscale and filter it (median filter)
  7. // type2str: https://stackoverflow.com/a/17820615
  8. std::string ty = type2str( filtered_img.type() );
  9. printf(&quot;Matrix: %s %dx%d \n&quot;, ty.c_str(),
  10. filtered_img.cols, filtered_img.rows );
  11. // https://stackoverflow.com/a/19488679
  12. try
  13. {
  14. std::cout &lt;&lt; &quot;Before Gaussian Filter&quot; &lt;&lt; std::endl;
  15. cv::GaussianBlur(filtered_img, filtered_img,
  16. cv::Size(3, 3), 0);
  17. }
  18. catch( cv::Exception&amp; e )
  19. {
  20. const char* err_msg = e.what();
  21. std::cout &lt;&lt; &quot;exception caught: &quot;
  22. &lt;&lt; err_msg &lt;&lt; std::endl;
  23. }
  24. // same issue with `getStructuringElement`
  25. try
  26. {
  27. cv::Mat dil_kernel = cv::getStructuringElement( dilation_type,
  28. cv::Size( 2*dial_k + 1, 2*dial_k+1 ),
  29. cv::Point( dial_k, dial_k ) );
  30. }
  31. catch( cv::Exception&amp; e )
  32. {
  33. const char* err_msg = e.what();
  34. std::cout &lt;&lt; &quot;exception caught: &quot; &lt;&lt; err_msg &lt;&lt; std::endl;
  35. }

output:

  1. Matrix: 8UC1 371x442
  2. Before Gaussian Filter
  3. Segmentation fault (core dumped)

I have seen the image cv::imshow(&#39;img&#39;, filtered_img) before passing it to the Gaussian Filter and it seems to be OK, and I have passed this image to a median filter and worked properly, can you please tell me how can I solve this issue please? thanks in advance.

答案1

得分: 0

安装ROS意味着安装旧版本4.2OpenCV库和头文件,因此即使我在CMakeLists.txt中指定所需版本为4.7,当我打印所使用的版本(来自头文件)时,我看到的是4.2,这导致所使用的库为4.7而头文件为4.2不匹配。

解决方案:

  • CMake中指定所需版本的OpenCV,并手动添加头文件的路径,
  • 或者删除ROS默认安装的OpenCV 4.2
英文:

Installing ROS implies installing older version 4.2 of OpenCV library and headers, so even if I specify in CMakeLists.txt that the required version is 4.7 when I print the used version (comes from the header file) I see 4.2 which made a mismatch between the used library 4.7 and the headers 4.2.

The Solution:

  • to specify the required version of OpenCV from CMake in addition to the path to he header files (manually),
  • or to remove the OpenCV 4.2 installed by default with ROS.

huangapple
  • 本文由 发表于 2023年3月12日 16:12:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711820.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定