0

I'm trying to replace a rectangular portion of an image. The original image is

Hugs

I have all the coordinates of the green rectangle, and I have a second image with the same dimensions. I need to apply this second image over the original in the same position as the green rectangle. Any help is appreciated. Thanks!

Davide Fiocco
  • 5,350
  • 5
  • 35
  • 72
Loris
  • 1
  • 1
  • 3
    Welcome to StackOverflow! Does this answer your question? [How to replace a contour (rectangle) in an image with a new image using Python?](https://stackoverflow.com/questions/38320865/how-to-replace-a-contour-rectangle-in-an-image-with-a-new-image-using-python). For next time (or now!) do not hesitate to put in the question any attempts you tried to solve your problem using code. Happy festivities! – Davide Fiocco Dec 24 '20 at 17:23
  • Thank you! :) I dont quite understand why pts1 = np.float32([[201, 561], [455, 279], [742, 985]]) has only 3 points and not 4. And I do not understand at all how pts2 should be hard-coded with my values. (these are my rectangle coordinates: [[479, 380] [689, 248] [742, 333] [532, 464]]) – Loris Dec 24 '20 at 17:45
  • In that answer, they define an (affine) transformation that gets the original image transformed in a specified way (so to fit exactly where it should go). To define such a transform, 2 sets of three points are needed: https://docs.opencv.org/3.4/d4/d61/tutorial_warp_affine.html. To adapt the example to your case (I think!) one has to construct a transformation using the original coordinates of 3 corners of your original image/rectangle, and then the coordinates those same 3 corners should have in the target image. – Davide Fiocco Dec 24 '20 at 18:16
  • 1
    I should start reading docs more often lol. Thank you! perfect explanation and now I got what I wanted. Happy xmas! :D – Loris Dec 24 '20 at 19:04

0 Answers0