Why does the tutorial code from OpenCV give unspecified error?

I’m using VS2019.
I put “starry_night.jpg” in the Sources Files folder

Code from OpenCV: Getting Started with Images

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;

int main()
{
    std::string image_path = samples::findFile("starry_night.jpg");
    Mat img = imread(image_path, IMREAD_COLOR);
    if (img.empty())
    {
        std::cout << "Could not read the image: " << image_path << std::endl;
        return 1;
    }
    imshow("Display window", img);
    int k = waitKey(0); // Wait for a keystroke in the window
    if (k == 's')
    {
        imwrite("starry_night.png", img);
    }
    return 0;
}

In this command prompt gives this error:

[ WARN:0] global C:\build\master_winpack-build-win64-vc15\opencv\modules\core\src\utils\samples.cpp (61) cv::samples::findFile cv::samples::findFile('starry_night.jpg') => ''
OpenCV(4.5.3) Error: Unspecified error (OpenCV samples: Can't find required data file: starry_night.jpg) in cv::samples::findFile, file C:\build\master_winpack-build-win64-vc15\opencv\modules\core\src\utils\samples.cpp, line 64

I’m stumped. How do I decipher this error?

Perhaps I thought I put the picture in the wrong place. So I read documentation about findFile() at OpenCV: Utility functions for OpenCV samples.
But where does the relative_path start at?

Found the answer. Turns out, at least for the sample code, you also need the sources\samples\data folder with all the images.

1 Like