null pointer in opencv (QT) when running
-
26-10-2019 - |
Question
Hello i have a Macbook pro with osx 10.7. I just installed Qt (nokia), opencv, cmake. I have never worked with any of those . So i started my first tutorial. I created a new QT Gui application. I changed my main.cpp to :
#include <QtGui/QApplication>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
IplImage* img = 0;
img = cvLoadImage("cat.jpg");
cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE);
cvShowImage("Example1", img);
cvWaitKey(0);
cvReleaseImage(&img);
cvDestroyWindow("Example1");
return a.exec();
}
I also changed my .pro file:
QT += core gui
TARGET = untitled1
TEMPLATE = app
INCLUDEPATH = /usr/local/include
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
LIBS += -lm -lopencv_core -lopencv_highgui -lopencv_video -lopencv_imgproc
FORMS += mainwindow.ui
for the rest :
the cat.jpg is in the qt project folder (next to main.cpp)
Now when i build this and run it. i get this error:
Starting /Users/olivierjanssens/untitled1-build-desktop-Desktop_Qt_4_7_4_for_GCC__Qt_SDK__Debug/untitled1.app/Contents/MacOS/untitled1...
OpenCV Error: Null pointer (NULL array pointer is passed) in cvGetMat, file /Users/olivierjanssens/source/OpenCV-2.3.1/modules/core/src/array.cpp, line 2382
terminate called throwing an exceptionThe program has unexpectedly finished.
/Users/olivierjanssens/untitled1-build-desktop-Desktop_Qt_4_7_4_for_GCC__Qt_SDK__Debug/untitled1.app/Contents/MacOS/untitled1 exited with code 0
The window opens but then it crashes and no image is shown.
What am i doing wrong ?
Solution
cat.jpg
is probably not in the right directory.
If you don't supply a path it searches the current directory, which is probably /Users/olivierjanssens/untitled1-build-desktop-Desktop_Qt_4_7_4_for_GCC__Qt_SDK__Debug
or /Users/olivierjanssens/untitled1-build-desktop-Desktop_Qt_4_7_4_for_GCC__Qt_SDK__Debug/untitled1.app/Contents/MacOS
. The directory with main.cpp
in it isn't searched.
Try supplying a full path to cvLoadImage()
(something like "/Users/olivierjanssens/untitled1/cat.jpg"
)