python cv2 的简单使用

pip install opencv-python
import cv2

cv2.imread() 读取图片

cv2.imread(img_path, mode)
img_path 表示图片路径
mode 表示读取图片的格式,如
\(\quad\)cv2.IMREAD_ANYCOLOR
\(\quad\)cv2.IMREAD_ANYDEPTH
\(\quad\)cv2.IMREAD_COLOR
\(\quad\)cv2.IMREAD_GRAYSCALE

img = cv2.imread('./04_001_004.jpg', cv2.IMREAD_COLOR)

cv2.imshow() 显示图片

cv2.imshow(winname, mat)
参数 winname 为图片显示窗口的名称
参数 mat 为numpy.ndarray 像素矩阵

cv2.waitKay() 等待key event

关于waitKey(delay)函数:
在时间delay(单位ms, delay=0表示无限等待触发事件)内,等待用户按键(例如关闭图像窗口)触发,如果没有触发事件,则跳出等待。

It returns the code of the pressed key or -1 if no key was
. pressed before the specified time had elapsed.
返回数值为按下的键,如果没有在规定时间按下key,就返回-1

cv2.cvtColor() 类型转换

cvtColor(src, code[, dst[, dstCn]]) -> dst。将颜色从一个色彩空间转换到另一个色彩空间。
\(\quad\)src 输入图像
\(\quad\)code 代码颜色空间转换代码
\(\quad\)dst 输出图像(dst output image of the same size and depth as src.)
\(\quad\)dstCn 目标图像中的信道数; 如果该参数为0,则通道的数量自动从SRC和code派生。

cv2.COLOR_BGR2RGB

COLOR_BGR2Lab = 44
COLOR_BGR2LAB = 44
COLOR_BGR2Luv = 50
COLOR_BGR2LUV = 50
COLOR_BGR2RGB = 4
COLOR_BGR2RGBA = 2
COLOR_BGR2XYZ = 32
COLOR_BGR2YCrCb = 36
img_src = cv2.imread(file_path, cv2.IMREAD_COLOR)
img_tgt = cv2.cvtColor(img_src, cv2.COLOR_BGR2RGB)
plt.imshow(img_tgt)
plt.show()
posted @ 2022-06-17 09:02  lucky_light  阅读(1569)  评论(0编辑  收藏  举报