Opencv-Python(一) 读取, 写入图片

来源:互联网 发布:如何做好客服数据分析 编辑:程序博客网 时间:2024/06/06 03:49

cv2.imread(filename, flags)

这个函数用来读取一副图像.
第一个参数(必须传)可以是图片的相对路径或者绝对路径(如果你第一个参数传错,程序不会报错,但是函数的返回值会是None).
第二个参数(可选)指定你要以何种方式读取图片,第二参数是个值它可以是:

  • cv2.IMREAD_COLOR:加载一张彩色图片,忽略它的透明度,在不传第二个参数时,它也是默认值.
  • cv2.IMREAD_GRAYSCALE:加载灰度图.
  • cv2.IMREAD_UNCHANGED:加载一张图片包含它的alpha通道(透明度),就是原图像不做改变的加载.
    可以简单的用1,0,-1分别代替三个值.
# read in one imageimg = cv2.imread('1.jpg')

将图像文件以数组的形式读入到变量img中。

cv2.imshow(winname, mat)

这个函数用来在制定窗口中显示指定的图像,如果这个窗口不存在,将新建这个窗口.
第一个参数为窗口名称,需要加单引号,每个窗口名称不同.
第二个参数为存储要显示的图片的变量.

# display the image in the window image1# the window's size is the size of image and can not be changedcv2.imshow('image1', img)

显示刚才读入的图像。

cv2.nameWindow(winname, flags)

用来新建一个窗口.
第一个参数为窗口名称.
第二个参数为窗口类型,具体可以使用的类型如下:

参数值 作用 cv2.WINDOW_NORMAL 用户可以调整窗口的大小,也可以将一个窗口从全屏窗口切换到普通窗口 cv2.WINDOW_AUTOSIZE 用户不能改变窗口的大小,窗口的大小被所展示的图片所约束 cv2.WINDOW_OPENGL opengl支持的窗口 cv2.WINDOW_FULLSCREEN 将窗口设置为全屏 cv2.WINDOW_FREERATIO 扩展图片不考虑图片的分辨率 cv2.WINDOW_KEEPRATIO 扩展图片但考虑图片的分辨率 cv2.WINDOW_GUI_EXPANDED 带进度条和工具条 cv2.WINDOW_GUI_NORMAL 旧方法
# open a new window ,the second parameter means ths size of the window can changecv2.namedWindow('NewWindow', cv2.WINDOW_NORMAL)# show the image in the opened windowcv2.imshow('NewWindow', img)

cv2.imwrite(filename, img, params)

用来将一个图片写入硬盘。
第一个参数是想要保存的图片的名称,
第二个参数是想要保存的图片存储在的变量,
第三个参数是一些附加参数(可选)。

  • 对于JPEG,其表示的是图像的质量,用0 - 100的整数表示,默认95;
  • 对于png ,第三个参数表示的是压缩级别。默认为3.
    注意:
  • cv2.IMWRITE_JPEG_QUALITY类型为 long ,必须转换成 int
  • cv2.IMWRITE_PNG_COMPRESSION, 从0到9 压缩级别越高图像越小。
# write the image into the diskcv2.imwrite('image1.jpg', img1)cv2.imwrite('1.png',img, [int( cv2.IMWRITE_JPEG_QUALITY), 95])cv2.imwrite('1.png',img, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])

将数组img1中保存的图片存入image1.jpg中.

cv2.destoryAllWindows()

代表销毁所有Opencv打开的窗口.
cv2.destoryWindow(window’s name)
用来销毁指定的窗口

# destory one windowcv2.destoryWindow(newWindow)# destory all opened windowscv2.destoryAllWindows()

需要注意的点

Opencv中读入彩色图像之后默认的通道顺序是:BGR(历史遗留问题)
很多日常使用的图像处理工具默认是RGB(例如matplotlib.pyplot),如果直接将Opencv读入的数组放到其它工具中进行处理很容易出错,一定要注意这个细节
下面给出一些将Opencv读入的BGR图像转换为RGB图像的方法:

# pay attention opencv use the BGR color model normally we use RGB color model# some ways of change BGR into RGB# img is the matrix created by Opencv using BGR model# other imgs are convert to RBG modelb, g, r = cv2.split(img)img2 = cv2.merge([r, g, b])img3 = img[:, :, ::-1]img4 = img[..., ::-1]img5 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

使用工具展示图像的时候一定要注意其使用的通道,不然显示的图像可能会出错。使用matplotlib.pyplot展示刚才那些处理后和没处理的图像结果如下:

plt.subplot(231); plt.imshow(img)    # expects distorted colorplt.subplot(232); plt.imshow(img2)   # expect true colorplt.subplot(233); plt.imshow(img3)   # expect true colorplt.subplot(234); plt.imshow(img4)   # expect true colorplt.subplot(235); plt.imshow(img5)   # expect true colorplt.show()

这里写图片描述

完整过程,使用的全部代码如下。

# -*- coding: utf-8 -*-# =================================================================# windows10, PyCharm, anaconda2, python 2.7.13, opencv 2.4.13# 2017-12-17# powered by tuzixini# attention: you might need install the encoder fist, like x264vfw# =================================================================import cv2  # import opencvimport matplotlib.pyplot as plt# read in one imageimg = cv2.imread('1.jpg')# display the image in the window image1# the window's size is the size of image and can not be changedcv2.imshow('image1', img)# open a new window ,the second parameter means ths size of the window can changecv2.namedWindow('NewWindow', cv2.WINDOW_NORMAL)# show the image in the opened windowcv2.imshow('NewWindow', img)# processing the imageimg1 = img + 100cv2.imshow('newImage', img1)# write the image into the diskcv2.imwrite('image1.jpg', img1)# pay attention opencv use the BGR color model normally we use RGB color model# some ways of change BGR into RGB# img is the matrix created by Opencv using BGR model# other imgs are convert to RBG modelb, g, r = cv2.split(img)img2 = cv2.merge([r, g, b])img3 = img[:, :, ::-1]img4 = img[..., ::-1]img5 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)plt.subplot(231); plt.imshow(img)    # expects distorted colorplt.subplot(232); plt.imshow(img2)   # expect true colorplt.subplot(233); plt.imshow(img3)   # expect true colorplt.subplot(234); plt.imshow(img4)   # expect true colorplt.subplot(235); plt.imshow(img5)   # expect true colorplt.show()# wait one key input and close the opened opencv windowscv2.waitKey()cv2.destroyAllWindows()