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

cv2.imread(filename, flags)

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

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

显示刚才读入的图像。

cv2.nameWindow(winname, flags)

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

参数值作用
cv2.WINDOW_NORMAL用户可以调整窗口的大小,也可以将一个窗口从全屏窗口切换到普通窗口
cv2.WINDOW_AUTOSIZE用户不能改变窗口的大小,窗口的大小被所展示的图片所约束
cv2.WINDOW_OPENGLopengl支持的窗口
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 change
cv2.namedWindow('NewWindow', cv2.WINDOW_NORMAL)
# show the image in the opened window
cv2.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 disk
cv2.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 window
cv2.destoryWindow(newWindow)
# destory all opened windows
cv2.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 model
b, 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 color
plt.subplot(232); plt.imshow(img2)   # expect true color
plt.subplot(233); plt.imshow(img3)   # expect true color
plt.subplot(234); plt.imshow(img4)   # expect true color
plt.subplot(235); plt.imshow(img5)   # expect true color
plt.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 opencv
import matplotlib.pyplot as plt
# read in one image
img = cv2.imread('1.jpg')
# display the image in the window image1
# the window's size is the size of image and can not be changed
cv2.imshow('image1', img)
# open a new window ,the second parameter means ths size of the window can change
cv2.namedWindow('NewWindow', cv2.WINDOW_NORMAL)
# show the image in the opened window
cv2.imshow('NewWindow', img)
# processing the image
img1 = img + 100
cv2.imshow('newImage', img1)
# write the image into the disk
cv2.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 model
b, 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 color
plt.subplot(232); plt.imshow(img2)   # expect true color
plt.subplot(233); plt.imshow(img3)   # expect true color
plt.subplot(234); plt.imshow(img4)   # expect true color
plt.subplot(235); plt.imshow(img5)   # expect true color
plt.show()
# wait one key input and close the opened opencv windows
cv2.waitKey()
cv2.destroyAllWindows()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值