Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Unable to load images with imread in Visual Studio 2019

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