【问题标题】:Opencv functions and wide stringsOpencv 函数和宽字符串
【发布时间】:2016-03-31 01:08:15
【问题描述】:

Opencvcv::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 并使用其他东西?

【问题讨论】:

    标签: c++ opencv widestring


    【解决方案1】:

    可以使用 std 的文件流和 OpenCV 的 imdecode 和 imencode 向 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);
    

    【讨论】:

      【解决方案2】:

      目前的答案是,正如官方 OpenCV 存储库的 Issue #4292 中所讨论的那样。

      目前使用 Boost 和内存映射文件的一种可能解决方法:

      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));
      

      【讨论】:

        猜你喜欢
        • 2011-02-11
        • 2015-07-10
        • 2019-03-12
        • 2021-10-28
        • 2012-07-16
        • 2020-08-24
        • 2015-04-02
        • 1970-01-01
        相关资源
        最近更新 更多