简体   繁体   中英

Encountered Segmentation fault in simple OpenCV code

Tools

  • Platform : 64-bit Windows
  • Compiler chain: mingw with Qt
  • Make system: CMake
  • Libraries: C++ 11, OpenCV 4, Qt 5

Problem (Updated)

The following simple program segment should compile and display the generated image in OpenCV. However, it always SIGSEGVs in DEBUG mode only(Backtrace at the end). However, it works just fine in RELEASE mode.

#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;

void testOPENCV()
{
   cv::Mat output(480, 640, CV_8UC3, cv::Scalar(255,0,100));
   cv::namedWindow( "Test", cv::WINDOW_AUTOSIZE );
   cv::imshow("Test",output);
   cv::waitKey(0);
}

int main(int argc, char** argv)
{
   testOPENCV();
   return 0;
}

I have a CMake script that builds only the required OpenCV modules and links these to the dependencies. The relevant part:

build_external_project(opencv "https://github.com/opencv/opencv.git" "4.2.0" "-DCMAKE_INSTALL_PREFIX=${THIRDPARTY_INSTALLFOLDER} - DCMAKE_BUILD_TYPE=${THIRDPARTY_BUILDTYPE} -DBUILD_LIST=core,imgproc,imgcodecs,highgui")

target_link_libraries(OpenVideo ${OpenCV_LIBS})

The binary can be run with no missing dll errors. Dependency walker also indicates the same.

Here is the backtrace:

堆栈跟踪

Few things to check here.

First, where do the OpenCV library come from? Is it compiled for your CPU? Looks like it crashed in AVX instructions. Might be that CPU does not support them.

Second, not obvious at all, happened to me with .png files. Same segfault during runtime. Turned out that OpenCV was built without png support. Please check if your OpenCV built with -DWITH_JPEG=ON .

Given that OpenCV works fine in Release mode, I suggest rebuilding the Debug version of the library.

Previous answer :

There are a few potential problems with your code:

  • Using Qt is unnecessary on this example and it adds a complexity that you don't need right now. Remove it from the project and its libraries on the link instruction in the CMake script. Later, you can bring it back to see if it causes of the crash. Right now you need to pinpoint if the problem is in OpenCV or Qt.
  • An image can only be displayed on a window if cv::waitKey() is invoked;
  • The directory separator on Windows is usually \\\\ and not / ;

This is the full source code to test your OpenCV build:

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

int main()
{
   std::string file_name("C:\\Images\\1.jpg");
   cv::Mat original_image = cv::imread(file_name, cv::IMREAD_COLOR);
   if (original_image.empty())
   {
       std::cout << "!!! image not found" << std::endl;
       return -1;
   }

   cv::imshow( "Display window", original_image ); 
   cv::waitKey(0);

   return 0;
}

https://docs.opencv.org/2.4/doc/tutorials/introduction/display_image/display_image.html

You forgot to create window

namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.

Adding to karlphillips answer: Deubg and Release behave quite differently on Windows than on Linux (due to runtime selection).

Especially if you link against release libraries on Windows but your libs or executable are being built in debug. If they use different runtimes you will be very likely run into issues and segfaults. So check the flags of both projects (typical culprits are flags like multithreading (debug) being present on one but not the other).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM