OpenCV的imread和imwrite有一个问题,就是在Windows平台不支持国际字符路径,并且作者打死也不改。
不过可能也是提问题的人比较多,很早就加了imdecode和imencode两个函数。而且实现也是非常蛋疼的,imdecode是先写到临时文件里再读出解码,imencode是先编码到临时文件再读出。
不过由于Windows使用GetTempPath+GetTempFileName、tmpnam等API产生的临时文件路径都是ANSI/OEM兼容的8.3短文件名,因此一般不会出错,也算是解决了这一问题。
通过支持Unicode文件名的_wfopen配合imdecode和imencode,可以解决imread和imwrite不支持Unicode路径的问题。
读取图像:_wfopen打开文件->fread读取字节->fclose->imdecode解码字节
写入图像:imencode获取字节->_wfopen创建文件->fwrite写入字节->fclose
XXXXXXXXXXXXXXXXXXXXXXXXX/question/103013/imwrite-with-unicode-path-windows/
XXXXXXXXXXXXXXXXXXXXXX/2.4/modules/highgui/doc/reading_and_writing_images_and_XXXXXXXXml
<code class="language-txt">Reading and Writing Images and Video imdecode Reads an image from a buffer in memory. C++: Mat imdecode(InputArray buf, int flags) C++: Mat imdecode(InputArray buf, int flags, Mat* dst) C: IplImage* cvDecodeImage(const CvMat* buf, int iscolor=CV_LOAD_IMAGE_COLOR) C: CvMat* cvDecodeImageM(const CvMat* buf, int iscolor=CV_LOAD_IMAGE_COLOR) Python: cv2.imdecode(buf, flags) → retval Parameters: buf – Input array or vector of bytes. flags – The same flags as in imread() . dst – The optional output placeholder for the decoded matrix. It can save the image reallocations when the function is called repeatedly for images of the same size. The function reads an image from the specified buffer in the memory. If the buffer is too short or contains invalid data, the empty matrix/image is returned. See imread() for the list of supported formats and flags description. Note In the case of color images, the decoded images will have the channels stored in B G R order. imencode Encodes an image into a memory buffer. C++: bool imencode(const string& ext, InputArray img, vector<uchar>& buf, const vector<int>& params=vector<int>()) C: CvMat* cvEncodeImage(const char* ext, const CvArr* image, const int* params=0 ) Python: cv2.imencode(ext, img[, params]) → retval, buf Parameters: ext – File extension that defines the output format. img – Image to be written. buf – Output buffer resized to fit the compressed image. params – Format-specific parameters. See imwrite() . The function compresses the image and stores it in the memory buffer that is resized to fit the result. See imwrite() for the list of supported formats and flags description. Note cvEncodeImage returns single-row matrix of type CV_8UC1 that contains encoded image as array of bytes. </int></int></uchar></code>
[修改于 7年3个月前 - 2017/03/05 05:17:14]
时段 | 个数 |
---|---|
{{f.startingTime}}点 - {{f.endTime}}点 | {{f.fileCount}} |