-
Notifications
You must be signed in to change notification settings - Fork 0
compositing with scaled down mask
Nur edited this page Mar 5, 2024
·
2 revisions
def run_case_eight(frame,new_bg,person_mask,overlay):
frame = cv2.cvtColor(frame,cv2.COLOR_RGBA2RGB)
new_bg = cv2.cvtColor(new_bg,cv2.COLOR_RGBA2RGB)
# resizing for operation
new_bg = cv2.resize(new_bg,(frame.shape[1],frame.shape[0]))
target_width = 500
target_height = int(target_width * frame.shape[1] / frame.shape[0])
frame = cv2.resize(frame, (target_height,target_width), interpolation=cv2.INTER_AREA)
person_mask = cv2.resize(person_mask,(frame.shape[1],frame.shape[0]))
# Calculate offsets for centering on the background
x_offset = int((new_bg.shape[1] - frame.shape[1]) / 2)
y_offset = int((new_bg.shape[0] - frame.shape[0]) )
# Create ROI in background for the frame
roi = new_bg[y_offset:y_offset + frame.shape[0], x_offset:x_offset + frame.shape[1]]
# blurring the mask for blending
person_mask = cv2.bitwise_not(person_mask)
cv2.imshow('person_mask_bitwise_not',person_mask)
person_mask = manipulator.expand_mask(person_mask)
cv2.imshow('person_mask_expand',person_mask)
mask = cv2.blur(person_mask,(30,30))
mask = cv2.normalize(mask, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U)
maskf = (mask/255).astype(np.float64)
maskf = cv2.merge([maskf,maskf,maskf])
print(f'maskf:{maskf.shape}')
print(f'frame:{frame.shape}')
print(f'background:{new_bg.shape}')
# Perform element-wise multiplication and update background ROI
# blended_roi = (maskf * frame).astype(np.uint8)
person_without_bg = (maskf * frame).astype(np.uint8)
roi_with_person_cutout = ((1-maskf) * roi).astype(np.uint8)
person_on_new_bg = person_without_bg + roi_with_person_cutout
final = new_bg.copy()
final[y_offset:y_offset + frame.shape[0], x_offset:x_offset + frame.shape[1]] = person_on_new_bg
final = cv2.addWeighted(final,0.7,new_bg,0.3,0)
cv2.imshow('roi',roi)
cv2.imshow('person_without_bg',person_without_bg)
cv2.imshow('roi_with_person_cutout',roi_with_person_cutout)
cv2.imshow('person_on_new_bg',person_on_new_bg)
# bg_without_person = ((1-maskf) * new_bg).astype(np.uint8)
cv2.imshow('maskf',maskf)
cv2.imshow('mask',mask)
cv2.imshow('new_bg',new_bg)
return final