0

I have a function that is something like this:

void OpenPattern(std::string filename, bool type){ Mat img = cv::imread(filename, cv::IMREAD_COLOR); if(img.empty()){ printf("CANNOT OPEN IMAGE"); }else{ doSomethingElse(); } } 

the code compiles alright, but when I run it, img is always empty. I've tested filename storing a relative path and a full path to the image. Neither worked here. I printed the filename content every time and it matched with the image address perfectly. It only worked when I tested like this:

void OpenPattern(std::string filename, bool type){ filename="path to image" //worked with relative and full path Mat img = cv::imread(filename, cv::IMREAD_COLOR); //or //Mat img = cv::imread("path to image", cv::IMREAD_COLOR); if(img.empty()){ printf("CANNOT OPEN IMAGE"); }else{ doSomethingElse(); } } 

I think many people have asked something similar to this (like this one here imread() won't read string variable in c++), but I haven't found an answer yet.

Does anyone know why the imread() simply can't open when the filename is a function argument? Or does anyone know if there is a way to investigate where imread decides to return the NULL Mat, so I can better investigate what is happening?

EDIT:

  1. "path to image" and filename are 100% equal. In fact, in one of my tests, I used imwrite(filename, a) to save a image with the same name as the one I wanted to open and it worked.
  2. I tried all variations in filename: using /, \, relative path, absolute path. 3.This code was supposed to be a refactored version of another one. The older version does open the image correctly, but I have no idea why this one doesn't.

Thanks a lot!

3
  • The "path to image" is 100% the same as the filename? Commented May 20, 2021 at 2:16
  • Yes, it is. When I printed them, they were the same, @Ema. That is why I got confused with this bug. In another code I do almost the same thing and it works perfectly. Commented May 20, 2021 at 10:04
  • @Ema you found out. there was a invisible '\r' in the string that was making imread() deliver a NULL Mat. Commented May 20, 2021 at 17:33

2 Answers 2

0

I don't know OpenCV, but maybe this will help. These are problems I encountered with other libraries and languages.

Make sure the path is properly formatted. Have you escaped the slashes and other special characters? Have you tried forward and backward slashes? Do you have spaces in it, while not supported? Did you try the path with and without starting with a slash?

Sign up to request clarification or add additional context in comments.

3 Comments

Yes. All of this was tested because it was the first thing I suspected. I'm using forward slashes for folders (I tried double back slashes too).
@pauloffsf Just guessing, but does the program have the authority to read the file and isn't it used by anything else? Otherwise you'll have to walk through the code. Looking up the source code <opencv2/imgcodecs.hpp> shows there are a lot of exceptions in imread. See which one is triggered.
I changed all file's permission to read and write. no success... Thanks for the try-catch idea to see what is happening in imread... I'll try that.
0

I found that the string passed in the argument of the function had an invisible '\r' that was making imread() miss the file.

I tried comparing size of the argument string with the written string "path to image".

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.