1

Opencv functions like cv::imread() take as arguments only strings. So, if I write:

cv::Mat image = cv::imread("C:\\folder\\image.jpg");

everything is fine and it will load the image. But, if the path contains wide characters (for example greek letters):

wstring path = L"C:\\folder\\εικονα.jpg";

I can't just write:

cv::Mat image = cv::imread( path );

I already tried (and obviously failed) this:

cv::Mat image = cv::imread( string(path.begin(), path.end()) );

Is there any solution? Or I'll just have to leave opencv and use something else?

DimChtz
  • 4,043
  • 2
  • 21
  • 39

3 Answers3

2

It is possible to write/read images to/from wstring paths using std's filestreams and OpenCV's imdecode and imencode.

    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);
sluki
  • 524
  • 5
  • 15
0

The current answer is NO, as discussed in Issue #4292 of the official OpenCV repo.

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));
Community
  • 1
  • 1
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
0

Use std::filesystem from C++17 as a workaround

#include <filesystem>
std::filesystem::path path = L"C:\\folder\\εικονα.jpg";
cv::Mat image = cv::imread( path.string() );
Olppah
  • 790
  • 1
  • 10
  • 21