图像基本操作
数据读取-图像
-
cv2.IMREAD_COLOR:彩色图像
-
cv2.IMREAD_GRAYSCALE:灰度图像
-
矩阵—图像大小
-
像素点的值(0-255)~亮度
import cv2 #opencv读取的格式是BGR
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
img=cv2.imread('cat.jpg')
img
array([[[142, 151, 160],
[146, 155, 164],
[151, 160, 170],
...,
[156, 172, 185],
[155, 171, 184],
[154, 170, 183]],
[[108, 117, 126],
[112, 123, 131],
[118, 127, 137],
...,
[155, 171, 184],
[154, 170, 183],
[153, 169, 182]],
[[108, 119, 127],
[110, 123, 131],
[118, 128, 138],
...,
[156, 169, 183],
[155, 168, 182],
[154, 167, 181]],
...,
[[162, 186, 198],
[157, 181, 193],
[142, 166, 178],
...,
[181, 204, 206],
[170, 193, 195],
[149, 172, 174]],
[[140, 164, 176],
[147, 171, 183],
[139, 163, 175],
...,
[169, 187, 188],
[125, 143, 144],
[106, 124, 125]],
[[154, 178, 190],
[154, 178, 190],
[121, 145, 157],
...,
[183, 198, 200],
[128, 143, 145],
[127, 142, 144]]], dtype=uint8)
#图像的显示,也可以创建多个窗口
cv2.imshow('image',img)
# 等待时间,毫秒级,0表示任意键终止
cv2.waitKey(0)
cv2.destroyAllWindows()
def cv_show(name,img):
cv2.imshow(name,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img.shape
(414, 500, 3)
img=cv2.imread('cat.jpg',cv2.IMREAD_GRAYSCALE)#灰度
img
array([[153, 157, 162, ..., 174, 173, 172],
[119, 124, 129, ..., 173, 172, 171],
[120, 124, 130, ..., 172, 171, 170],
...,
[187, 182, 167, ..., 202, 191, 170],
[165, 172, 164, ..., 185, 141, 122],
[179, 179, 146, ..., 197, 142, 141]], dtype=uint8)
img.shape
(414, 500)
#图像的显示,也可以创建多个窗口
cv2.imshow('image',img)
# 等待时间,毫秒级,0表示任意键终止
cv2.waitKey(0)
cv2.destroyAllWindows()
#保存
cv2.imwrite('mycat.png',img)
True
type(img)
numpy.ndarray
img.size
207000
img.dtype
dtype('uint8')
数据读取-视频
- cv2.VideoCapture可以捕获摄像头,用数字来控制不同的设备,例如0,1。
- 如果是视频文件,直接指定好路径即可。