简体   繁体   中英

unable to load picture with opencv with vs2013

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 are not loading the image correctly, ie 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM