英文:
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 <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
// 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("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;
}
// 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& e )
{
const char* err_msg = e.what();
std::cout << "exception caught: " << err_msg << std::endl;
}
output:
Matrix: 8UC1 371x442
Before Gaussian Filter
Segmentation fault (core dumped)
I have seen the image cv::imshow('img', 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.2
的OpenCV
库和头文件,因此即使我在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
fromCMake
in addition to the path to he header files (manually), - or to remove the
OpenCV 4.2
installed by default with ROS.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论