opencv / opencv_contrib

Repository for OpenCV's extra modules
Apache License 2.0
9.46k stars 5.77k forks source link

segmentation fault when using LSD or FLD #2714

Closed pdeman closed 4 years ago

pdeman commented 4 years ago

I am using a jetson tx2, base jetpack 32, ubuntu 18.04 I installed OpenCV => 4.3 including opencv-contrib following this:

sudo apt-get update
sudo apt-get install -y build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt-get install -y python3.6-dev python-dev python-numpy python3-numpy
sudo apt-get install -y libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev
sudo apt-get install -y libv4l-dev v4l-utils qv4l2 v4l2ucp
sudo apt-get install -y curl
sudo apt-get update

echo "** Download opencv-4.3.0"
cd $folder
curl -L https://github.com/opencv/opencv/archive/4.3.0.zip -o opencv-4.3.0.zip
curl -L https://github.com/opencv/opencv_contrib/archive/4.3.0.zip -o opencv_contrib-4.3.0.zip
unzip opencv-4.3.0.zip
unzip opencv_contrib-4.3.0.zip
cd opencv-4.3.0/

echo "** Apply patch"
sed -i 's/include <Eigen\/Core>/include <eigen3\/Eigen\/Core>/g' modules/core/include/opencv2/core/private.hpp

echo "** Building..."
mkdir release
cd release/
cmake -D WITH_CUDA=ON -D WITH_CUDNN=ON -D CUDA_ARCH_BIN="5.3,6.2,7.2" -D CUDA_ARCH_PTX="" -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.3.0/modules -D WITH_GSTREAMER=ON -D WITH_LIBV4L=ON -D BUILD_opencv_python2=OFF -D BUILD_opencv_python3=ON -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=OFF -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
make -j$(nproc)

when I do cv2.getBuildInformation() from python:

'General configuration for OpenCV 4.3.0 
=====================================
Version control:               
unknown  Extra modules:    Location (extra):            
/home/nvidia/opencv_build/opencv_contrib-4.3.0/modules   
Version control (extra):     unknown  Platform:    Timestamp:  2020-07-24T06:34:58Z   Host: Linux 4.9.140+ aarch64    
CMake:  3.10.2    CMake generator:   Unix Makefiles    CMake build tool: usr/bin/make\n    Configuration:  RELEASE  
CPU/HW features:    Baseline:                    NEON FP16      required:  NEON     disabled:  VFPV3
C/C++:    Built as dynamic libs?:      YES    C++ standard: 11    C++ Compiler:  /usr/bin/c++  (ver 7.5.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 -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment
-Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option 
-pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    
-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 -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread 
-fomit-frame-pointer -ffunction-sections -fdata-sections    
-fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG 
-D_DEBUG\n    C Compiler:                  /usr/bin/cc\n    C flags 
(Release):           -fsigned-char -W -Wall -Werror=return-type 
-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-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer 
-ffunction-sections -fdata-sections    -fvisibility=hidden -O3 -DNDEBUG  
-DNDEBUG\n    C flags (Debug):             -fsigned-char -W -Wall 
-Werror=return-type -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-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread 
-fomit-frame-pointer -ffunction-sections -fdata-sections    
-fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
Linker flags (Release):      -Wl,--gc-sections -Wl,--as-needed     
Linker flags (Debug):        -Wl,--gc-sections -Wl,--as-needed  \n    ccache:         
              NO\n    Precompiled headers:         NO\n    Extra 
dependencies:          m pthread cudart_static -lpthread dl rt nppc 
nppial nppicc nppicom nppidei nppif nppig nppim nppist nppisu nppitc npps cublas cufft -L/usr/local/cuda/lib64 -L/usr/lib/aarch64-linux-gnu\n 
    3rdparty dependencies:
  OpenCV modules:    To be built:         
         aruco bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hdf hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor ml objdetect optflow phase_unwrapping photo plot python3 quality rapid reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking video videoio videostab 
xfeatures2d ximgproc xobjdetect xphoto\n    Disabled:                    
python2 world\n    Disabled by dependency:      -\n    Unavailable:      
            alphamat cnn_3dobj cvv java js matlab ovis sfm ts viz\n    
Applications:                apps\n    Documentation:               NO\n 
    Non-free algorithms:         NO\n\n  GUI: \n    GTK+:                 
        YES (ver 3.22.30)\n      GThread :                  YES (ver 
2.56.3)\n      GtkGlExt:                  NO\n    VTK support:           
       NO\n\n  Media I/O: \n    ZLib:                        
/usr/lib/aarch64-linux-gnu/libz.so (ver 1.2.11)\n    JPEG:               
          /usr/lib/aarch64-linux-gnu/libjpeg.so (ver 80)\n    WEBP:       
                  build (ver encoder: 0x020f)\n    PNG:                   
       /usr/lib/aarch64-linux-gnu/libpng.so (ver 1.6.34)\n    TIFF:       
                  /usr/lib/aarch64-linux-gnu/libtiff.so (ver 42 / 
4.0.9)\n    JPEG 2000:                   build Jasper (ver 1.900.1)\n    
OpenEXR:                     build (ver 2.3.0)\n    HDR:                 
         YES\n    SUNRASTER:                   YES\n    PXM:              
            YES\n    PFM:                         YES\n\n  Video I/O:\n   
  DC1394:                      YES (2.2.5)\n    FFMPEG:                   
    YES\n      avcodec:                   YES (57.107.100)\n      
avformat:                  YES (57.83.100)\n      avutil:                
     YES (55.78.100)\n      swscale:                   YES (4.8.100)\n    
   avresample:                NO\n    GStreamer:                   YES 
(1.14.5)\n    v4l/v4l2:                    YES (linux/videodev2.h)\n\n  
Parallel framework:            pthreads\n\n  Trace:                      
    YES (with Intel ITT)\n\n  Other third-party libraries:\n    Lapack:   
                    NO\n    Eigen:                       NO\n    Custom 
HAL:                  YES (carotene (ver 0.0.1))\n    Protobuf:          
           build (3.5.1)\n\n  NVIDIA CUDA:                   YES (ver 
10.0, CUFFT CUBLAS)\n    NVIDIA GPU arch:             53 62 72\n    
NVIDIA PTX archs:\n\n  cuDNN:                         NO\n\n  OpenCL:    
                     YES (no extra features)\n    Include path:           

/home/nvidia/opencv_build/opencv-4.3.0/3rdparty/include/opencl/1.2    
Link libraries:              Dynamic load\n\n  Python 3:\n    
Interpreter:                 /usr/bin/python3 (ver 3.6.9)\n    
Libraries:                   /usr/lib/aarch64-linux-gnu/libpython3.6m.so 
(ver 3.6.9)\n    numpy:                       
/usr/local/lib/python3.6/dist-packages/numpy/core/include (ver 1.16.1)\n 
    install path:                
lib/python3.6/dist-packages/cv2/python-3.6\n\n  Python (for build):      
       /usr/bin/python2.7\n\n  Java:                          \n    ant:  
                        NO\n    JNI:                         NO\n    Java 
wrappers:               NO\n    Java tests:                  NO\n\n  
Install to:                    
/usr/local\n-----------------------------------------------------------------\n\n'

and then I python3 I do:

fullName = 'car.jpg'
folder, imgName = os.path.split(fullName)
src = cv2.imread(fullName, cv2.IMREAD_COLOR)
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
lsd = cv.LineSegmentDetector();
fld = cv.FastLineDetector();

this works. but when I do : lines_lsd = lsd.detect(gray) or lines_fld = fld.detect(gray); both generates a segmentation fault and I don't know why.

alalek commented 4 years ago

Build with debug information, run under gdb and provide stracktrace dump.

pdeman commented 4 years ago

I didn't know gdb can be used with python. I am installing it. and I am compiling opencv in debug mode. but how can I specify python to use opencv "debug" or "release" ? for now I did this py file.

import cv2

src = cv2.imread("/SSD/corridor.jpg", cv2.IMREAD_COLOR)
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
lsd = cv2.LineSegmentDetector()
fld = cv2.ximgproc_FastLineDetector()

lines_lsd = lsd.detect(gray)

I do

gdb python3
run testLineSegmentDetector.py

and I get :

Using host libthread_db library "/lib/aarch64-linux-gnu/libthread_db.so.1".
[New Thread 0x7f845c11f0 (LWP 18624)]
[New Thread 0x7f83dc01f0 (LWP 18625)]
[New Thread 0x7f835bf1f0 (LWP 18626)]
[New Thread 0x7f82dbe1f0 (LWP 18627)]
[New Thread 0x7f825bd1f0 (LWP 18628)]

Thread 1 "python3" received signal SIGSEGV, Segmentation fault.
0x0000007fb6060308 in pyopencv_cv_LineSegmentDetector_detect(_object*, _object*, _object*) ()
   from /usr/local/lib/python3.6/dist-packages/cv2/python-3.6/cv2.cpython-36m-aarch64-linux-gnu.so

I don't know if it used opencv release or debug build.

berak commented 4 years ago

@pdeman

lsd = cv2.LineSegmentDetector()

does not create a valid object ! instead you would have to call createLineSegmentDetector() to create an instance, which unfortunately will throw:

cv2.error: OpenCV(4.5.0-pre) C:\p\opencv\modules\imgproc\src\lsd.cpp:143: error: (-213:The function/feature is not implemented) Implementation has been removed due original code license issues in function 'LineSegmentDetectorImpl'

so, currently, you cannot use LSD at all ;(

apart from that, it's a similar problem to:

o = cv2.ORB() # must be cv2.ORB_create()
o.detect(np.ones((100,100,3),np.uint8))

(segfaults, dereferencing the self pointer)

imho, it's a bug, that we are able to call python constructors like that.

p.s: at least createFastLineDetector() seems to work as expected..

alalek commented 4 years ago

@berak is right.

cv.LineSegmentDetector() cv.FastLineDetector()

Both classes are "abstract" and should not be used (work) in this way. Looks like python binding generator doesn't handle them properly. You should use createFastLineDetector() instead.

BTW, LSD implementation was removed from OpenCV due to license issues.

pdeman commented 4 years ago

ok thanks. the fastlinedetector is working. too bad for lsd, i had better results with it. I guess the license problem is not related to python. If have try to use LSD using c++ that will be the same.