def __add_figure_to_frame(self, frame, figure_location):
"""This function is used to add a file from hard disk to the figure
Algorithm source: http://docs.opencv.org/trunk/d0/d86/tutorial_py_image_arithmetics.html
"""
# Get size of frame
height, width, channels = frame.shape
# Only add icon when the frame is big enough
if height >= 100 and width >= 100:
# Load heart icon
icon_heart = cv2.imread(figure_location)
# Convert to RGB
icon_heart = cv2.cvtColor(icon_heart, cv2.COLOR_BGR2RGB)
# Create ROI
rows, cols, channels = icon_heart.shape
roi = frame[:rows, :cols, :]
# Convert heart to greyscale
icon_heart_gray = cv2.cvtColor(icon_heart, cv2.COLOR_RGB2GRAY)
# Create mask and inverse mask with binary threshold
ret, mask = cv2.threshold(icon_heart_gray, 10, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)
# Background: Original frame with inverse mask
frame_bg = cv2.bitwise_and(roi, roi, mask=mask_inv)
# Foreground: Heart with normal mask
icon_heart_fg = cv2.bitwise_and(icon_heart, icon_heart, mask=mask)
# Add heart icon to frame
icon_heart_final = cv2.add(frame_bg, icon_heart_fg)
frame[:rows, :cols, :] = icon_heart_final
return frame
# Setter and getter following
评论列表
文章目录