Python-Opencv入门函数学习(一)

转自博客https://www.jianshu.com/p/7fcfc6253c81。感谢作者整理。若侵权,告知即删。

opencv 简单入门

1、读取图片 imread(path)

Use the function cv2.imread() to read an image. The image should be in the working directory or a full path of image should be given.
Second argument is a flag which specifies the way image should be read.
cv2.IMREAD_COLOR : Loads a color image. Any transparency of image will be neglected. It is the default flag.
cv2.IMREAD_GRAYSCALE : Loads image in grayscale mode
cv2.IMREAD_UNCHANGED : Loads image as such including alpha channel
Instead of these three flags, you can simply pass integers 1, 0 or -1 respectively.

OpenCV目前支持读取bmp、jpg、png、tiff等常用格式。

通过上面的英文我们知道opencv提供了一个imread方法让我们去读取图片,该方法有两个参数:第一个参数是我们所要读取图片的路径, 第二个参数是图片读取的方式,如IMREAD_COLOR 表示加载彩色图片(默认),IMREAD_GRAYSCALE以黑白方式加载图片,IMREAD_UNCHANGED原图加载含alpha channel信息。其中第二个参数的这三个值也可用1,0,-1来表示,方便我们在编程时的输入。

阿尔法通道(Alpha Channel)是指一张图片的透明和半透明度.

import cv2

#采取默认方式读取图片,即cv2.imread(path, 1)
img = cv2.imreda('img800.jpg')

#黑白方式读取图片
img1 = cv2.imread('img800', 0)

imread-imshow

2、图片显示 imshow()

opencv提供了cv2.imshow()来显示图片,该方法有两个参数,第一个为窗口名,第二个为所要显示的图片。

Use the function cv2.imshow() to display an image in a window. The window automatically fits to the image size.
First argument is a window name which is a string. second argument is our image. You can create as many windows as you wish, but with different window names.

直接上代码:
第一种方法:

import cv2

img1 = cv2.imread('img800.jpg', 0)
#取名图片窗口名为gray,显示图片是img1
cv2.imshow('gray', img1)
#添cv2.waitKey(0),在IDLE中执行窗口直接无响应。在命令行中执行的话,则是一闪而过
cv2.waitKey(0)
#释放窗口,简单暴力释放掉所有的窗口
cv2.destroyAllWindows()

其中cv2.waitKey()cv2.destroyAllWindows()方法在上面程序中有解释,官方解释如下:

cv2.waitKey() is a keyboard binding function. Its argument is the time in milliseconds. The function waits for specified milliseconds for any keyboard event. If you press any key in that time, the program continues. If 0 is passed, it waits indefinitely for a key stroke. It can also be set to detect specific key strokes like, if key a is pressed etc which we will discuss below.
cv2.destroyAllWindows() simply destroys all the windows we created. If you want to destroy any specific window, use the function cv2.destroyWindow() where you pass the exact window name as the argument.

第二种方法:
先通过cv2.namedWindow()对图片窗口先进行命名并进行其他设置,如可改变窗口大小。
代码如下:

import cv2 

img2 = cv2.imread('img800.jpg')
#通过设置第二个参数使得图片窗口可调节大小,默认是不可调的(cv2.WINDOW_AUTOSIZE)
cv2.namedWindow('color', cv2.WINDOW_NORMAL)
cv2.imshow('color',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

There is a special case where you can already create a window and load image to it later. In that case, you can specify whether window is resizable or not. It is done with the functioncv2.namedWindow(). By default, the flag is cv2.WINDOW_AUTOSIZE. But if you specify flag to be cv2.WINDOW_NORMAL, you can resize window. It will be helpful when image is too large in dimension and adding track bar to windows.

cv2.WINDOW_AUTOSIZE:根据原图大小进行展示
cv2.WINDOW_NORMAL:图片窗口可调节大小

namedWindow()

3、保存图片 cv2.imwrite()

opencv提供了cv2.imwrite()方法保存图片

Use the function cv2.imwrite() to save an image.
First argument is the file name, second argument is the image you want to save.

#保存图片到当前工作目录
cv2.imwrite('saveimg.png', img1)

imwrite()的第三个参数设置


##对于JPEG,其表示的是图像的质量,用0-100的整数表示,默认为95。 
##注意,cv2.IMWRITE_JPEG_QUALITY类型为Long,必须转换成int。
cv2.imwrite('saveimg11.jpg', img1, [int(cv2.IMWRITE_JPEG_QUALITY), 5])

##对于png图片,第三个参数表示的是压缩级别。
##cv2.IMWRITE_PNG_COMPRESSION,从0到9,压缩级别越高,图像尺寸越小。默认级别为3
cv2.imwrite('saveimg11.png', img1, [int(cv2.IMWRITE_JPEG_QUALITY), 5])

save image


例子1:根据用户在键盘所敲下的不同按键对图片进行处理,如ESC退出,按下‘s’时保存图片并退出。

import numpy as np
import cv2

img = cv2.imread('img800.jpg',0)
cv2.imshow('image',img)
#本台机器系统是64位 k = cv2.waitKey(0) & 0xF
#若是32位系统请用 k = cv2.waitKey(0)
k = cv2.waitKey(0) & 0xFF
if k == 27:         # 按下ESC退出
    cv2.destroyAllWindows()
elif k == ord('s'): # 按下's'保存图片并退出
    cv2.imwrite('savegray.png',img)
    cv2.destroyAllWindows()

例子2:通过opencv来读取图片,用matplotlib来显示图片,充分利用其plot的诸多特性。

import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('img800.jpg',0)
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([]) 
plt.show()

因为opencv与matplotlib对图片的读取值顺序等的差异,在二者互相交替使用时需要注意一些细节,更多问题讨论大家可以参考 some problem when you try to load color image in OpenCV and display it in Matplotlib

Color image loaded by OpenCV is in BGR mode. But Matplotlib displays in RGB mode. So color images will not be displayed correctly in Matplotlib if image is read with OpenCV.

show Image by matplotlib.png

猜你喜欢

转载自blog.csdn.net/u012911202/article/details/88743431