Ask Your Question
0

First Example Code - Runs well but Failed IMG

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

Robinus gravatar image

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

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);

cvWaitKey(0);


cvReleaseImage(&img);

cvDestroyWindow("Example1");

}

edit retag flag offensive close merge delete

Comments

2

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 -0600 )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 -0600 )edit
1

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

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

please have a look here as well.

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

1 answer

Sort by » oldest newest most voted
2

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

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() {
    namedWindow("mywindow");
    //
    // 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

Comments

1

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 -0600 )edit
1

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 -0600 )edit

Question Tools

Stats

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

Seen: 194 times

Last updated: Nov 04 '14