简体   繁体   English

在简单的 OpenCV 代码中遇到分段错误

[英]Encountered Segmentation fault in simple OpenCV code

Tools工具

  • Platform : 64-bit Windows平台:64 位 Windows
  • Compiler chain: mingw with Qt编译器链:mingw with Qt
  • Make system: CMake制作系统:CMake
  • Libraries: C++ 11, OpenCV 4, Qt 5库:C++ 11、OpenCV 4、Qt 5

Problem (Updated)问题(更新)

The following simple program segment should compile and display the generated image in OpenCV.下面的简单程序段应该在 OpenCV 中编译并显示生成的图像。 However, it always SIGSEGVs in DEBUG mode only(Backtrace at the end).但是,它始终仅在调试模式下使用 SIGSEGV(最后的回溯)。 However, it works just fine in RELEASE mode.但是,它在 RELEASE 模式下工作得很好。

#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.我有一个 CMake 脚本,它只构建所需的 OpenCV 模块并将这些模块链接到依赖项。 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.二进制文件可以在没有丢失 dll 错误的情况下运行。 Dependency walker also indicates the same. Dependency walker 也表示相同。

Here is the backtrace:这是回溯:

堆栈跟踪

Few things to check here.很少有事情要检查这里。

First, where do the OpenCV library come from?首先,OpenCV 库从何而来? Is it compiled for your CPU?它是为你的 CPU 编译的吗? Looks like it crashed in AVX instructions.看起来它在 AVX 指令中崩溃了。 Might be that CPU does not support them.可能是 CPU 不支持它们。

Second, not obvious at all, happened to me with .png files.其次,根本不明显,发生在我身上的.png文件。 Same segfault during runtime.运行时相同的段错误。 Turned out that OpenCV was built without png support.原来,OpenCV 是在没有png支持的情况下构建的。 Please check if your OpenCV built with -DWITH_JPEG=ON .请检查您的 OpenCV 是否使用-DWITH_JPEG=ON构建。

Given that OpenCV works fine in Release mode, I suggest rebuilding the Debug version of the library.鉴于 OpenCV 在 Release 模式下工作正常,我建议重建库的 Debug 版本。

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.在这个例子中不需要使用 Qt,它增加了你现在不需要的复杂性。 Remove it from the project and its libraries on the link instruction in the CMake script.根据 CMake 脚本中的链接指令将其从项目及其库中删除。 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.现在您需要查明问题出在 OpenCV 还是 Qt 中。
  • An image can only be displayed on a window if cv::waitKey() is invoked;如果调用了cv::waitKey()则图像只能显示在窗口上;
  • The directory separator on Windows is usually \\\\ and not / ; Windows 上的目录分隔符通常是\\\\而不是/

This is the full source code to test your OpenCV build:这是测试 OpenCV 构建的完整源代码:

#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 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).添加到 karlphillips 答案:Deubg 和 Release 在 Windows 上的行为与在 Linux 上的行为完全不同(由于运行时选择)。

Especially if you link against release libraries on Windows but your libs or executable are being built in debug.特别是如果您链接到 Windows 上的发布库,但您的库或可执行文件是在调试中构建的。 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).因此,请检查两个项目的标志(典型的罪魁祸首是多线程(调试)等标志出现在一个而不是另一个上)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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