使用 Imread 打开具有 Unicode 名称的图像文件
open image files with unicode names using imread
我需要读取带有Unicode名称的图像文件,但是openCV函数imread的图像名称参数仅支持字符串。 如何将我的 Unicode 路径保存到字符串对象。有什么解决方案吗?
您可以:
- 使用
ifstream
打开文件 , - 一
std::vector<uchar>
读完, - 用
cv::imdecode
解码它。
请参阅下面的示例,该示例使用 ifstream
加载到具有 Unicode 文件名的图像img2
:
#include <opencv2opencv.hpp>
#include <vector>
#include <fstream>
using namespace cv;
using namespace std;
int main()
{
// This doesn't work with Unicode characters
Mat img = imread("D:\SO\img\æbärnɃ.jpg");
if (img.empty()) {
cout << "Doesn't work with Unicode filenamesn";
}
else {
cout << "Work with Unicode filenamesn";
imshow("Unicode with imread", img);
}
// This WORKS with Unicode characters
// This is a wide string!!!
wstring name = L"D:\SO\img\æbärnɃ.jpg";
// Open the file with Unicode name
ifstream f(name, iostream::binary);
// Get its size
filebuf* pbuf = f.rdbuf();
size_t size = pbuf->pubseekoff(0, f.end, f.in);
pbuf->pubseekpos(0, f.in);
// Put it in a vector
vector<uchar> buffer(size);
pbuf->sgetn((char*)buffer.data(), size);
// Decode the vector
Mat img2 = imdecode(buffer, IMREAD_COLOR);
if (img2.empty()) {
cout << "Doesn't work with Unicode filenamesn";
}
else {
cout << "Work with Unicode filenamesn";
imshow("Unicode with fstream", img2);
}
waitKey();
return 0;
}
如果您使用的是Qt,则可以使用QFile
和QString
更方便地执行此操作,因为QString
本机处理Unicode字符,并且QFile
提供了一种简单的文件大小方法:
QString name = "path/to/unicode_img";
QFile file(name);
file.open(QFile::ReadOnly);
qint64 sz = file.size();
std::vector<uchar> buf(sz);
file.read((char*)buf.data(), sz);
cv::Mat3b img = cv::imdecode(buf, cv::IMREAD_COLOR);
为了完整起见,在这里您可以看到如何在 Python 中执行此操作
相关文章:
- 在qt窗口中具有图像,而无需将图像文件本身作为源
- 如何从所选目录(位于listWidget中)中筛选图像文件
- 如何将图像文件夹添加到CMake项目以便在c ++可执行文件中使用这些图像?
- 解码并保存 base64 C++中的图像文件
- BASE64图像文件用C 编码
- 如何在使用 c++/COM 发送时在电子邮件中插入/嵌入图像文件(.png)
- 如何使用Imebra库将压缩像素(用我自己的编码器压缩)回到DICOM图像文件中
- 使用 Imread 打开具有 Unicode 名称的图像文件
- 如何使用stb_image将像素颜色数据写入 BMP 图像文件?
- C 读取/写作PPM图像文件灰色图像
- 如何在C 中删除或删除.jpg和.dpx图像文件
- C++图像处理 - 将图像文件读入 2D 数组
- 将图像文件(.BMP)复制到其他位置
- 从位图图像文件读取像素数据值
- Boost:Asio.从服务器下载图像文件
- 打开不同的图像文件,并使用GDI 库在主应用程序窗口的背景上绘制它们
- 使用 QPixmap 和 QLabel 在 Qt Creator 中以编程方式添加图像文件 (.PNG)
- C++:将整数的文本文件转换为BMP格式的位图图像文件
- 读取/写入PPM图像文件C++
- 将C++图像文件转换为数组