1

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.

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Bilal
  • 3,191
  • 4
  • 21
  • 49
  • 1
    Are you sure you're allowed to use the same image for the source *and* destination arguments to `cv:: GaussianBlur`? – G.M. Mar 12 '23 at 08:21
  • @G.M. I have tried to use another one as an output `g_filtered_img` but that didn't solve the problem! – Bilal Mar 12 '23 at 08:23
  • 1
    Can you show the full code? Where does the image come from? What else are you doing? What is type2str? – Micka Mar 12 '23 at 11:25
  • Can you add a std::cout << "After Gaussian Filter" << std::endl;? – Micka Mar 12 '23 at 11:26
  • Which values have dilation_type and dial_k? – Micka Mar 12 '23 at 11:28
  • 1
    Can you try to use an image with even size in both dimensions? E.g. 372x442 instead of 371x442? – Micka Mar 12 '23 at 11:29
  • 1
    [mre] please. I don't see you defining `filtered_img` anywhere – Christoph Rackwitz Mar 12 '23 at 12:20
  • 1
    @Micka the problem turns to be that Multiple `OpenCV` versions are installed on my machine `warning: libopencv_imgproc.so.407, needed by /usr/local/lib/libopencv_features2d.so.4.7.0, may conflict with libopencv_imgproc.so.4.2` – Bilal Mar 12 '23 at 12:50
  • 1
    @ChristophRackwitz the problem is not related to the code itself, the problem is CMake is using an older version of `OpenCV` due to multiple versions are installed. – Bilal Mar 12 '23 at 12:52
  • 1
    I couldn't have guessed that (without seeing that warning in the output). good to know that presents as a segfault. – Christoph Rackwitz Mar 12 '23 at 21:05
  • 1
    @ChristophRackwitz removing all `OpenCV` versions and recompiling from source solved the problem. – Bilal Mar 12 '23 at 21:13

1 Answers1

0

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.
Bilal
  • 3,191
  • 4
  • 21
  • 49