skip to Main Content

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);
}

2

Answers


  1. Chosen as BEST ANSWER

    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.


  2. The problem happens when you use imread() from a release-mode library in debug mode.

    In Debug, you need to use the lib with d: opencv_world4100d.lib

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search