Simple OpenCV program crashes

asked 2015-03-16 14:59:59 -0600

wagp11 gravatar image

updated 2015-03-16 17:05:58 -0600

Hello community,

I am currently having a problem. I am following the instructions given in this book "OpenCV 2 Computer Vison Application Programming Cookbook" for creating a simple image display program in OpenCV (2.4.10-1) using Qt 5.4, QtCreator in Ubuntu 14.04.

The code is as follows:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

int main() {
    // read an image
    cv::Mat image = cv::imread("${PATH_TO_HOME}/Pictures/bottle_label.jpg");

    if ( image.data == NULL )    {
        std::cout << "no data loaded" << std::endl;
        return -1;
    }
    else {
        // create image window named "My image"
        cv::namedWindow("My Image", 1);
        // show the image on window
        std::cout << "Image loaded" << std::endl;
        cv::imshow("My Image", image);
        // wait key for 5000 ms
        cv::waitKey(5000);

        return 0;
    }
}

And the contents in the .pro file are

# Standard .pro content for qt console application #
INCLUDEPATH += /usr/local/include/

LIBS += -L/usr/local/lib \
        -lopencv_core  -lopencv_highgui -lopencv_imgproc -lopencv_features2d -lopencv_calib3d

The code compiles, but when I run it the program crashes immediately. The debuger points a SIGSEGV occurring in line 17: cv::namedWindow("My Image", 1) I don't know what to do. I have other programs that use qt and opencv libraries in the same computer but used cmake to build them and they run fine. Could someone please point out where the problem is?

Thank you in advance

edit retag flag offensive close merge delete

Comments

The code compiles in qt creator. The problem is during the execution SIGSEGV. I compiled the same code under qt creator using cmake to build it and the program executes without a problem.

I tried what you recommended and I got this output: g

++ `pkg-config --libs --cflags opencv` -o test main.cpp
/usr/bin/ld: cannot find -lcufft
/usr/bin/ld: cannot find -lcublas
/usr/bin/ld: cannot find -lnpps
/usr/bin/ld: cannot find -lnppi
/usr/bin/ld: cannot find -lnppc
/usr/bin/ld: cannot find -lcudart
collect2: error: ld returned 1 exit status

Which I can also not understad, because I installed OpenCV WITH_CUDA=ON after installing cuda 6.5. I checked my opencv.pc file and the flags mentioned in the output don't point where the .so file actually are.

wagp11 gravatar imagewagp11 ( 2015-03-16 15:44:01 -0600 )edit

Did you check it with cv::namedWindow("My Image"); by removing 1

FLY gravatar imageFLY ( 2015-03-16 16:37:44 -0600 )edit

Yes, the original code does no use the second parameter. Both variants generate the same problem. The code compiles, but I get a direct crash.

wagp11 gravatar imagewagp11 ( 2015-03-16 16:52:27 -0600 )edit

did you check the path by making it simple like cv::Mat image = cv::imread(C://bottle_label.jpg"); ?

FLY gravatar imageFLY ( 2015-03-16 16:55:13 -0600 )edit

Yes, the path is correct and also tried your suggestion. The console ouput give me a "realloc(): invalid pointer" I have also tried to run it in another machine with opencv 2.4.8 an Qt 4 same problem there.

wagp11 gravatar imagewagp11 ( 2015-03-16 16:59:31 -0600 )edit

Did you by any chance compiled OpenCV against a different version of QT than the one you are using in your test program?

juanmanpr gravatar imagejuanmanpr ( 2015-03-17 07:50:13 -0600 )edit

I had initially openCV 2.4.9 along with qt4 . I deinstalled both and installed afterwards (in the following order) cuda 6.5, qt 5.4 and openCV 2.4.10-1. (In cmake build directory) cmake - D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D WITH_CUBLAS=ON -D WITH_CUFFT=ON -D WITH_EIGEN=ON -D WITH_OPENGL=ON -D WITH_QT=ON -D WITH_TBB=ON -D BUILD_DOCS=ON -D BUILD_EXAMPLES=ON -D BUILD_TESTS=ON -D CUDA_ARCH_BIN="3.0" ..

wagp11 gravatar imagewagp11 ( 2015-03-17 16:41:34 -0600 )edit

@wagp11 Same problem here. Have you figure it out by now? If so could you please share your solution here? Thanks

mask2007 gravatar imagemask2007 ( 2016-04-05 14:58:11 -0600 )edit