Home > Enterprise >  Imread function of opencv can't open my images, what I'm doing wrong?
Imread function of opencv can't open my images, what I'm doing wrong?

Time:10-13

as the title suggest i have problems with opencv and in particulare with imread function. This is the simple program I'm trying to run

#include<iostream>
#include<opencv2/imgproc.hpp>
#include<opencv2/imgcodecs.hpp>
#include<opencv2/highgui.hpp>


using namespace cv;

int main(){
    std::string path = "Images/NewYork.jpg";
Mat img = imread(path);
imshow("image", img);
waitKey(0);
return 0;

}

My CMakeLists looks like this

cmake_minimum_required(VERSION 3.23)
project(Convolution)
find_package( OpenCV REQUIRED )
set(CMAKE_CXX_STANDARD 14)
include_directories( 
${OpenCV_INCLUDE_DIRS} )
add_executable(Convolution main.cpp)
target_link_libraries(Convolution 
${OpenCV_LIBS})

The message error that I'm getting is: [ WARN:[email protected]] global /opt/opencv/modules/imgcodecs/src/loadsave.cpp (244) findDecoder imread_('Images/NewYork.jpg'): can't open/read file: check file path/integrity terminate called after throwing an instance of 'cv::Exception'

what(): OpenCV(4.6.0-dev) /opt/opencv/modules/highgui/src/window.cpp:969: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'

I know that the second error is due to the fact that the Mat object is empty and that's becuase my imread function does'nt work. I already checked if my path is correct, i saved my image in different directories, i checked the spelling, i checked evertyhing and i can't make it work. Please help.

CodePudding user response:

You should always check whether your cv2.imread() succeeded or not by seeing if it returns NULL data.

You may be running inside an IDE, or from a different directory, so consider using getcwd() and printing the result so you know where the relative path to your Images directory starts.

CodePudding user response:

Try to put full path instaed of shortened path.

ex) "D://images//example.jpg"

CodePudding user response:

You path is still wrong most likely. Try with the absolute path first, then check what your working directory of the compiler actually is (default would be the same as your main.cpp).

If you want to use relative paths (which makes a lot of sense) i'd recommend a "./Images/NewYork.jpg" syntax to make that clearer.

  • Related