OpenCV imshow分段错误

OpenCV imshow segmentation fault

本文关键字:错误 分段 imshow OpenCV      更新时间:2023-10-16

我今天按照本指南在最新版本的Raspbian上安装了OpenCV 4.0:https://docs.opencv.org/4.0.0/d7/d9f/tutorial_linux_install.html

我准确地遵循了每个必需的步骤,跳过了所有可选的步骤,并在没有参数的情况下运行cmake。我还安装了精简版,因为我在安装完整版时遇到了问题。

之后,我运行sudo apt-get install libopencv-dev来解决一些链接器问题。

我现在正试图运行一个简单的程序来显示图像,但它在到达imshow时出错。

这里是它的整体:

#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main() {
cout << "hello" << endl;
Mat src = imread("test_img_1.jpg", IMREAD_COLOR);
if (src.empty()) {
cout << "read error" << endl;
}
namedWindow("Source", WINDOW_AUTOSIZE);
imshow("Source", src);
waitKey(0);
return 0;
}

我正在使用g++ -I/usr/local/include/opencv4 main.cpp `pkg-config --cflags --libs opencv`进行编译

使用gdb运行会得到以下输出:

Thread 1 "a.out" received signal SIGSEGV, Segmentation fault.
0x76b24cdc in cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&) ()
from /usr/lib/arm-linux-gnueabihf/libopencv_highgui.so.2.4

使用valgrind运行会得到以下结果:

==5560== Memcheck, a memory error detector
==5560== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==5560== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==5560== Command: ./a.out
==5560== 
==5560== Invalid read of size 8
==5560==    at 0x486603C: ??? (in /usr/lib/arm-linux-gnueabihf/libarmmem.so)
==5560==  Address 0xb6c48b4 is 12 bytes inside a block of size 18 alloc'd
==5560==    at 0x4847568: malloc (vg_replace_malloc.c:299)
==5560== 
==5560== Invalid read of size 8
==5560==    at 0x4866054: ??? (in /usr/lib/arm-linux-gnueabihf/libarmmem.so)
==5560==  Address 0xb6c5344 is 20 bytes inside a block of size 26 alloc'd
==5560==    at 0x4847568: malloc (vg_replace_malloc.c:299)
==5560== 
==5560== Invalid read of size 8
==5560==    at 0x4865CA4: ??? (in /usr/lib/arm-linux-gnueabihf/libarmmem.so)
==5560==  Address 0xb6cdc24 is 28 bytes inside a block of size 34 alloc'd
==5560==    at 0x4847568: malloc (vg_replace_malloc.c:299)
==5560== 
==5560== Invalid read of size 8
==5560==    at 0x4865FC4: ??? (in /usr/lib/arm-linux-gnueabihf/libarmmem.so)
==5560==  Address 0xb6e1d64 is 20 bytes inside a block of size 26 alloc'd
==5560==    at 0x4847568: malloc (vg_replace_malloc.c:299)
==5560== 
hello
==5560== Invalid read of size 8
==5560==    at 0x4865B38: ??? (in /usr/lib/arm-linux-gnueabihf/libarmmem.so)
==5560==  Address 0xb800e34 is 12 bytes inside a block of size 17 alloc'd
==5560==    at 0x4847568: malloc (vg_replace_malloc.c:299)
==5560==    by 0x6508C2F: g_malloc (in /lib/arm-linux-gnueabihf/libglib-2.0.so.0.5000.3)
==5560== 
==5560== Invalid read of size 8
==5560==    at 0x4865D34: ??? (in /usr/lib/arm-linux-gnueabihf/libarmmem.so)
==5560==  Address 0xb83cfa4 is 12 bytes inside a block of size 18 alloc'd
==5560==    at 0x4847568: malloc (vg_replace_malloc.c:299)
==5560==    by 0x6508C2F: g_malloc (in /lib/arm-linux-gnueabihf/libglib-2.0.so.0.5000.3)
==5560== 
==5560== Invalid read of size 8
==5560==    at 0x48660C8: ??? (in /usr/lib/arm-linux-gnueabihf/libarmmem.so)
==5560==  Address 0xb841c24 is 12 bytes inside a block of size 19 alloc'd
==5560==    at 0x4847568: malloc (vg_replace_malloc.c:299)
==5560==    by 0x6508C2F: g_malloc (in /lib/arm-linux-gnueabihf/libglib-2.0.so.0.5000.3)
==5560== 
==5560== Invalid read of size 8
==5560==    at 0x4865BC4: ??? (in /usr/lib/arm-linux-gnueabihf/libarmmem.so)
==5560==  Address 0xb84796c is 12 bytes inside a block of size 17 alloc'd
==5560==    at 0x4847568: malloc (vg_replace_malloc.c:299)
==5560==    by 0x6508C2F: g_malloc (in /lib/arm-linux-gnueabihf/libglib-2.0.so.0.5000.3)
==5560== 
==5560== Invalid read of size 8
==5560==    at 0x4865C78: ??? (in /usr/lib/arm-linux-gnueabihf/libarmmem.so)
==5560==  Address 0xb848850 is 8 bytes inside a block of size 14 alloc'd
==5560==    at 0x4847568: malloc (vg_replace_malloc.c:299)
==5560==    by 0x6508C2F: g_malloc (in /lib/arm-linux-gnueabihf/libglib-2.0.so.0.5000.3)
==5560== 
==5560== Invalid read of size 8
==5560==    at 0x4865FAC: ??? (in /usr/lib/arm-linux-gnueabihf/libarmmem.so)
==5560==  Address 0xb84b8cc is 12 bytes inside a block of size 19 alloc'd
==5560==    at 0x4847568: malloc (vg_replace_malloc.c:299)
==5560==    by 0x6508C2F: g_malloc (in /lib/arm-linux-gnueabihf/libglib-2.0.so.0.5000.3)
==5560== 
==5560== Invalid read of size 8
==5560==    at 0x4865E8C: ??? (in /usr/lib/arm-linux-gnueabihf/libarmmem.so)
==5560==  Address 0xb871790 is 8 bytes inside a block of size 15 alloc'd
==5560==    at 0x4847568: malloc (vg_replace_malloc.c:299)
==5560==    by 0x6508C2F: g_malloc (in /lib/arm-linux-gnueabihf/libglib-2.0.so.0.5000.3)
==5560== 
==5560== Invalid read of size 8
==5560==    at 0x4865BDC: ??? (in /usr/lib/arm-linux-gnueabihf/libarmmem.so)
==5560==  Address 0xb8f7ec4 is 28 bytes inside a block of size 34 alloc'd
==5560==    at 0x4847568: malloc (vg_replace_malloc.c:299)
==5560==    by 0x6508C2F: g_malloc (in /lib/arm-linux-gnueabihf/libglib-2.0.so.0.5000.3)
==5560== 
==5560== Invalid read of size 4
==5560==    at 0x4CFDCDC: cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&) (in /usr/lib/arm-linux-gnueabihf/libopencv_highgui.so.2.4.9)
==5560==  Address 0x1010018 is not stack'd, malloc'd or (recently) free'd
==5560== 
==5560== 
==5560== Process terminating with default action of signal 11 (SIGSEGV)
==5560==  Access not within mapped region at address 0x1010018
==5560==    at 0x4CFDCDC: cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&) (in /usr/lib/arm-linux-gnueabihf/libopencv_highgui.so.2.4.9)
==5560==  If you believe this happened as a result of a stack
==5560==  overflow in your program's main thread (unlikely but
==5560==  possible), you can try to increase the size of the
==5560==  main thread stack using the --main-stacksize= flag.
==5560==  The main thread stack size used in this run was 8388608.

我注意到valgrind输出显示在执行main的第一行之前有无效的读取,所以这可能是问题的根源?不过,我不确定这个错误是什么意思。

我已经在StackOverflow上搜索过类似的问题,但找不到太多。如果我能做些什么来改进这个问题,请告诉我。

编辑:不要以为我说得很清楚——程序从未进入"读取错误"块。从那以后,我更改了代码,如果它真的进入了那个块,就会返回,但它仍然是分段错误。

以下是安装信息:

General configuration for OpenCV 4.0.0 =====================================
Version control:               unknown
Platform:
Timestamp:                   2018-12-30T23:20:59Z
Host:                        Linux 4.14.79-v7+ armv7l
CMake:                       3.7.2
CMake generator:             Unix Makefiles
CMake build tool:            /usr/bin/make
Configuration:               Release
CPU/HW features:
Baseline:
requested:                 DETECT
disabled:                  VFPV3 NEON
C/C++:
Built as dynamic libs?:      YES
C++ Compiler:                /usr/bin/c++  (ver 6.3.0)
C++ flags (Release):         -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -mfp16-format=ieee -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
C++ flags (Debug):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -mfp16-format=ieee -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
C Compiler:                  /usr/bin/cc
C flags (Release):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -mfp16-format=ieee -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
C flags (Debug):             -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -mfp16-format=ieee -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
Linker flags (Release):      
Linker flags (Debug):        
ccache:                      NO
Precompiled headers:         YES
Extra dependencies:          dl m pthread rt
3rdparty dependencies:
OpenCV modules:
To be built:                 calib3d core dnn features2d flann gapi highgui imgcodecs imgproc java_bindings_generator ml objdetect photo python2 python3 python_bindings_generator stitching ts video videoio
Disabled:                    world
Disabled by dependency:      -
Unavailable:                 java js
Applications:                tests perf_tests apps
Documentation:               NO
Non-free algorithms:         NO
GUI: 
GTK+:                        YES (ver 2.24.31)
GThread :                  YES (ver 2.50.3)
GtkGlExt:                  NO
VTK support:                 NO
Media I/O: 
ZLib:                        /usr/lib/arm-linux-gnueabihf/libz.so (ver 1.2.8)
JPEG:                        libjpeg-turbo (ver 1.5.3-62)
WEBP:                        build (ver encoder: 0x020e)
PNG:                         /usr/lib/arm-linux-gnueabihf/libpng.so (ver 1.6.28)
TIFF:                        build (ver 42 - 4.0.9)
JPEG 2000:                   build (ver 1.900.1)
OpenEXR:                     build (ver 1.7.1)
HDR:                         YES
SUNRASTER:                   YES
PXM:                         YES
PFM:                         YES
Video I/O:
DC1394:                      NO
FFMPEG:                      YES
avcodec:                   YES (ver 57.64.101)
avformat:                  YES (ver 57.56.101)
avutil:                    YES (ver 55.34.101)
swscale:                   YES (ver 4.2.100)
avresample:                NO
GStreamer:                   NO
v4l/v4l2:                    linux/videodev2.h
Parallel framework:            pthreads
Trace:                         YES (built-in)
Other third-party libraries:
Lapack:                      NO
Eigen:                       NO
Custom HAL:                  YES (carotene (ver 0.0.1))
Protobuf:                    build (3.5.1)
OpenCL:                        YES (no extra features)
Include path:                /home/pi/opencv-4.0.0/3rdparty/include/opencl/1.2
Link libraries:              Dynamic load
Python 2:
Interpreter:                 /usr/bin/python2.7 (ver 2.7.13)
Libraries:                   /usr/lib/arm-linux-gnueabihf/libpython2.7.so (ver 2.7.13)
numpy:                       /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.12.1)
packages path:               lib/python2.7/dist-packages
Python 3:
Interpreter:                 /usr/bin/python3 (ver 3.5.3)
Libraries:                   /usr/lib/arm-linux-gnueabihf/libpython3.5m.so (ver 3.5.3)
numpy:                       /usr/lib/python3/dist-packages/numpy/core/include (ver 1.12.1)
packages path:               lib/python3.5/dist-packages
Python (for build):            /usr/bin/python2.7
Java:                          
ant:                         /usr/bin/ant (ver 1.9.9)
JNI:                         NO
Java wrappers:               NO
Java tests:                  NO
Install to:                    /usr/local
-----------------------------------------------------------------

只需检查图像的路径,这是一个负载问题因此,将图像复制到与cpp代码相同的目录中,或者只给"imread"一个绝对路径