
本文共 8408 字,大约阅读时间需要 28 分钟。
���������������������������������������������������������������������������
���������������
������ OpenCV ������������������������������������������������������������������������������������������������������������������ Python ���������������
import numpy as npimport cv2# ���������������������cap = cv2.VideoCapture(0)while True: # ������������������ ret, frame = cap.read() # ��������������� cv2.imshow('frame', frame) # ������������ 'q' ��������� if cv2.waitKey(1) & 0xFF == ord('q'): break # ���������������������cap.release()cv2.destroyAllWindows()
������������������������
������������������������������������������������������������������
1. ���������������
������������������������������������������������������������������������������������ 'q' ���������������������
2. ������������
���������������������������������������������������������������������������������������������������
import cv2# ���������������cap = cv2.VideoCapture(0)# ���������������������������������fourcc = cv2.VideoWriter_fourcc('F','L','V','1')out = cv2.VideoWriter('output_1.flv', fourcc, 20.0, (640,480))while(cap.isOpened()): ret, frame = cap.read() # ������������������ out.write(frame) # ��������������� cv2.imshow('frame', frame) # ������������������ if cv2.waitKey(1) & 0xFF == ord('q'): break # ������������������out.release()cap.release()cv2.destroyAllWindows()
3. ������������������
������������������������������������������������������������������ video writer ���������������������������������������������������������������
import cv2# ���������������cap = cv2.VideoCapture('output_1.flv')# ������������������fps = int(cap.get(cv2.CAP_PROP_FPS))frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))# ���������������������������������fourcc = cv2.VideoWriter_fourcc('M','J','P','G')# ������������������������out = cv2.VideoWriter('output_2.avi', fourcc, fps, (frame_width, frame_height))while True: ret, frame = cap.read() # ������������������������ if frame.shape == (frame_height, frame_width): out.write(frame) # ��������������� cv2.imshow('frame', frame) # ��������������������������� if cv2.waitKey(25) & 0xFF == ord('q'): breakcap.release()out.release()cv2.destroyAllWindows()
4. ���������
���������������������������������������������������������������������������������������������������������
import cv2# ���������������������������cap = cv2.VideoCapture('input.mp4')# ���������������������������last_frame = None# ������������������������fourcc = cv2.VideoWriter_fourcc('M','J','P','G')out1 = cv2.VideoWriter('output1.mp4', fourcc, 12.0, (800, 600))out2 = cv2.VideoWriter('output2.mp4', fourcc, 12.0, (800, 600))while cap.isOpened(): ret, frame = cap.read() if not ret: break # ������������������������������������������ frame = cv2.resize(frame, (800, 600), interpolation=cv2.INTER_CUBIC) if last_frame is None: last_frame = frame continue # ��������������������������������� diff_frame = cv2.absdiff(last_frame, frame) # ������������������������ thresh = cv2.cvtColor(diff_frame, cv2.COLOR_BGRA2GRAY) thresh, binar_img = cv2.threshold(thresh, 25, 255, cv2.THRESH_BINARY) # ������������������������ binar_img = cv2.morphologyEx(binar_img, cv2.MORPH_OPEN, kernel=None, iterations=2) # ������������������������������ contours, hierarchy = cv2.findContours(binar_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # ��������������������������� for contour in contours: area = cv2.contourArea(contour) if area < 100: continue x, y, w, h = cv2.boundingRect(contour) cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) # ������������������ out1.write(frame) out2.write(diff_frame) # ��������������� cv2.imshow('���������', frame) cv2.imshow('������������', diff_frame) cv2.imshow('������������', binar_img) # ������������������ k = cv2.waitKey(20) & 0xFF if k == 27: break # ��������� ������������ last_frame = frame # ������������������out1.release()out2.release()cap.release()cv2.destroyAllWindows()
5. ���������
���������������������������������������������������������������������������������������������������
import cv2 as cvimport numpy as np# ������������������������kernel = cv.getStructuringElement(cv.MORPH_ELLIPSE, (10, 10))median_kernel = cv.getStructuringElement(cv.MORPH_ELLIPSE, (5, 5))# ���������������������cap = cv.VideoCapture('input.mp4')# ������������������������fourcc = cv.VideoWriter_fourcc('M','P','4','2')output_size = (800, 600)out1 = cv.VideoWriter('output1.mp4', fourcc, cap.get(cv.CAP_PROP_FPS), output_size)out2 = cv.VideoWriter('output2.mp4', fourcc, cap.get(cv.CAP_PROP_FPS), output_size)# ������������������������������prvs = Nonenext_frame = Nonewhile cap.isOpened(): # ��������������� ret, frame = cap.read() if not ret: break # ��������������������� next_gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) if prvs is None: prvs = next_gray continue # ��������������� flow = cv.calcOpticalFlowFarneback(prvs, next_gray, None, 0.5, 3, 15, 3, 5, 1.2, 0) # ������������������������������ mag, ang = cv.cartToPolar(flow[..., 0], flow[..., 1]) # ������HSV������������ hsv = np.zeros_like(frame) hsv[..., 0] = ang * 180 / np.pi hsv[..., 2] = cv.normalize(mag, None, 0, 255, cv.NORM_MINMAX) # ���HSV���������BGR bgr = cv.cvtColor(hsv, cv.COLOR_HSV2BGR) # ��������������������������������� draw = cv.morphologyEx(cv.cvtColor(bgr, cv.COLOR_BGR2GRAY), cv.MORPH_OPEN, median_kernel, iterations=2) draw = cv.threshold(draw, 25, 255, cv.THRESH_BINARY)[1] # ������������������ Image, contours, hierarchy = cv.findContours(draw, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) for contour in contours: area = cv.contourArea(contour) if area < 5000: continue x, y = contour[0] w = contour[1][0] - x h = contour[1][1] - y cv.rectangle(frame, (x, y), (x + w, y + h), (255, 255, 0), 2) # ������������������ out1.write(bgr) out2.write(frame) # ������������ cv.imshow('������', frame) cv.imshow('���������', cv.cvtColor(bgr, cv.COLOR_BGR2RGB)) cv.imshow('������', draw) # ������������������ k = cv.waitKey(20) & 0xFF if k == 27: break # ESC��������� prvs = next_gray next_gray = frameout1.release()out2.release()cap.release()cv.destroyAllWindows()
6. ���������������
���������������������������������������������������������������������������������������������������������������
import numpy as npimport cv2# ������������������������fgbg = cv2.createBackgroundSubtractorMOG2(history=500, varThreshold=100, detectShadows=False)def detect_person(frame): # ������������������������ foreground = fgbg.apply(frame) # ������������������������ struct = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 5)) foreground = cv2.morphologyEx(foreground, cv2.MORPH_OPEN, struct) # ��������������������� cv2.imshow('���������������', foreground) return foregroundwhile True: ret, frame = cap.read() if not ret: break # ������������������ frame = cv2.resize(frame, (800, 600), interpolation=cv2.INTER_CUBIC) mask = detect_person(frame) # ��������������� result = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) result = cv2.add(result, result // 255) # ��������������������� cv2.imwrite('input.png', frame) cv2.imwrite('output.png', result) cv2.imwrite('mask.png', mask) # ��������������� cv2.imshow('������������', frame) cv2.imshow('������������', result) k = cv2.waitKey(20) & 0xFF if k == 27: # ESC��������� breakcap.release()cv2.destroyAllWindows()
������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
