10

I'm investigating ways of transforming an image and overlaying it within a contour of another image.

As best as I can tell there are three ways to do this with OpenCV in Python:

  1. getAffineTransform()
  2. getPerspectiveTransform()
  3. findHomography()

I found three different methods via blog and SO posts and they all produce the same results, i.e. taking a source image and warping/overlaying on a contour of a different shape on a destination image.

This demonstrates getAffineTransform() https://stackoverflow.com/a/38323528/1887261

This demonstrates getPerspectiveTransform() http://uberhip.com/python/image-processing/opencv/2014/10/26/warping-brien/

This demonstrates findHomography() http://www.learnopencv.com/homography-examples-using-opencv-python-c/#download

I'm wondering what is the best method to use and why would you use one over the other?

Community
  • 1
  • 1
metersk
  • 11,803
  • 21
  • 63
  • 100
  • Please use a better title -- there should be something after "between" – Barmar Apr 07 '17 at 03:20
  • accident, fixing! – metersk Apr 07 '17 at 03:39
  • 1
    read the doc to find hints about differences. For example findHomography allows you to choose different robust methods (for example RANSAC) which is nice if you expect a lot of outliers in the point correspondences. – Micka Apr 07 '17 at 06:08
  • There is difference between Homography and affine transform itself – Optimus 1072 Apr 07 '17 at 06:11
  • getAffineTransform uses 3 point pairs, so it is not a "perspective homography" but only has 6 dof. This will make a transformation more stable and less error prone, but can't handle perspective effects, so the transformation will typically not give perfect results. – Micka Apr 07 '17 at 06:12
  • getPerspectiveTransform and getAffineTransform use the minimum number of necessary point pairs to compute the transformation (4 and 3), which perfectly maps the points. findHomography (and estimateRigidTransform) use/allow more point pairs to optimize a result. – Micka Apr 07 '17 at 06:15
  • Duplicate of this? https://stackoverflow.com/questions/11237948/findhomography-getperspectivetransform-getaffinetransform – BourbonCreams Sep 05 '18 at 09:48

1 Answers1

1

cv2.findHomography finds the perspective transformation between two frames. cv2.warpPerspective takes that image transformation between two frames and applies it to the image. Below is some code I wrote for homework the other day:

H, masked = cv.findHomography(src, dst, cv.RANSAC, 5.0)

dst = cv.warpPerspective(topright,H,(topleft.shape[1] + topright.shape[1], topleft.shape[0])
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103