I am trying to paste an object with a completely tight known mask onto an image so it should be easy, but without some post treatments I get artefacts at the border. I want to use the blending technique Poisson Blending to reduce the artefacts. It is implemented in opencv seamlessClone
.
import cv2
import matplotlib.pyplot as plt
#user provided tight mask array tight_mask of dtype uint8 with only white pixel the ones on the object the others are black (50x50x3)
tight_mask
#object obj to paste a 50x50x3 uint8 in color
obj
#User provided image im which is large 512x512 of a mostly uniform background in colors
im
#two different modes of poisson blending, which give approximately the same result
normal_clone=cv2.seamlessClone(obj, im, mask, center, cv2.NORMAL_CLONE)
mixed_clone=cv2.seamlessClone(obj, im, mask, center, cv2.MIXED_CLONE)
plt.imshow(normal_clone,interpolation="none")
plt.imshow(mixed_clone, interpolation="none")
However, with the code above, I only get images where the pasted objects are very very very transparent. So they are obviously well blended but they are so blended that they fade away like ghosts of objects.
I was wondering if I was the only one to have such issues and if not what were the alternatives in term of poisson blending ?
Do I have to reimplement it from scratch to modify the blending factor (is that even possible ?), is there another way ? Do I have to use dilatation on the mask to lessen the blending ? Can I enhance the contrast somehow afterwards ?
In fact the poisson blending is using the gradient information in the image to paste to blend it into the target image.
It turns out that if the mask is completely tight the border gradient is then artificially interpreted as null. That is why it ignores it completely and produces ghosts.
Using a larger mask by dilating the original mask using morphological operations and therefore including some background is thus the solution.
Care must be taken when choosing the color of the background included if the contrast is too big the gradient will be too strong and the image would not be well blended. Using a color like gray is a good starting point.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With