OpenCV使用外来字符 [英] OpenCV imread with foreign characters
问题描述
我们正在使用C ++中的OpenCV 2.4.6和Qt 5.1.1项目。我们必须在我们的代码中的几个点加载图像进行图像处理,我们使用 cv :: imread
作为正常。然而,我们想让软件与其他语言文件系统兼容,并发现具有外部字符的文件路径将无法加载。
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.
我们认为,这个问题与 imread
只能接受 std :: string
(或 char *
),并将一个非拉丁1符号的路径转换为 std :: string
导致在UTF-8中使用多个字节的字符(用于 QString
的编码,这是我们如何存储路径)转换为多个字符。
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.
为了确认文件路径是否有效,我们通过将 wstring
code> ifstream ,它成功打开文件并读取位。我们当前的攻击是将图像加载为 QImage
,然后将数据复制到 cv :: Mat
,但这不是一个令人满意的解决方案,主要是因为我们理解它, Qt :: QImage
加载图像的8位格式,我们的图像是一个更高的位深度。
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.
有没有干净的方法来解决这个问题?我看到了此问题,但 toAscii
已弃用,它的替代品不为我们工作。我们已经尝试了以下方法将 QString
转换为 std :: string
并将它们传递给 imread
。
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()
。他们似乎产生大致相同的结果。
QString::toStdString()
, QString::toUtf8().data()
, QString::toLocal8Bit().data()
, QString::toLatin1().data()
. They all appear to yield roughly the same results.
提前感谢。
推荐答案
$ c> QString :: toStdWString()然后将结果 std :: wstring
转换为 std :: string
。
You can try QString::toStdWString()
and then convert the resulting std::wstring
to std::string
.
这篇关于OpenCV使用外来字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!