问题
We're working on a project using OpenCV 2.4.6 and Qt 5.1.1 in C++. We have to load images for image processing at several points in our code, which we did using cv::imread
, as normal. However, we wanted to make software compatible with other language filesystems, and found that having file paths with foreign characters would fail to load.
The problem, we believe, has to do with the fact that imread
can only take in a std::string
(or char*
), and casting a path with non Latin-1 symbols to a std::string
results in characters that use multiple bytes in UTF-8 (the encoding used for QString
, which is how we store the paths) being converted to multiple chars.
To confirm that the file paths are valid, we've opened them by passing a wstring
to a regular ifstream
, which successfully opens the file and reads bits. Our current hack is to load the image as a QImage
and then copy the data to a cv::Mat
, but this isn't a satisfying solution, for multiple reasons, chiefly that as we understand it, Qt::QImage
loads images in 8bit format, and our images are of a higher bit depth.
Is there any "clean" way to get around this? I saw this questions, but toAscii
is deprecated, and its replacements didn't work for us. We've tried the following ways of converting the QString
to a std::string
and passing them to imread
.
QString::toStdString()
, QString::toUtf8().data()
, QString::toLocal8Bit().data()
, QString::toLatin1().data()
. They all appear to yield roughly the same results.
Thanks in advance.
回答1:
You can try QString::toStdWString()
and then convert the resulting std::wstring
to std::string
.
来源:https://stackoverflow.com/questions/19896142/opencv-imread-with-foreign-characters