Ask Your Question

First Example Code - Runs well but Failed IMG

asked 2014-11-04 12:42:03 -0500

Robinus gravatar image

updated 2014-11-04 12:45:01 -0500

C:\fakepath\2.JPGimage description

I am a newbie for CV. Mostly I wanted to try FAQ,

but it might be not an easy matter happening, just specific for me.

The code below should be too famous and you will be reading briefly.

I have attached the result IMG output.

For the first time, I was successful with Webcam runner,

but with the secondary IMG View Example, the ouput is just in grey,

which is just can't be figured out.

There seems not to be any problems in the code.

Have any idea?

 #include < opencv\cv.h > 

 #include < opencv\highgui.h >

using namespace cv;

int main(int argc, char* argv){

IplImage img = cvLoadImage(argv[1]);

cvNamedWindow ("Example1", 0);

cvResizeWindow ("Example1", 1024, 768);

cvShowImage("Example1", img);





edit retag flag offensive close merge delete



sorry for the downvote, please don't take that all too personal, but you should not use the outdated c-api in 2014 anymore

berak gravatar imageberak ( 2014-11-04 12:45:31 -0500 )edit

I am referring to "Learning OpenCV: Computer Vision in C++ with the OpenCV Library" - O'Reily. And I am a starter. Any other options? I will be appreciated with any advice from you. Thank you.

Robinus gravatar imageRobinus ( 2014-11-04 12:49:01 -0500 )edit

yea, still, a nice book, but you should prefer the code from opencv/samples/cpp

berak gravatar imageberak ( 2014-11-04 13:01:13 -0500 )edit

please have a look here as well.

berak gravatar imageberak ( 2014-11-04 13:21:29 -0500 )edit

1 answer

Sort by » oldest newest most voted

answered 2014-11-04 12:59:51 -0500

berak gravatar image

ok, let's try again (assuming, you're running opencv2.4.x):

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;

#include <iostream>
using namespace std;

int main() {
    // now here's the hidden challenge: it has to *find* the image file.
    // if you're using a relative path (like below, it assumes, 
    // that the image is in the folder, where your prog *starts*
    // that is not nessecarily the location of your binary )
    // when in doubt, try an *absolute path* like "/home/opencv/samples/cpp/lena.png"
    Mat img = imread("lena.png"); 
    if ( img.empty() ) {
        cerr << "sorry dave, i could not find the specified image" << endl;
        return -1;
    imshow("mywindow", img);
    waitKey(0); // forever

    return 0;
edit flag offensive delete link more



in case , you wonder, - the old c-api will just show an empty window, if the img was not found at the expected location, the c++ api will throw a exception in imshow() (the img was empty).

berak gravatar imageberak ( 2014-11-04 13:09:28 -0500 )edit

I have missed to reply. You have just helped me out to solve the hidden quest! It works fine, thank you again.

Robinus gravatar imageRobinus ( 2014-11-05 21:49:54 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2014-11-04 12:42:03 -0500

Seen: 80 times

Last updated: Nov 04 '14