opencv imread() on Windows for non-ASCII file names

后端 未结 4 1315
清歌不尽
清歌不尽 2020-12-10 04:55

We have an OpenCV problem of opening (and writing) file paths that contain non-Ascii characters on Windows. I saw questions OpenCV imread with foreign characters and imread

4条回答
  •  一整个雨季
    2020-12-10 05:26

    Try this:

    cv::Mat ReadImage(const wchar_t* filename)
    {
        FILE* fp = _wfopen(filename, L"rb");
        if (!fp)
        {
            return Mat::zeros(1, 1, CV_8U);
        }
        fseek(fp, 0, SEEK_END);
        long sz = ftell(fp);
        char* buf = new char[sz];
        fseek(fp, 0, SEEK_SET);
        long n = fread(buf, 1, sz, fp);
        _InputArray arr(buf, sz);
        Mat img = imdecode(arr, CV_LOAD_IMAGE_COLOR);
        delete[] buf;
        fclose(fp);
        return img;
    }
    

提交回复
热议问题