Ask Your Question
0

image display with imread not functioning

asked 2013-10-26 11:58:42 -0600

Lupin gravatar image

Hi everybody. I am new to OpenCV, I was following the material here link text Here is my code

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

using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
 char* imageName = argv[1];

 Mat image;

 image = cvLoadImage("D:\\b.png",1);
 if( argc != 2  )
 {
     cout <<  "Could not open or find the image" << std::endl ;


   return -1;
 }

 Mat gray_image;
 cvtColor( image, gray_image, CV_BGR2GRAY );

 imwrite( "D:\\C++\\output.jpg", gray_image);

 namedWindow( imageName, CV_WINDOW_AUTOSIZE );
 namedWindow( "Gray image", CV_WINDOW_AUTOSIZE );

 imshow( imageName, image );
 imshow( "Gray image", gray_image );



 return 0;
}

Everything is working, I open a png image, convert it to gray scale and save it to a jpg image. It functions well, however, this error appear:

Could not open or find the image

RUN FAILED (exit value -1, total time: 1s)

I am using Netbean 7.4 on windows 8.1 64 bit.

Strange is that the image is successfully processed and saved, but not display and the error appears.

edit retag flag offensive close merge delete

Comments

try not to mix the old c api and the c++ api, use image = imread("D:\b.png",1);

instead.

and then, replace

 if ( argc != 2  ) // you'll facepalm, if you understand, what is actually checked here...

with

if ( image.empty() )
berak gravatar imageberak ( 2013-10-26 12:33:56 -0600 )edit

I just did that, no thing changed. The same errors happen. Having no clue why... any suggestions, please ? :D

Lupin gravatar imageLupin ( 2013-10-26 12:57:29 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-10-26 13:20:56 -0600

sodeq gravatar image

what does this line for?

char* imageName = argv[1];

then, for cvLoadImage() function, why don't you just pass the second argument of progrm calls? (the argv[1]?

Have you try hard debugging? Examining line by line?

edit flag offensive delete link more

Comments

It debugged successfully. Build in was nice. Just that annoying error. What else should I do, please?

p.s: char* imageName =argv[1] was to take the name of the image. What do you mean by pass the second argument? I do not get it clearly, Do you mind please give some more explanation?

Lupin gravatar imageLupin ( 2013-10-26 14:45:14 -0600 )edit

as seen in your code, you use the var "imageName" as window name, not for the image source. Maybe you could try cvLoadImage(argv[1], 1). If the problem persist, i guess this out of my current understanding.

sodeq gravatar imagesodeq ( 2013-10-27 03:10:13 -0600 )edit
1

I found the problem. It is not related to the code, the code is all right. Just my image. The image suppose to be RGB image and in my case it is not. I changed it to several other different ones and it is o.k.

Thanks for your attention

Lupin gravatar imageLupin ( 2013-10-27 06:52:25 -0600 )edit

please mark as answered, Great! lol

sodeq gravatar imagesodeq ( 2013-10-28 11:13:26 -0600 )edit

Question Tools

Stats

Asked: 2013-10-26 11:58:42 -0600

Seen: 567 times

Last updated: Oct 26 '13