OpenCV加载Unicode路径图片的方法
acmilan2016/06/25软件综合 IP:四川

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]

来自:计算机科学 / 软件综合
0
已屏蔽 原因:{{ notice.reason }}已屏蔽
{{notice.noticeContent}}
~~空空如也

想参与大家的讨论?现在就 登录 或者 注册

所属专业
所属分类
上级专业
同级专业
acmilan
进士 学者 笔友
文章
461
回复
2934
学术分
4
2009/05/30注册,5年3个月前活动
暂无简介
主体类型:个人
所属领域:无
认证方式:邮箱
IP归属地:未同步
文件下载
加载中...
{{errorInfo}}
{{downloadWarning}}
你在 {{downloadTime}} 下载过当前文件。
文件名称:{{resource.defaultFile.name}}
下载次数:{{resource.hits}}
上传用户:{{uploader.username}}
所需积分:{{costScores}},{{holdScores}}下载当前附件免费{{description}}
积分不足,去充值
文件已丢失

当前账号的附件下载数量限制如下:
时段 个数
{{f.startingTime}}点 - {{f.endTime}}点 {{f.fileCount}}
视频暂不能访问,请登录试试
仅供内部学术交流或培训使用,请先保存到本地。本内容不代表科创观点,未经原作者同意,请勿转载。
音频暂不能访问,请登录试试
支持的图片格式:jpg, jpeg, png
插入公式
评论控制
加载中...
文号:{{pid}}
投诉或举报
加载中...
{{tip}}
请选择违规类型:
{{reason.type}}

空空如也

加载中...
详情
详情
推送到专栏从专栏移除
设为匿名取消匿名
查看作者
回复
只看作者
加入收藏取消收藏
收藏
取消收藏
折叠回复
置顶取消置顶
评学术分
鼓励
设为精选取消精选
管理提醒
编辑
通过审核
评论控制
退修或删除
历史版本
违规记录
投诉或举报
加入黑名单移除黑名单
查看IP
{{format('YYYY/MM/DD HH:mm:ss', toc)}}