PDA

View Full Version : the cv::imread OpenCV function can't read image file in Qt Creator



northbear
23rd January 2013, 14:08
my codes are following:

1, the code of the project file:

#-------------------------------------------------
#
# Project created by QtCreator
#
#-------------------------------------------------

QT -= gui

TARGET = myQtConsoleProject
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp

INCLUDEPATH += E:\opencv\build\include\

LIBS += -LE:\opencv\build\x86\vc10\lib \
-lopencv_core243 \
-lopencv_highgui243 \
-lopencv_imgproc243 \
-lopencv_features2d243 \
-lopencv_calib3d243

2, the code of the only C++ file of the project----main.cpp:

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

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

// read an image
cv::Mat image= cv::imread("img.jpg");
// create image window named "My Image"
cv::namedWindow("My Image");
// show the image on window
cv::imshow("My Image", image);
// wait key for 5000 ms
cv::waitKey(5000);

return a.exec();
}


The OS used is Windows xp, the VS2010 has been completely installed, and OpenCV 2.4.3 has been installed. The project posted above cab be built successfully.

The problem is that the project built can't run normoally because of the cv::imread can't read the image file.

please help me. Thanks.

Added after 1 59 minutes:

The plateform is Windowsxp + MSVS2010 + Qt 5.0 for Windows +
Qt Creator 2.6.1 for Windows.


my codes are following:

1, the code of the project file:

#-------------------------------------------------
#
# Project created by QtCreator
#
#-------------------------------------------------

QT -= gui

TARGET = myQtConsoleProject
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp

INCLUDEPATH += E:\opencv\build\include\

LIBS += -LE:\opencv\build\x86\vc10\lib \
-lopencv_core243 \
-lopencv_highgui243 \
-lopencv_imgproc243 \
-lopencv_features2d243 \
-lopencv_calib3d243

2, the code of the only C++ file of the project----main.cpp:

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

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

// read an image
cv::Mat image= cv::imread("img.jpg");
// create image window named "My Image"
cv::namedWindow("My Image");
// show the image on window
cv::imshow("My Image", image);
// wait key for 5000 ms
cv::waitKey(5000);

return a.exec();
}


The OS used is Windows xp, the VS2010 has been completely installed, and OpenCV 2.4.3 has been installed. The project posted above cab be built successfully.

The problem is that the project built can't run normoally because of the cv::imread can't read the image file.

please help me. Thanks.

d_stranz
23rd January 2013, 22:52
So why are you posting this question in a Qt forum? What does this have to do with Qt besides the fact that you instantiate a QCoreApplication - it looks like an OpenCV problem to me. Find an OpenCV forum and post it there.

The simplest problem you could have is that OpenCV may not be looking for your jpg file where you think it is looking and it isn't finding the file.

Or your JPG file could be corrupt.

Or OpenCV needs some plugin to read JPG files, and you don't have the plugin installed.

Or the "imread" method isn't the right one to use to read JPG files.

Or any one of a thousand other things which Qt programmer have no idea about, since most of us aren't OpenCV programmers.

northbear
24th January 2013, 03:21
Sorry, I see. I will accept your suggestion.

The same code has been compiled in the VS2010 IDE onece, the executable file generated can run normally, that is it can read the image file and display it.

But, The same code has been compiled in the Qt Creator, the problem occurs.

So, Hope the experienced Qt creator programmers help me.

ChrisW67
24th January 2013, 04:15
Since Qt Creator is not a compiler or linker and this is not a failing Qt library call this still has nothing to do with Qt. The involvement of Qt ended when qmake wrote a Makefile for your compiler and linker based on your PRO file.

An OpenCV function failed, why not ask the OpenCV library for a reason?
Or Google up (http://www.lmgtfy.com/?q=opencv+imread+fails) some interesting things like this quirk in OpenCV: http://stackoverflow.com/questions/9125817/opencv-imreadfilename-fails-in-debug-mode-when-using-release-libraries

My first guess is the same as d_stranz: the image file is not in the current working directory of the running program. Please note this is generally not the same thing as the source code directory.

northbear
24th January 2013, 04:33
Thank u for paying attention to my problem. I knew Qt Creator is not a compiler or linker.

I want to know how to develop a application with OpenCV by using Qt Creator!

northbear
27th January 2013, 03:24
The Problem has been solved.
Just modify the .pro file as the following:
QT -= gui

TARGET = myQtConsoleProject
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp

INCLUDEPATH += E:\opencv\build\include\

CONFIG(debug,debug|release){
LIBS += E:\opencv\build\x86\vc10\lib\opencv_core243d.lib
LIBS += E:\opencv\build\x86\vc10\lib\opencv_highgui243d.li b
LIBS += E:\opencv\build\x86\vc10\lib\opencv_imgproc243d.li b
LIBS += E:\opencv\build\x86\vc10\lib\opencv_features2d243d .lib
LIBS += E:\opencv\build\x86\vc10\lib\opencv_calib3d243d.li b
}

CONFIG(release,debug|release){
LIBS += E:\opencv\build\x86\vc10\lib\opencv_core243.lib
LIBS += E:\opencv\build\x86\vc10\lib\opencv_highgui243.lib
LIBS += E:\opencv\build\x86\vc10\lib\opencv_imgproc243.lib
LIBS += E:\opencv\build\x86\vc10\lib\opencv_features2d243. lib
LIBS += E:\opencv\build\x86\vc10\lib\opencv_calib3d243.lib
}

share this with all of you.

ibr_ex
9th September 2013, 08:27
Hi, :D
Same problem here but under Ubuntu :p. I tried the above solution but no luck :eek:.
Any suggestions?
Everything works fine outside qtcreator.

ChrisW67
11th September 2013, 00:10
Search the forum for OpenCV. This has been covered numerous times in this forum (most recently this morning), and it is even in the documentation

ibr_ex
13th September 2013, 10:57
This has been covered numerous times in this forum
Thanks for reply. I already did and tried those solutions but still negative. Maybe I didn't correctly. Here is my environment:
openSUSE 12.3 64 bits with Gnome Desktop 3.6.2
I have installed openCV using install/remove software tool comes with gnome. I have installed qtcreator 2.8.0 manually. Qt 4.8.4 is already installed in openSUSE.
In usr/include/ there are two opencv folders
opencv
opencv2
all opencv libraries and links files are in usr/libs64
e.g. libopencv_core.so.2.4.6
libopencv_core.so.2.4
libopencv_core.so

Here is my .pro contents:


#-------------------------------------------------
#
# Project created by QtCreator 2013-09-05T12:37:35
#
#-------------------------------------------------

QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = QTopenCV03
TEMPLATE = app

INCLUDEPATH += /usr/include/opencv2

LIBS += -L/usr/lib64
LIBS += -lopencv_core
LIBS += -lopencv_imgproc
LIBS += -lopencv_highgui
LIBS += -lopencv_features2d
LIBS += -lopencv_calib3d

SOURCES += main.cpp\
mainwindow.cpp

HEADERS += mainwindow.h

FORMS += mainwindow.ui



The contents of main.cpp


#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}


The contents of mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
void on_pushButton_clicked( );

private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H



The contents of mainwindow.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace cv;


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButton_clicked()
{

IplImage* pImg = cvLoadImage("./HappyFish.jpg");
if(pImg == NULL)
std::cout<< "Error loading image"<<std::endl;
cvShowImage("Image", pImg);

cvReleaseImage(&pImg);


std::cout<< "start **********"<<std::endl;
Mat image;
image = imread("./HappyFish.jpg"); // Read the file

if( !image.data ) { std::cout<< "Error loading src again"<<std::endl; }
namedWindow( "Display window", 1 );// Create a window for display.
imshow( "Display window", image ); // Show our image inside it.

waitKey(5000); // Wait for a keystroke in the window
}


The compiler output:


Starting /home/ibr/projects/build-QTopenCV03-Desktop-Release/QTopenCV03...
Error loading image
start **********
Error loading src again
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /home/abuild/rpmbuild/BUILD/opencv-2.4.6.1/modules/highgui/src/window.cpp, line 269
Qt has caught an exception thrown from an event handler. Throwing
exceptions from an event handler is not supported in Qt. You must
reimplement QApplication::notify() and catch all exceptions there.

terminate called after throwing an instance of 'cv::Exception'
what(): /home/abuild/rpmbuild/BUILD/opencv-2.4.6.1/modules/highgui/src/window.cpp:269: error: (-215) size.width>0 && size.height>0 in function imshow

The program has unexpectedly finished.
/home/ibr/projects/build-QTopenCV03-Desktop-Release/QTopenCV03 exited with code 0


Is there something I did wrong? The same code works with out problem outside qtcreator.
any suggestions? :)

wysota
13th September 2013, 11:22
The same code works with out problem outside qtcreator.

Do you mean that it works if you build the project using the command line (cd, qmake, make), it works, and the same project (with the same .pro file) built by pushing the "build" button in QtCreator doesn't work?

ChrisW67
13th September 2013, 21:35
You are opening a file using a relative path. The process current working directory is different between when it "works with out problem outside qtcreator" versus when Qt Creator runs the process on your behalf. Different base directory, different absolute path to the file it tries to open, different result.

It crashes terribly because OpenCv throws exceptions and Qt is not able to handle this in slot code. You must avoid or catch the exception and handle it yourself.

Since your OpenCV install is in the system standard directory your INCLUDEPATH does not need to be modified for OpenCV.
The LIBS variable does not need a search path (-L) addition because the libs are already on the default search path.

ibr_ex
16th September 2013, 10:14
Thanks guys for your reply.


Do you mean that it works if you build the project using the command line (cd, qmake, make), it works, and the same project (with the same .pro file) built by pushing the "build" button in QtCreator doesn't work?

I mean when I write a code like bellow in a single cpp file and compile it using this line


g++ myCVex01.cpp -o myCVex01 -I /usr/local/include/opencv -L /usr/local/lib -lm -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_objdetect

every thing works fine. Here is the code:


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

using namespace cv;

int main( void ){

IplImage* pImg = cvLoadImage("./HappyFish.jpg");
if(pImg == NULL)
std::cout<< "Error loading image"<<std::endl;
cvShowImage("Image", pImg);

cvReleaseImage(&pImg);


std::cout<< "start **********"<<std::endl;
Mat image;
image = imread("./cat.jpg"); // Read the file

if( !image.data ) { std::cout<< "Error loading src again"<<std::endl; }
namedWindow( "Display window", 1 );// Create a window for display.
imshow( "Display window", image ); // Show our image inside it.

waitKey(5000); // Wait for a keystroke in the window
}



Since your OpenCV install is in the system standard directory your INCLUDEPATH does not need to be modified for OpenCV.
The LIBS variable does not need a search path (-L) addition because the libs are already on the default search path.


If I remove the INCLUDEPATH and LIBS part from the project I get many errors like this


/home/ibr/projects/QTopenCV03/mainwindow.cpp:-1: error: undefined reference to `cvLoadImage'

wysota
16th September 2013, 12:41
So why don't you repeat the same line in your project file by adding the libraries to LIBS and include search paths to INCLUDEPATH.

ibr_ex
17th September 2013, 13:04
So why don't you repeat the same line in your project file by adding the libraries to LIBS and include search paths to INCLUDEPATH

I tried this but also negative.

wysota
17th September 2013, 14:42
What is negative? Do you get the exact same error? What does your project file look like?

ChrisW67
18th September 2013, 06:15
Since your OpenCV install is in the system standard directory your INCLUDEPATH does not need to be modified for OpenCV.
The LIBS variable does not need a search path (-L) addition because the libs are already on the default search path.

If I remove the INCLUDEPATH and LIBS part from the project I get many errors like this


/home/ibr/projects/QTopenCV03/mainwindow.cpp:-1: error: undefined reference to `cvLoadImage'


Read my advice again. It does not say remove the LIBS entry, it says you do not need the -L option that specifies the standard system path because the linker will search there anyway. The LIBS entry contains more than just the -L options, and these must stay. It does not say remove the INCLUDEPATH, it says the INCLUDEPATH does not need to be modified for OpenCV. There may be other things in INCLUDEPATH that need to stay.

Now your manual compilation command is implying that OpenCV is not installed where you were previously telling us, i.e. under/usr/local rather than /usr.

All that is secondary to the program that crashes after it is successfully compiled and linked. The reason for that is already in this thread.

ibr_ex
18th September 2013, 08:57
========================================
SOLVED :D I added the codes and my comments
========================================



What is negative? Do you get the exact same error? What does your project file look like?

Yes, same error code. The project file looks like this:


#-------------------------------------------------
#
# Project created by QtCreator 2013-09-05T12:37:35
#
#-------------------------------------------------

QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = QTopenCV03
TEMPLATE = app

INCLUDEPATH += /usr/include/opencv2

LIBS += -L/usr/lib64
LIBS += -lopencv_core
LIBS += -lopencv_imgproc
LIBS += -lopencv_highgui
LIBS += -lopencv_features2d
LIBS += -lopencv_calib3d

SOURCES += main.cpp\
mainwindow.cpp

HEADERS += mainwindow.h

FORMS += mainwindow.ui




Read my advice again. It does not say remove the LIBS entry, it says you do not need the -L option that specifies the standard system path because the linker will search there anyway. The LIBS entry contains more than just the -L options, and these must stay. It does not say remove the INCLUDEPATH, it says the INCLUDEPATH does not need to be modified for OpenCV. There may be other things in INCLUDEPATH that need to stay.

Sorry but still not clear which parts should be removed.


ow your manual compilation command is implying that OpenCV is not installed where you were previously telling us, i.e. under/usr/local rather than /usr.

I tried both of them. Not working!

Anyway, today a friend helped me to solve it (thanks Urs). I would like to thank all members participated in this thread. It looks like the problem is with one of the images and the path for the images. Here is the content of the project file:


#-------------------------------------------------
#
# Project created by QtCreator 2013-09-05T12:37:35
#
#-------------------------------------------------

QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = QTopenCV03
TEMPLATE = app

INCLUDEPATH += /usr/local/include/opencv

LIBS += -L /usr/local/lib
LIBS += -lm
LIBS += -lopencv_core
LIBS += -lopencv_imgproc
LIBS += -lopencv_highgui
LIBS += -lopencv_objdetect
LIBS += -lopencv_calib3d

SOURCES += main.cpp\
mainwindow.cpp

HEADERS += mainwindow.h

FORMS += mainwindow.ui


and here is the contents of mainwindow.cpp



#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace cv;

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButton_clicked()
{

// IplImage* pImg = cvLoadImage("./HappyFish.jpg"); // this one works only if I change the project work directory value
IplImage* pImg = cvLoadImage("/home/ibr/projects/QTopenCV03/HappyFish.jpg"); // I should provide the complete path
if(pImg == NULL)
std::cout<< "Error loading image"<<std::endl; // no more error message
namedWindow( "Display window 1", 1 );// Create a window for display. // not needed I guess
cvShowImage("Image", pImg);

cvReleaseImage(&pImg);


std::cout<< "start **********"<<std::endl;
Mat image;
// image = imread("/home/ibr/projects/QTopenCV03/catd.jpg"); // This image does not work but I will check this later, maybe qtcreator does not like cats :-P
image = imread("/home/ibr/projects/QTopenCV03/HappyFish.jpg"); //works just fine

if( !image.data ) { std::cout<< "Error loading src again"<<std::endl; } // no more error message
namedWindow( "Display window 2", 1 );// Create a window for display. // not needed I guess
imshow( "Display window", image ); // Show our image inside it.


//waitKey(5000); // wait 5 seconds
waitKey(0); // Wait for a keystroke in the window
}



See you guys in another problem :-D

wysota
18th September 2013, 09:32
I don't think an incorrect path to an image can cause a function to be missing in a library... If you compare your two project files, you will notice totally different directories set in INCLUDEPATH and LIBS.

aakashsgoyal
7th August 2015, 15:53
The Problem has been solved.
Just modify the .pro file as the following:
QT -= gui

TARGET = myQtConsoleProject
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp

INCLUDEPATH += E:\opencv\build\include\

CONFIG(debug,debug|release){
LIBS += E:\opencv\build\x86\vc10\lib\opencv_core243d.lib
LIBS += E:\opencv\build\x86\vc10\lib\opencv_highgui243d.li b
LIBS += E:\opencv\build\x86\vc10\lib\opencv_imgproc243d.li b
LIBS += E:\opencv\build\x86\vc10\lib\opencv_features2d243d .lib
LIBS += E:\opencv\build\x86\vc10\lib\opencv_calib3d243d.li b
}

CONFIG(release,debug|release){
LIBS += E:\opencv\build\x86\vc10\lib\opencv_core243.lib
LIBS += E:\opencv\build\x86\vc10\lib\opencv_highgui243.lib
LIBS += E:\opencv\build\x86\vc10\lib\opencv_imgproc243.lib
LIBS += E:\opencv\build\x86\vc10\lib\opencv_features2d243. lib
LIBS += E:\opencv\build\x86\vc10\lib\opencv_calib3d243.lib
}

share this with all of you.

Thanks, this worked for me :)