OpenCV Seamless Cloning shift position after finish the process
Asked Answered
A

3

6

I am trying to used the seamsless cloning to blend to image together. but I notice that after using the seamsless clone function the area in the mask that I want to transfer is shift upward. So I have a question that is this a normal behaviour of the seamsless clone function or it is a bug on my implementation.

Here are the Source photo

enter image description here

Here are the destination photo

enter image description here

Here are the result photo

enter image description here

Annadiane answered 15/12, 2017 at 6:49 Comment(1)
Hello, do you remember if you solved this or what the problem was? I'm facing a similar issue where my source photo is shifted slightly to the left when using seamless cloning. Interestingly, the mask that I use seems to affect this; using an all-white mask will result in no shift.Invitation
R
4

I encountered similar situation. Moreover, like @JoshuaCWebDeveloper noted, this shift disappeared when all one mask is used. Nevertheless, I got a fix for this. What I did is this. I cropped valid mask (non-zero sub-section) out using cv2.boundingRect. So my source image and mask image are reduced to a smaller size, while center is now calculated from boundingRect outputs (Since reference point is marked on destination image). This way, error got solved/shift got ridden.

Raviv answered 15/4, 2020 at 19:23 Comment(0)
P
4

(Based on the answer posted by Fractalic Forieu) You can achieve the same result without reducing the image size.

Instead of using the image center:

center = (width // 2, height // 2)
poissonImage = cv2.seamlessClone(srcImage, dstImage, maskImage, center)

use the center of the bounding rect:

monoMaskImage = cv2.split(maskImage)[0] # reducing the mask to a monochrome
br = cv2.boundingRect(monoMaskImage) # bounding rect (x,y,width,height)
centerOfBR = (br[0] + br[2] // 2, br[1] + br[3] // 2)
poissonImage = cv2.seamlessClone(srcImage, dstImage, maskImage, centerOfBR )
Pralltriller answered 23/2, 2021 at 16:22 Comment(0)
R
0

It seems that a shift occurs such that only the non-zero subsection of the mask is considered. To prevent the shift artifact, I've found a good workaround to be clipping the mask to a small value.

mask = np.clip(mask,a_min=1,a_max=255)
Riba answered 27/7, 2024 at 3:20 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.