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]
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。