How to correctly read Mat images with special characters in filename

You might have noticed that trying to read images that have special characters (such as Ü, Ö or other non-Ascii characters) with imread function in  OpenCV will lead to crash. I noticed this while trying to open files with special Turkish characters (but I assume it is the same with Spanish, French, German or any other languages.) If you are using Qt, you are lucky because there is a workaround.

It’s simple, just follow these steps.

First read the file that has some strange characters in file name using a QFile.

Then convert the contents to a string (std::string)

After that convert you string to a byte vector (std::vector<byte>)

Finally you can use imdecode function to correctly decode that vector to a Mat image.

Here is how I actually did it:

QFile imfile(fname);;
std::string imbuff = imfile.readAll().toStdString();
std::vector<byte> vectordata(imbuff.begin(), imbuff.end());
cv::Mat mat_img(cv::imdecode(cv::Mat(vectordata, true), CV_LOAD_IMAGE_COLOR));

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.