Search code examples
c++stringvisual-studioopencv

OpenCV won't read the image even though the path is correct, string appears empty (imread_(''): can't open/read file: check file path/integrity)


I'm trying to learn opencv in c++ as i will be required to use it very soon. The problem i'm facing is exactly as the title says: opencv won't open (cv::imread()) any image even though the path is correct.

When i use cv::imread(path) i get the error

[ WARN:[email protected]] global loadsave.cpp:241 cv::findDecoder imread_(''): can't open/read file: check file path/integrity

I have tried using /, //, \\\\ \\. I tried using the absolute path and the relative path. I also have tried opening different images in different folders and in differents disks as well (c:/ and d:/). I also have tried updating my opencv installation.

I'm now using OpenCV 4.10 and am using Visual Studio 2022.

This is the code i am trying to run:

#pragma once
#include <opencv2/opencv.hpp>
#include <iostream>
#include <filesystem>
#include <windows.h>
#include <shobjidl.h>

int main() {

    cv::String filepath("D:/repos/faculdade/dataset/AnaCedran60dias.jpg");

    cv::Mat cvimg = cv::imread(filepath, 0);

    std::cout << "Path:\n" << filepath << std::endl;

    if(cvimg.empty())
        std::cout << "img wasnt read" << std::endl;
    
    cv::waitKey(0);
}

Solution

  • So what solved my problem was changing the build profile to "Release". For some reason building it with the "Debug" profile caused the string to be seen as empty, even though it wasn't.