C++ opencv 如何在英文系统中保存中文路径文件
时间: 2024-10-23 10:07:25 浏览: 43
在 C++ 使用 OpenCV 时,在英文系统中保存包含中文路径的文件,你需要特别处理,因为默认情况下,系统可能会编码错误。一种常见的方法是使用宽字符(wchar_t)和相关的函数,如 `cv::imwrite()` 的宽字符版本 `cv::imwrite(wchar_t*, cv::Mat)`。
首先,确保正确设置你的工作目录到包含中文路径的位置,并且使用宽字符字符串库(如 `<windows.h>` 中的 `MultiByteToWideChar` 函数)来转换路径。例如:
```cpp
#include <Windows.h>
#include <opencv2/opencv.hpp>
std::wstring convertPath(const std::string& path) {
int len = MultiByteToWideChar(CP_UTF8, 0, path.c_str(), -1, NULL, 0);
std::vector<wchar_t> wideChars(len);
MultiByteToWideChar(CP_UTF8, 0, path.c_str(), -1, &wideChars[0], len);
return wideChars;
}
cv::Mat img; // 假设你已经有了一个 Mat 对象
std::string chinesePath = "中文路径/图片.jpg"; // 你的中文路径
std::wstring wpath = convertPath(chinesePath);
cv::imwrite(wpath.c_str(), img);
```
这里,我们先将UTF-8编码的中文路径转换为宽字符形式,然后传递给 `cv::imwrite` 函数。
阅读全文
相关推荐


















