import cv2
img=cv2.imread('D:/bridge.jpg',cv2.IMREAD_GRAYSCALE)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyWindow('image')
imread():读入图片
函数参数:1.图片路径 2.读取(默认)彩色图或灰度图 1或0
imshow():展示图片
函数参数: 1.窗口名称 2.图像对象
waitKey(0):按任意键相当pass
destoryWindow():销毁窗口
函数参数 1.销毁窗口的名称
图像属性
print(img)
print(img.shape)
print(type(img))
print(img.size)
[[145 145 145 ... 117 117 117]
[145 145 145 ... 117 117 117]
[145 145 145 ... 116 116 116]
...
[ 64 64 64 ... 48 48 48]
[ 64 64 64 ... 48 48 48]
[ 64 64 64 ... 48 48 48]]
(1280, 960)
<class 'numpy.ndarray'>
1228800
读取视频
import cv2
vc=cv2.VideoCapture("D:/vidio.mp4")
if vc.isOpened():#判断打开正确与否
open,frame=vc.read()#read()读第一帧图片,返回一个布尔值和图片对象
while open:
ret,frame=vc.read()
if frame is None:
break#读取到最后一帧之后跳出循环
if ret==True:
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)#图像处理为灰度图
cv2.imshow("result",gray)
if cv2.waitKey(10)&0xFE==27:
break
vc.release()
cv2.destroyWindow("result")