0

i am reading the Learning CV book, i came across the first example and encounter this problem

Using OPENCV 3.0.0 and VS 2013, all libraries added and checked.

the code is as follows

 #include "opencv2/highgui/highgui.hpp"

int main( int argc, char** argv)
{
    IplImage* img = cvLoadImage(argv[1]);
    cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE);
    cvShowImage("Example1", img);
    cvWaitKey(0);
    cvReleaseImage(&img);
    cvDestroyWindow("Example1");
}   

So after compiling or build, I got a window named Example1, and it is grey, no image in the window.

Is this correct? Or what should I expect to get?

  • You provided the proper path to the image you're trying to load as a command-line argument in your debug-configuration, *right* ? Your example is about as stock as you can get: [nearly verbatim](http://dasl.mem.drexel.edu/~noahKuntz/openCVTut1.html) to the tutorial online. – WhozCraig Aug 02 '15 at 06:49
  • Hi, thanks for you speedy response. I don't think I do... The difference between my code and the link you gave me is this line, 'cvLoadImage(argv[1])‘ . how do i specify the path to image using my code? – bearbearsocute Aug 02 '15 at 07:02

2 Answers2

1

You are not loading the image correctly, i.e. argv[1] has an invalid path. You can check this like:

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

int main(int argc, char** argv)
{
    IplImage* img = cvLoadImage(argv[1]);
    //IplImage* img = cvLoadImage("path_to_image");

    if (!img)
    {
        std::cout << "Image not loaded";
        return -1;
    }

    cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE);
    cvShowImage("Example1", img);
    cvWaitKey(0);
    cvReleaseImage(&img);
    cvDestroyWindow("Example1");
}

You can supply the path also directly in the code like:

IplImage* img = cvLoadImage("path_to_image");

You can refer here to know why your path may be wrong.

You also shouldn't use old C syntax, but use the C++ syntax. Your example will be like:

#include <opencv2\opencv.hpp>
#include <iostream>
using namespace cv;

int main()
{
    Mat3b img = imread("path_to_image");

    if (!img.data)
    {
        std::cout << "Image not loaded";
        return -1;
    }

    imshow("img", img);
    waitKey();
    return 0;
}

You can refer to this answer to know how to setup Visual Studio correctly.

Community
  • 1
  • 1
Miki
  • 40,887
  • 13
  • 123
  • 202
0

Its not clear if the image is being loaded. OpenCV will silently fail if it can't find the image.

Try

    auto img= cv::imread(name, CV_LOAD_IMAGE_ANYDEPTH);
    if (img.data() == nullptr)
    {
        std::cout << "Failed to load image" << std::endl;
    }
Mikhail
  • 7,749
  • 11
  • 62
  • 136