5

I installed OpenCV 4.0 on the latest version of Raspbian today following this guide: https://docs.opencv.org/4.0.0/d7/d9f/tutorial_linux_install.html

I followed each required step exactly, skipped all optional ones, and ran cmake with no parameters. I also installed the stripped version because I was running into issues installing the full one.

Afterwards I ran sudo apt-get install libopencv-dev to resolve some linker issues.

I'm now trying to run a simple program to display an image, but it segfaults upon reaching imshow.

Here it is in its entirety:

#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;
}

I'm compiling with g++ -I/usr/local/include/opencv4 main.cpp `pkg-config --cflags --libs opencv`

Running with gdb gives this output:

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

And running with valgrind gives this:

==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.

I noticed the valgrind output shows there are invalid reads before the first line of main executes, so perhaps this is the source of the problem? I'm not sure what the error means, though.

I have searched for similar issues on StackOverflow already and have not been able to find much. Please let me know if there's anything I can do to improve this question.

Edit: Don't think I made this clear--the program is never entering the "read error" block. I have since changed the code to return if it does enter that block, but it is still segfaulting.

Here is the installation information:

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
-----------------------------------------------------------------
Phoebe
  • 61
  • 1
  • 4
  • 1
    is "read error" printed before the segfault? If yes, add a return 0 in that if-block, because you dont want to proceed if the image could not successfully be loaded. – Micka Dec 31 '18 at 06:45
  • I thought OpenCV 4 required use of `-std=c++14` – Mark Setchell Dec 31 '18 at 09:29
  • @Micka No, I should have clarified. There is no read error before the segfault. – Phoebe Dec 31 '18 at 14:03
  • @MarkSetchell I tried compiling with that option but there was also no change. – Phoebe Dec 31 '18 at 14:06
  • I think your include files are for OpenCV version 2. I believe you only need 1 include file from v3 onwards `opencv.hpp` – Mark Setchell Dec 31 '18 at 14:35
  • @MarkSetchell You're right, it does work with just that--I wasn't aware. The segfault is still occurring, however. – Phoebe Dec 31 '18 at 14:43
  • I don't understand why your `gdb` message mentions `_cxx11` if you are using `c++14` – Mark Setchell Dec 31 '18 at 14:46
  • The installation should have created an executable called `opencv_version` somewhere. Maybe you can find it with `find / -name opencv_version 2>/dev/null` and run it with `-v` parameter and add the output to your question by clicking `edit` underneath it. – Mark Setchell Dec 31 '18 at 14:49
  • @MarkSetchell, I printed the `__cplusplus` macro which shows "201402". Unless I'm misunderstanding the compiler is indeed using C++14, regardless of whether I include the `-std=c++14` flag. – Phoebe Dec 31 '18 at 14:56
  • @MarkSetchell Original post is updated with version info – Phoebe Dec 31 '18 at 15:04
  • I am at the edges of my experience here, not having used v4 myself, so other folk may have better or more useful ideas than me. For the moment though, it looks to me like OpenCV was built with `/usr/bin/c++` maybe not c++14 but you are trying to build with `g++`. I am not sure if that is an issue or not but, if it were me, I would try to build both OpenCV and my app with the same compiler both to c++14... as I said though, that may not be necessary at all. Sorry I don't know more. – Mark Setchell Dec 31 '18 at 15:21
  • No worries, I'll continue looking into it. Thanks for the help! – Phoebe Dec 31 '18 at 16:06

1 Answers1

1

Just check the path of your image, it s a load problem so copy the image in the same directory as your cpp code or just give the "imread" an absolute path

Spinkoo
  • 2,080
  • 1
  • 7
  • 23
  • 1
    It is in the same directory--I tried using an absolute path anyway, but to no avail. – Phoebe Dec 31 '18 at 14:06
  • hey try to add exit(0) to your code if src is empty – Spinkoo Dec 31 '18 at 14:18
  • Thanks for the suggestion, I don't think I was clear but it is never entering that if block. I've since added an exit(0) line just to be safe, but the results are the same! – Phoebe Dec 31 '18 at 14:29