Image Not Loading In OpenCV 4.x.x

asked 2019-11-03 11:09:13 -0500

faheyon007 gravatar image

updated 2019-11-05 09:27:04 -0500


This issue is still unresolved. I have also asked it in the Stack Overflow (here) but failed to get any solution. Thus, I have uploaded videos to explain the problem. So, Kindly take a look for clarification.

I am not sure if I am the only one facing this issue. But In my machine, images are not loading in OpenCV 4.x.x. I have tried 4.0.0 (prebuilt x64 Windows binaries), 4.1.1(prebuilt x64 Windows binaries) and 4.1.2(built x64 binaries from source using VS 2019).

Interestingly, the same code works for OpenCV 3.x.x.

Am I missing something?

#include <iostream>
#include <filesystem>

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

using namespace std;
using namespace cv;

int main(int argc, char* argv[])

    Mat image;
    string imagePath = filesystem::current_path().string() + "\\images\\road.jpg";

    cout << "Image Path : " << imagePath << "\n";

    image = imread(imagePath, IMREAD_COLOR);

    if (image.empty())
        cout << "ERROR READING IMAGE!";
        return -1;

    String windowTitle = "Hello OpenCV";
    namedWindow(windowTitle, WINDOW_AUTOSIZE);

    imshow(windowTitle, image);

    return 0;

Development Environment

  • Windows 10 64 Bit
  • Visual Studio 2019 Community Edition
edit retag flag offensive close merge delete


May be image is empty! You can try

Mat img(64,64,CV_8UC3,Scalar::all(64));

Mat img2 = imread("c:/tmp/test.jpg");
cout << img2.rows;

Can you read image? If yes path or file problem . If no ...

LBerger gravatar imageLBerger ( 2019-11-03 11:16:40 -0500 )edit

I'm sure you have googled and found problems where imread fails if the program and opencv library do not have matching build type - release or debug, so I'd check that

mvuori gravatar imagemvuori ( 2019-11-03 11:52:16 -0500 )edit

@LBerger, Yes, the image created by the above code can be read by imread(). But I do not think there is any path or file related problem. That exact same code works with OpenCV 3.x.x.

faheyon007 gravatar imagefaheyon007 ( 2019-11-03 20:55:03 -0500 )edit

@mvuori, I think there is no mismatch. The program runs fine except for the fact that for some reason, imread() is returning an empty Mat.

faheyon007 gravatar imagefaheyon007 ( 2019-11-03 21:15:21 -0500 )edit

Can you post road.jpg?

Can you give full path as c:/tmp/images/road.jpg ?

LBerger gravatar imageLBerger ( 2019-11-04 01:42:23 -0500 )edit

@LBerger The image is here.

The absolute path of the image is "E:\Development\opencv-c++-demo\opencv-c++-demo\images\road.jpg"

faheyon007 gravatar imagefaheyon007 ( 2019-11-04 03:30:35 -0500 )edit

There is no problem with this image using opencv 4.1.2-dev. Idea to solve your problem :

Move this image on c:\ try to read it.

try to read lena.jpg in opencv/samples/data


LBerger gravatar imageLBerger ( 2019-11-04 03:56:38 -0500 )edit

@LBerger tried but failed to get it to work. I think there is something else going on. Also, kindly check my videos.

faheyon007 gravatar imagefaheyon007 ( 2019-11-05 09:31:24 -0500 )edit