def make_background_black(frame):
"""
Makes everything apart from the main object of interest to be black in color.
"""
print("Making background black...")
# Convert from RGB to HSV
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Prepare the first mask.
# Tuned parameters to match the skin color of the input images...
lower_boundary = np.array([0, 40, 30], dtype="uint8")
upper_boundary = np.array([43, 255, 254], dtype="uint8")
skin_mask = cv2.inRange(frame, lower_boundary, upper_boundary)
# Apply a series of erosions and dilations to the mask using an
# elliptical kernel
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
skin_mask = cv2.erode(skin_mask, kernel, iterations=2)
skin_mask = cv2.dilate(skin_mask, kernel, iterations=2)
# Prepare the second mask
lower_boundary = np.array([170, 80, 30], dtype="uint8")
upper_boundary = np.array([180, 255, 250], dtype="uint8")
skin_mask2 = cv2.inRange(frame, lower_boundary, upper_boundary)
# Combine the effect of both the masks to create the final frame.
skin_mask = cv2.addWeighted(skin_mask, 0.5, skin_mask2, 0.5, 0.0)
# Blur the mask to help remove noise.
# skin_mask = cv2.medianBlur(skin_mask, 5)
frame_skin = cv2.bitwise_and(frame, frame, mask=skin_mask)
frame = cv2.addWeighted(frame, 1.5, frame_skin, -0.5, 0)
frame_skin = cv2.bitwise_and(frame, frame, mask=skin_mask)
print("Done!")
return frame_skin
image_transformation.py 文件源码
python
阅读 27
收藏 0
点赞 0
评论 0
评论列表
文章目录