Visual Studio Cannot Locate jpeg File

73 Views Asked by At

I am trying to experiment with OpenCV's C++ Library. However, I encounter an issue when trying to read an image. Here is my code.

#include <iostream>
#include <fstream>
#include <filesystem>

#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/stitching.hpp>

using namespace std;
using namespace cv;


int main() {

    Mat image = imread("tiger.jpg");
    
    if (image.empty()) {
        cout << "No Image" << endl;
        return -1;
    }
    else {

        imshow("Test", image);
        waitKey(0);
        return 0;
    }

}

The "image" keeps evaluating as empty, implying that Visual Studio could not find my image. I have included the image into my project directory and have even resorted to using the absolute file path but nothing seems to work.

Project

This problem has started occurring very recently and beforehand I had no problem reading images. Any help would be appreciated.

Edit:

I have re-written my code to see if Visual Studio could actually locate the file.

int main() {

    std::filesystem::path imagePath = "C:\\Images\\flower.jpg";

    if (std::filesystem::exists(imagePath) && std::filesystem::is_regular_file(imagePath)) {
        cv::Mat image = cv::imread(imagePath.string());

        if (!image.empty()) {
            cv::imshow("My Image", image);
            cv::waitKey(0);
        }
        else {
            std::cerr << "Error: Could not read the image." << std::endl;
        }
    }
    else {
        std::cerr << "Image file does not exist or is not a regular file." << std::endl;
    }

    return 0;

}

It seems to be able to locate the file, but I am still getting the error that the image is empty. I have tried with multiple different images and image extensions. Nothing seems to work. This problem only seems to occur on my Windows desktop but not on my Mac.

0

There are 0 best solutions below