Ask Your Question
0

Unable to load images with imread in Visual Studio 2019

asked 2019-04-20 14:36:57 -0600

funkfuzz gravatar image

Hi guys,

I am trying to do the OpenCV tutorial " Load and Display an Image " however, when I run the code my image doesn't load and I get the "Could not open or find the image" - message. I have tried changing the path directory with the complete path to the image, but it doesn't help.

I am using OpenCV 4.1.0 and Visual Studio 2019. Any ideas what might be going wrong?

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>

#include <iostream>
#include <string>

using namespace cv;

using namespace std;

int main(int argc, char** argv)
{
    String imageName("Image002"); // by default
    if (argc > 1)
    {
        imageName = argv[1];
    }

    Mat image;

    image = imread(imageName, IMREAD_COLOR); // Read the file

    if (image.empty())                      // Check for invalid input
    {
        cout << "Could not open or find the image" << std::endl;
        return -1;
    }

    namedWindow("Display window", WINDOW_AUTOSIZE); // Create a window for display.

    imshow("Display window", image);                // Show our image inside it.

    waitKey(0); // Wait for a keystroke in the window
    return 0;
}
edit retag flag offensive close merge delete

Comments

1

Likely a path and/or extension issue. Try using an absolute path and check to see if the image file has an extension like .jpg, .png, etc. something like "c:/temp/image002.jpg"

Chris gravatar imageChris ( 2019-04-20 15:30:53 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-04-21 01:47:57 -0600

iamM gravatar image

updated 2019-04-21 01:48:25 -0600

Please add file extension and also image file should be passed as argument else it should be in output directory in your case.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-04-20 14:36:57 -0600

Seen: 1,840 times

Last updated: Apr 21 '19