视频/摄像头处理
#1打开视屏文件
#打开视屏文件 def extrace_object_demo(): capture = cv.VideoCapture("D:/vcprojects/images/video_006.mp4") while(True): ret, frame = capture.read() if ret == False:#读到视频结尾返回false break; hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV) lower_hsv = np.array([37, 43, 46]) upper_hsv = np.array([77, 255, 255]) mask = cv.inRange(hsv, lowerb=lower_hsv, upperb=upper_hsv) dst = cv.bitwise_and(frame, frame, mask=mask) cv.imshow("video", frame) cv.imshow("mask", dst) c = cv.waitKey(40) if c == 27: break
2#调用摄像头获得像素数据
import cv2 as cv import numpy as np #视频 def video_demo(): capture = cv.VideoCapture(0)#0是代表摄像头,也可以改成视频文件路径 while(True): ret, frame = capture.read()#ret是返回值,frame代表视频每一帧 frame = cv.flip(frame, 1)#镜像变化 cv.imshow("video", frame)#显示视频的每一帧 c = cv.waitKey(50) if c == 27: break #获取像素等数据 def get_image_info(image): print(type(image))#类型 print(image.shape)#高,宽,通道数 print(image.size) print(image.dtype) pixel_data = np.array(image)#获取像素 print(pixel_data) print("--------- Hello Python ---------") src = cv.imread("C:/Users/wml/Desktop/wml/ym.jpg") cv.namedWindow("input image", cv.WINDOW_AUTOSIZE) cv.imshow("input image", src) video_demo() # get_image_info(src) gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)#获取灰度图 cv.imwrite("C:/Users/wml/Desktop/wml/ym_gray.jpg", gray)#保存图片 cv.waitKey(0) cv.destroyAllWindows()