一,图片操作
1.读取图片
cv2.imread()
第一个变量为图片地址,第二个变量为颜色标志
-
cv2.IMREAD_COLOR:读取彩色图像(默认) - 1
-
cv2.IMREAD_GRAYSCALE:以灰度模式读取 - 0
-
cv2.IMREAD_UNCHANGED:读取包括alpha通道的所有数据 - -1
import numpy as np
import cv2
#以灰度模式加载原图像
img = cv2.imread('timg.jpeg',0)
2.显示图片
cv2.imshow()
第一个参数为窗口名称,第二个变量为输入图像变量名称
#新建一个窗口并显示图片
cv2.imshow('image',img)
#等待(键盘输入)如果没有此步图片无法输出
cv2.waitKey(0)
#关闭所有建立的窗口
cv2.destroyAllWindows()
cv2.waitKey()
参数为等待时间(单位为ms)的,为0时一直等待
返回值是键盘输入的键值
#k = cv2.waitKey(0)
if k == 27: #27代表ESC键的键值
cv2.destroyAllWindows()
elif k == ord('s'): #使用ord函数来代表某个按键的键值
cv2.imwrite('grayimg.png',img) #保存图片
cv2.destroyAllWindows()
3.使用GUI界面来显示图片
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('messi5.jpg',0)
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis
plt.show()
二,视频操作
1.videocapture类
OpenCV中提供了videocapture类来实现视频的获取,只需调用其中的方法
如:
(1)使用VideoCapture()来打开摄像头或导入视频,该函数的参数为摄像头识别号或视频的路径
(2)使用read()方法来获取一帧图像,注意该方法的返回值为一个布尔值和一帧图像的一个list
(3)使用isOpened()来查询摄像头是否开启
(4)使用release()来关闭摄像头
(5)使用get(propid)来获取视频属性,参数propid为属性代码,见表1
(6)使用set(propid)来设置属性
CAP_PROP_POS_MSEC Python: cv.CAP_PROP_POS_MSEC | Current position of the video file in milliseconds. |
CAP_PROP_POS_FRAMES Python: cv.CAP_PROP_POS_FRAMES | 0-based index of the frame to be decoded/captured next. |
CAP_PROP_POS_AVI_RATIO Python: cv.CAP_PROP_POS_AVI_RATIO | Relative position of the video file: 0=start of the film, 1=end of the film. |
CAP_PROP_FRAME_WIDTH Python: cv.CAP_PROP_FRAME_WIDTH | Width of the frames in the video stream. |
CAP_PROP_FRAME_HEIGHT Python: cv.CAP_PROP_FRAME_HEIGHT | Height of the frames in the video stream. |
CAP_PROP_FPS Python: cv.CAP_PROP_FPS | Frame rate. |
CAP_PROP_FOURCC Python: cv.CAP_PROP_FOURCC | 4-character code of codec. see VideoWriter::fourcc . |
CAP_PROP_FRAME_COUNT Python: cv.CAP_PROP_FRAME_COUNT | Number of frames in the video file. |
CAP_PROP_FORMAT Python: cv.CAP_PROP_FORMAT | Format of the Mat objects returned by VideoCapture::retrieve(). |
CAP_PROP_MODE Python: cv.CAP_PROP_MODE | Backend-specific value indicating the current capture mode. |
CAP_PROP_BRIGHTNESS Python: cv.CAP_PROP_BRIGHTNESS | Brightness of the image (only for those cameras that support). |
CAP_PROP_CONTRAST Python: cv.CAP_PROP_CONTRAST | Contrast of the image (only for cameras). |
CAP_PROP_SATURATION Python: cv.CAP_PROP_SATURATION | Saturation of the image (only for cameras). |
CAP_PROP_HUE Python: cv.CAP_PROP_HUE | Hue of the image (only for cameras). |
CAP_PROP_GAIN Python: cv.CAP_PROP_GAIN | Gain of the image (only for those cameras that support). |
CAP_PROP_EXPOSURE Python: cv.CAP_PROP_EXPOSURE | Exposure (only for those cameras that support). |
CAP_PROP_CONVERT_RGB Python: cv.CAP_PROP_CONVERT_RGB | Boolean flags indicating whether images should be converted to RGB. |
CAP_PROP_WHITE_BALANCE_BLUE_U Python: cv.CAP_PROP_WHITE_BALANCE_BLUE_U | Currently unsupported. |
CAP_PROP_RECTIFICATION Python: cv.CAP_PROP_RECTIFICATION | Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently). |
表1
读取摄像头视频的一个例程
import cv2
cap=cv2.VideoCapture(0)
if cap.isOpened():
print('camre opened successfully')
else:
cap.open()
while cap.isOpened():
statue,frame=cap.read()#注意此处返回两个变量
if statue==False:
print('frame input faliure')
cv2.destoryAllWindows()
break
cv2.imshow('frame',frame)
if cv2.waitKey(30)==ord('q'):
cv2.destroyAllWindows()
break
cap.release()
三、绘图函数
line(), circle(), rectangle(), ellipse(), putText()
以上五个函数都有类似的参数:
1.输出的图像
2.