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

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

Segmentation Fault in some OpenCV Functions

问题

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

#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

// 一些代码用于:
// 读取图像,
// 将其转换为灰度并对其进行滤波(中值滤波)

// type2str: https://stackoverflow.com/a/17820615
std::string ty = type2str(filtered_img.type());
printf("Matrix: %s %dx%d \n", ty.c_str(),
    filtered_img.cols, filtered_img.rows);

// https://stackoverflow.com/a/19488679
try
{
    std::cout << "Before Gaussian Filter" << std::endl;
    cv::GaussianBlur(filtered_img, filtered_img,
        cv::Size(3, 3), 0);
}
catch (cv::Exception& e)
{
    const char* err_msg = e.what();
    std::cout << "exception caught: "
        << err_msg << std::endl;
}
// 与`getStructuringElement`相同的问题
try
{
    cv::Mat dil_kernel = cv::getStructuringElement(dilation_type,
        cv::Size(2 * dial_k + 1, 2 * dial_k + 1),
        cv::Point(dial_k, dial_k));
}
catch (cv::Exception& e)
{
    const char* err_msg = e.what();
    std::cout << "exception caught: " << err_msg << std::endl;
}

输出:

Matrix: 8UC1 371x442 
Before Gaussian Filter
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++:

#include &lt;opencv2/imgcodecs.hpp&gt;
#include &lt;opencv2/imgproc.hpp&gt;
#include &lt;opencv2/highgui/highgui.hpp&gt;

// some code to:
// read the image, 
// convert it to grayscale and filter it (median filter)

// type2str: https://stackoverflow.com/a/17820615
std::string ty =  type2str( filtered_img.type() );
printf(&quot;Matrix: %s %dx%d \n&quot;, ty.c_str(),
 filtered_img.cols, filtered_img.rows );

// https://stackoverflow.com/a/19488679
try
	{	
		std::cout &lt;&lt; &quot;Before Gaussian Filter&quot; &lt;&lt; std::endl;
		cv::GaussianBlur(filtered_img, filtered_img,
                          cv::Size(3, 3), 0);
	}
	catch( cv::Exception&amp; e )
	{
		const char* err_msg = e.what();
		std::cout &lt;&lt; &quot;exception caught: &quot; 
        &lt;&lt; err_msg &lt;&lt; std::endl;
	}
// same issue with `getStructuringElement`
try
	{
		cv::Mat dil_kernel = cv::getStructuringElement( dilation_type,
                       cv::Size( 2*dial_k + 1, 2*dial_k+1 ),
                       cv::Point( dial_k, dial_k ) );
	}
	catch( cv::Exception&amp; e )
	{
		const char* err_msg = e.what();
		std::cout &lt;&lt; &quot;exception caught: &quot; &lt;&lt; err_msg &lt;&lt; std::endl;
	}

output:

Matrix: 8UC1 371x442 
Before Gaussian Filter
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:

确定