我今天按照此指南在最新版本的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
-----------------------------------------------------------------
答案 0 :(得分:0)
只需检查图像的路径,这是一个加载问题 因此,将图片复制到与您的cpp代码相同的目录中,或者只是给“ imread”一个绝对路径