seamlessClone flickering

asked 2020-06-27 16:22:30 -0600

hak16 gravatar image

updated 2020-10-24 03:02:59 -0600

I have a video, from which I extract the first frame to use as a static background. For each frame of the video, I would like to mask a part of it on top of the static background, using this mask. Without blending:

maskedFrame = cv2.bitwise_and(frame, frame, mask = mask) 

maskedBackground = cv2.bitwise_and(staticBackground, staticBackground, mask = cv2.bitwise_not(mask)) 

output = maskedFrame + maskedBackground

Result

However, I would like to use Poisson blending:

output = cv2.seamlessClone(frame, staticBackground, center, cv2.NORMAL_CLONE)

With the center computed like so:

x,y,w,h = cv2.boundingRect(contours[0]) 
center = (int(x+w/2), int(y+h/2))

This produces weird results and flickering, and what looks like a rectangular mask. I thought maybe the mask was too tight around the moving object, so I scaled its contour but that didn't fix the problem. I also tried seamlessClone with maskedBackground instead of staticBackground but, unsurprisingly, that didn't work either. Is seamlessClone a wrong choice for this problem or am I using it incorrectly?

edit retag flag offensive close merge delete

Comments

This could be wrong:

x,y,w,h = cv2.boundingRect(contours[0]) 
center = (int(x+w/2), int(y+h/2))

Actually, could be similar:

rect = cv2.boundingRect(contours[0]) 
center = (rect[0] + rect[2] // 2, rect[1] + rect[3] // 2)

Hope this help.

supra56 gravatar imagesupra56 ( 2020-06-27 21:15:32 -0600 )edit

Thank you for the reply.

Unfortunately, it has not fixed my problem. I think the two codes produce the same result. I don't think it's a problem with the center coordinates, as the mask looks aligned with the background.

Edit: I have solved the problem, but can't post the answer yet as I'm a new user.

hak16 gravatar imagehak16 ( 2020-06-28 12:21:42 -0600 )edit