0

I have an image of white paper on the desk, with a different orientation. Something like this:

enter image description here

Now, I detected the white paper and obtained the coordinates of the trapezium. I now have another image, say of a keyboard like this:

enter image description here

Now my aim is to replace that paper by this keyboard. I discovered that getPerspectiveTransform can be used. I referred the code given here as a starter for mine. There, in place of output, I fed output = imread("paper.jpg") and in place of input, input = imread("keyboard.jpg"). And also had detected the corners of the white sheet earlier.

It worked well by replacing the white paper by the keyboard after transformation, but the background containing the desk, the pencil, etc were blackened out. The entire background became black and only the keyboard replacing the white paper could be seen.

So I wanted to know how to replace the portion through perspective transform, still retaining the background image.

Bms bharadwaj
  • 483
  • 5
  • 18

1 Answers1

0

If you did not edit the code in the post, you are transforming the keyboard, but not adding the original image behind it. You'll need to composite the 2 images together in some way. Assuming the keyboard image has a alpha channel, you could follow this method: https://www.learnopencv.com/alpha-blending-using-opencv-cpp-python/.

If it doesn't have an alpha channel, you can create your own mask and combine the input and output images together, moderated by the mask:

composite = input
composite[mask != 0] = output * mask

Good luck!

NateTheGrate
  • 590
  • 3
  • 11