Search code examples
opencvimread

OpenCV imread() is not working


I have this simple code to open an image but it shows blank image in the "Video" window. I have checked release and debug lib's to make sure. It is in release mode and the lib is release lib. The picture is there, and I also tried full path for the image but still doesn't work. Also tried different image formats: jpg, png, bmp. It is OpenCV 3.4.1 release, and VS 2017 (also tried 2015).

#include "stdafx.h"
#include <opencv2/opencv.hpp>
#include <iostream>


using namespace std;
using namespace cv;

int main()
{
    namedWindow("Video");

    Mat frame1 = imread("Penguins.bmp", CV_LOAD_IMAGE_COLOR ); 

    imshow("Video", frame1);

    system("Pause");

    return 0;


}

Thanks in advance


Solution

  • You need to use the waitKey() function after imshow(). You can pass in a delay parameter to show the image for a certain time (in milliseconds).

    Example:

    #--- It will display the image for 30 ms 
    waitKey(30);
    
    #--- It will display the image until a key is pressed
    waitKey();
    

    Check THIS PAGE for more.

    There is also a demo example shown HERE