import cv2
import numpy as np
import time
cap = cv2.VideoCapture(0)
background_capture = cv2.VideoCapture(r'./a.avi')
counter = -1
while cap.isOpened():
counter += 1
start_time_extract_figure = time.time()
# your code
# extract your figure
_, frame = cap.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
mask = np.zeros(frame.shape[:2], np.uint8)
bgdModel = np.zeros((1, 65), np.float64)
fgdModel = np.zeros((1, 65), np.float64)
rect = (50, 50, 450, 490)
start_time_t = time.time()
cv2.grabCut(frame, mask, rect, bgdModel, fgdModel, 1, cv2.GC_INIT_WITH_RECT)
during_time = time.time() - start_time_t
print('{}-th t_time: {}'.format(counter, during_time))
mask2 = np.where((mask == 2) | (mask == 0), (0,), (1,)).astype('uint8')
frame = frame * mask2[:, :, np.newaxis]
elapsed_time_extract_figure = time.time() - start_time_extract_figure
print('{}-th extract_figure_time: {}'.format(counter, elapsed_time_extract_figure))
# extract the background
start_time_combination = time.time()
# your code
ret, background = background_capture.read()
background = cv2.resize(background, (640, 480), interpolation=cv2.INTER_AREA)
# maybe the default size of embedded camera is 640x480
# combine the figure and background using mask instead of iteration
mask_1 = frame > 0
mask_2 = frame <= 0
combination = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) * mask_1 + background * mask_2
elapsed_time_combination = time.time() - start_time_combination
print('{}-th combination_time: {}'.format(counter, elapsed_time_combination))
cv2.imshow('combination', combination)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
发现影响速度的最主要因素是grabCut函数, 因此附官网grabCut详细说明https://docs.opencv.org/trunk/d8/d83/tutorial_py_grabcut.html供以参考.
- img - Input image
- mask - It is a mask image where we specify which areas are background, foreground or probable background/foreground etc. It is done by the following flags, cv2.GC_BGD, cv2.GC_FGD, cv2.GC_PR_BGD, cv2.GC_PR_FGD, or simply pass 0,1,2,3 to image.
- rect - It is the coordinates of a rectangle which includes the foreground object in the format (x,y,w,h)
- bdgModel, fgdModel - These are arrays used by the algorithm internally. You just create two np.float64 type zero arrays of size (1,65).
- iterCount - Number of iterations the algorithm should run.
- mode - It should be cv2.GC_INIT_WITH_RECT or cv2.GC_INIT_WITH_MASK or combined which decides whether we are drawing rectangle or final touchup strokes.