saqib
saqib

Reputation: 11

Open cv is not reading image

Here is my piece of code. But every time it shows data cannot be read. I have tried multiple formates of writing path to image but that did not worked. Any one else facing same problem?

#include <iostream>
#include <fstream>
#include <string>
#include <opencv2/opencv.hpp>
#include <vector>
using namespace cv;
int main()
{
    Mat source = imread("C:\\Untitled.jpg");
    namedWindow("My Image");
    waitKey(0);
    if (!source.data)
    {
         std::cout << "Data cannot be read" << std::endl;
         return -1;
    }
    imshow("My Image", source);
    destroyAllWindows();
    return 0;
}

Upvotes: -1

Views: 347

Answers (1)

John Park
John Park

Reputation: 1774

imshow() must be followed by waitKey() or you can never see the named window.

imshow("My Image", source);
waitKey(0);  // should be here.
destroyAllWindows();

The note for imshow() in OpenCV 2.4 says:

This function should be followed by waitKey function which displays the image for specified milliseconds. Otherwise, it won’t display the image. ...

Upvotes: 1

Related Questions