Opencv 函数和宽字符串
Opencv functions and wide strings
Opencv
类似 cv::imread()
的函数只接受 strings
作为参数。所以,如果我写:
cv::Mat image = cv::imread("C:\folder\image.jpg");
一切正常,它将加载图像。但是,如果路径包含宽字符(例如希腊字母):
wstring path = L"C:\folder\εικονα.jpg";
我不能只写:
cv::Mat image = cv::imread( path );
我已经尝试过(显然失败了):
cv::Mat image = cv::imread( string(path.begin(), path.end()) );
有什么解决办法吗?或者我只需要离开 opencv
并使用其他东西?
目前的答案是 NO,如官方 OpenCV 存储库 Issue #4292 中所述。
One possible workaround for now using Boost and a memory mapped file:
mapped_file map(path(L"filename"), ios::in);
Mat file(1, numeric_cast<int>(map.size()), CV_8S, const_cast<char*>(map.const_data()), CV_AUTOSTEP);
Mat image(imdecode(file, 1));
可以使用 stds 文件流和 OpenCV imdecode 和 imencode write/read 图像 to/from wstring 路径。
wstring path = getPath();
size_t size = getFileSize(path);
vector<uchar> buffer(size);
ifstream ifs(path, ios::in | ios::binary);
ifs.read(reinterpret_cast<char*>(&buffer[0]), size);
Mat image = imdecode(buffer, flags);
Opencv
类似 cv::imread()
的函数只接受 strings
作为参数。所以,如果我写:
cv::Mat image = cv::imread("C:\folder\image.jpg");
一切正常,它将加载图像。但是,如果路径包含宽字符(例如希腊字母):
wstring path = L"C:\folder\εικονα.jpg";
我不能只写:
cv::Mat image = cv::imread( path );
我已经尝试过(显然失败了):
cv::Mat image = cv::imread( string(path.begin(), path.end()) );
有什么解决办法吗?或者我只需要离开 opencv
并使用其他东西?
目前的答案是 NO,如官方 OpenCV 存储库 Issue #4292 中所述。
One possible workaround for now using Boost and a memory mapped file:
mapped_file map(path(L"filename"), ios::in); Mat file(1, numeric_cast<int>(map.size()), CV_8S, const_cast<char*>(map.const_data()), CV_AUTOSTEP); Mat image(imdecode(file, 1));
可以使用 stds 文件流和 OpenCV imdecode 和 imencode write/read 图像 to/from wstring 路径。
wstring path = getPath();
size_t size = getFileSize(path);
vector<uchar> buffer(size);
ifstream ifs(path, ios::in | ios::binary);
ifs.read(reinterpret_cast<char*>(&buffer[0]), size);
Mat image = imdecode(buffer, flags);