视频处理
demo1
import cv2
capture = cv2.VideoCapture(0)
while capture.isOpened():
retval, image = capture.read()
cv2.imshow("Video", image)
key = cv2.waitKey(1)
if key == 32:
break
capture.release()
cv2.destroyAllWindows()
demo2
import cv2
capture = cv2.VideoCapture(0, cv2.CAP_DSHOW)
while capture.isOpened():
retval, image = capture.read()
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("Video", image_gray)
key = cv2.waitKey(1)
if key == 32:
break
capture.release()
cv2.destroyAllWindows()
demo3
import cv2
capture = cv2.VideoCapture(0, cv2.CAP_DSHOW)
while capture.isOpened():
retval, frame = capture.read()
cv2.imshow("Video", frame)
key = cv2.waitKey(1)
if key == 32:
capture.release()
cv2.destroyWindow("Video")
cv2.imwrite("./copy.png", frame)
cv2.imshow("img", frame)
break
cv2.destroyAllWindows()
demo4
import cv2
video = cv2.VideoCapture("公司宣传.avi")
while video.isOpened():
retval, image = video.read()
cv2.namedWindow("Video", 0)
cv2.resizeWindow("Video", 432, 300)
if retval == True:
cv2.imshow("Video", image)
else:
break
key = cv2.waitKey(1)
if key == 27:
break
video.release()
cv2.destroyAllWindows()
demo5
import cv2
video = cv2.VideoCapture("公司宣传.avi")
while video.isOpened():
retval, image = video.read()
cv2.namedWindow("Gray", 0)
cv2.resizeWindow("Gray", 432, 300)
if retval == True:
img_Gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("Gray", img_Gray)
else:
break
key = cv2.waitKey(1)
if key == 27:
break
video.release()
cv2.destroyAllWindows()
demo6
import cv2
video = cv2.VideoCapture("公司宣传.avi")
while video.isOpened():
retval, image = video.read()
cv2.namedWindow("Video", 0)
cv2.resizeWindow("Video", 420, 300)
if retval == True:
cv2.imshow("Video", image)
else:
break
key = cv2.waitKey(50)
if key == 32:
cv2.waitKey(0)
continue
if key == 27:
break
video.release()
cv2.destroyAllWindows()
demo7
import cv2
video = cv2.VideoCapture("公司宣传.avi")
fps = video.get(cv2.CAP_PROP_FPS)
frame_Count = video.get(cv2.CAP_PROP_FRAME_COUNT)
frame_Width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_Height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
print("帧速率:", fps)
print("帧数:", frame_Count)
print("帧宽度:", frame_Width)
print("帧高度:", frame_Height)
demo8
import cv2
video = cv2.VideoCapture("output.avi")
fps = video.get(cv2.CAP_PROP_FPS)
frame_Num = 1
while video.isOpened():
retval, frame = video.read()
cv2.namedWindow("Video", 0)
cv2.resizeWindow("Video", 420, 300)
if retval == True:
cv2.putText(frame, "frame:" + str(frame_Num), (0, 100), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 5)
cv2.putText(frame, "second:" + str(round(frame_Num/fps, 2)) + "s", (0, 200), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 5)
cv2.imshow("Video", frame)
else:
break
key = cv2.waitKey(50)
frame_Num+=1
if key == 27:
break
video.release()
cv2.destroyAllWindows()
demo9
import cv2
capture = cv2.VideoCapture(0, cv2.CAP_DSHOW)
fourcc = cv2.VideoWriter_fourcc('X', 'V', 'I', 'D')
output = cv2.VideoWriter("output.avi", fourcc, 20, (640, 480))
while capture.isOpened():
retval, frame = capture.read()
if retval == True:
output.write(frame)
cv2.imshow("frame", frame)
key = cv2.waitKey(1)
if key == 27:
break
capture.release()
output.release()
cv2.destroyAllWindows()