-1

I am new to opencv and I am starting to make a simple code to read and display image in gui ,I am working in qt IDE, first I wirte this block of code

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


int main()
{
   cv::Mat image=cv::imread("image.jpg");
   cv::namedWindow("My Image");
   cv::imshow("My Image",image);
   cv::waitKey(0);
   cv::destroyAllWindows();
   return 1;
 }

But it displays a white window and error in console and then display another window "not responsive" message and then stop working, This is a screen shot http://pbrd.co/1u2A0ow Then I wrote another validity code to check wheater or not the image is been read

int main()
{

Mat image;
cout<<"Size is"<<image.size().height<<","<<image.size().width<<endl;

image=imread("image.jpg");

//Checking first if the image have been read
if(!image.data)
{
    cout<<"\n No image has created \n"<<endl;
}

return 1;

}

It displays the message, which means that the image is not read,So The question is How can I successfully read and load image note: The image in the same folder of main.cpp file http://pbrd.co/1u2Bmj1

Barq
  • 3
  • 1
  • 6
  • Check is this file exist: `QFile file("image.jpg"); if(file.exists()) cout<<"\n exist \n"< – Jablonski Sep 18 '14 at 05:26
  • In the second code you call image.size().height before you have opened the image. This happens in the next line. So you can't get the correct height and width from your image. – retinotop Sep 18 '14 at 05:32
  • I copied the same line that display the size after openning the image and it displays the same size 0,0 ,actually I tried this before posting here – Barq Sep 18 '14 at 06:03
  • I have tried the code , it displays that the file is not exist,How can I overcome this? – Barq Sep 18 '14 at 06:10

2 Answers2

1

As you said following code showed you that file not exist:

QFile file("image.jpg");
     if(file.exists())
         cout<<"\n exist \n"<<endl;
     else
         cout<<"\n not exist \n"<<endl;

Solution:

First of all, try to set full path to your image. For some reasons Qt search your file in wrong place, so set full path.

For example:

cv::Mat image=cv::imread("G:\\2\\qt.jpg");
QFile file("G:\\2\\qt.jpg");
if(file.exists())
    cout<<"\n exist \n"<<endl;
else
    cout<<"\n not exist \n"<<endl;

Or UNIX style:

cv::Mat image=cv::imread("G:/2/qt.jpg");
QFile file("G:/2/qt.jpg");
if(file.exists())
    qDebug()<<"\n exist \n"<<endl;
else
     qDebug()<<"\n not exist \n"<<endl;
Jablonski
  • 18,083
  • 2
  • 46
  • 47
  • It works , Really thank you Chernoby , It also displayed on gui – Barq Sep 18 '14 at 06:30
  • Can I ask another question? What if I want to run this code in another computer ,What if for example I send the folder of app to you, sure it will not work because the folder of program in drive c: and the folder of image in drive D:\ , when I have tried to put image on the same folder it was not work ,How can I make a program to display image on any platform ? – Barq Sep 18 '14 at 08:53
  • @Barq to do this you should use your first path image.jpg BUT put this image near exe file and send it to user. And it will work because you app find it, it didn't work for you because you run app with QtCreator and qt creator search this image in other place(can be near exe or not) – Jablonski Sep 18 '14 at 09:42
0

it seems that the program does not know where the image is. Try to include main.cpp as one of the libraries that the program will use. That way, the program will be able to find and open the image.

Gerald
  • 1
  • 1