recently I have installed Opencv following the official guide (https://docs.opencv.org/4.x/d7/d9f/tutorial_linux_install.html) and everything seemed to work fine until I tried to use some more complex functions. For example, the following code applies a Gaussian Filter on a simple image, but when I try to execute it I get "Segmentation fault (core dumped)".
This is the code:
#include <opencv2/opencv.hpp>
#include <iostream>
int main(int argc, char** argv) {
if(argc < 2) {
std::cout << "Error: insert the image name! \n";
return 0;
}
cv::Mat img = cv::imread(argv[1]);
if(img.data == NULL) {
std::cout << "Error: wrong name! \n";
return 0;
}
cv::Mat img_grayscale(img.rows, img.cols, CV_8U);
cv::cvtColor(img, img_grayscale, cv::COLOR_BGR2GRAY);
cv::Mat img_gaussian(img.rows, img.cols, CV_8U);
cv::GaussianBlur(img_grayscale, img_gaussian, cv::Size(5, 5), 0, 0, cv::BORDER_DEFAULT);
cv::imshow("image gaussian filter", img_grayscale);
cv::waitKey(0);
}
and this is the compile line I run in the terminal:
g++ lab02_task4.cpp -o lab02_task4 -I/usr/local/include/opencv4 -lopencv_highgui -lopencv_core -lopencv_imgcodecs -lopencv_imgproc
I explored a bit and I found out that the function GaussianBlur()
is the cause. Can you help me understanding what's the problem?