opencv数据的读取

imread

函数的作用是从文件中读取一张图片。

python调用语法:

cv2.imread(filename[, flags]) → retval

参数说明:

  • filename:需要载入的文件名
  • flags:指定载入图片的颜色类型
    >0时返回三通道的彩色图片
    =0时返回灰度图
    <0时Return the loaded image as is. Note that in the current implementation the alpha channel, if any, is stripped from the output image. For example, a 4-channel RGBA image is loaded as RGB if flags ≥ \ge 0 .

返回值:

返回(高度、宽度、通道数)的元组。

如果当前图片无法读取(由于文件缺失、权限不够、不支持的或非法的数据格式),那么这个函数会返回一个空的矩阵。目前函数支持如下的文件格式:

  • Windows bitmaps:即*.bmp*.dib
  • JPEG文件:即*.jpeg*.jpg*.jpe
  • 待补充

函数返回的数组为(height,width,channel),且返回的RGB图片三个通道顺序为BGR,所以如果想要RGB格式的图片,可以采用如下的方式:

RGB_picture=cv2.imread(image_path)[...,::-1]

http://www.opencv.org.cn/opencvdoc/2.3.2/html/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imread#cv2.imread

imwrite

python函数原型:

cv.imwrite(	filename, img[, params]	) ->	retval

这个函数将图片存储到当前工作路径下指定的文件中。图片格式由filename后缀决定。通常来讲,only 8-bit single-channel or 3-channel (with ‘BGR’ channel order) images can be saved using this function, with these exceptions:

在这里插入图片描述
如果文件格式不被支持,那么图片将会被转化为8-bit unsigned (CV_8U)并且按照这种方式进行存储。

如果the format, depth or channel order is different, use Mat::convertTo and cv::cvtColor to convert it before saving. Or, use the universal FileStorage I/O functions to save the image to XML or YAML format.

参数:

  • filename:文件名
  • img: (Mat or vector of Mat) Image or Images to be saved.
  • params:Format-specific parameters encoded as pairs (paramId_1, paramValue_1, paramId_2, paramValue_2, … .) see cv::ImwriteFlags

返回值:

如果图片成功保存,返回True。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值