7

I'm trying to perform basic affine transformation using pivot points.

import cv2
import numpy as np
import PIL
import matplotlib.pyplot as plt

img = cv2.imread('earth.png')
img_pivots = cv2.imread('earth_keys.png')
map_img = cv2.imread('earth2.png')
map_pivots = cv2.imread('earth2_keys.png')

pts_img_R = np.transpose(np.where(img_pivots[:, :, 2] > 0 ))
pts_img_G = np.transpose(np.where(img_pivots[:, :, 1] > 0 ))
pts_img_B = np.transpose(np.where(img_pivots[:, :, 0] > 0 ))
pts_img = np.vstack([pts_img_R, pts_img_G, pts_img_B])
pts_map_R = np.transpose(np.where(map_pivots[:, :, 2] > 0 ))
pts_map_G = np.transpose(np.where(map_pivots[:, :, 1] > 0 ))
pts_map_B = np.transpose(np.where(map_pivots[:, :, 0] > 0 ))
pts_map = np.vstack([pts_map_R, pts_map_G, pts_map_B])

M = cv2.estimateRigidTransform(pts_map.astype(np.float32), pts_img.astype(np.float32), True)

dst = cv2.warpAffine(map_img,M,(img.shape[1], img.shape[0]))

plt.subplot(121),plt.imshow(img),plt.title('earth.png')
plt.subplot(122),plt.imshow(dst),plt.title('earth2.png transrofmed')
plt.show()

On both images I made 3 points (R, G & B) and saved them in separate images ('earth_keys.png' for 'earth.png' and 'earth2_keys.png' for 'earth2.png'). All I want is to match pivot points on 'earth2.png' with pivot points on 'earth.png'.

Still, all I get after transformation is this enter image description here

I'm assuming that I misplaced some arguments or something like this, but I tried all combinations and got all types of wrong results, but still can't spot it.

Example images (with pivots)

Edit: Changed pivots number to 6

Still wrong transformation enter image description here

M is now equal to

 array([[  4.33809524e+00,   8.28571429e-01,  -5.85633333e+02],
   [ -6.22380952e+00,  -1.69285714e+00,   1.03468333e+03]])

Example with 6 pivots

3
  • can you please try to use more than 3 points? there are rigid teansforms that allow a minimum of 3 points, but openCV has a "fullAffine" parameter. If this is set I think there are more than 6 dof. Can you tell us the result values of M too?
    – Micka
    Commented May 8, 2016 at 13:24
  • just checked.. 6dof should be ok for fullAffine so 3 point pairs should be ok. please print and add M
    – Micka
    Commented May 8, 2016 at 13:27
  • Added another 3 pivots and edited the question, still something is wrong. Are you using the same code?
    – arkhy
    Commented May 8, 2016 at 13:54

1 Answer 1

6
+50

How confident are you in your pivot points ?

If I plot them on your images, I obtain this: Plotting points

Which gives, after manual superposition, something that looks like your result: Manual superposition

If I define points manually for 3 correspondences, I get this:

pts_img = np.vstack([[68,33],   [22,84],  [113,87]] )
pts_map = np.vstack([[115,101], [30,199], [143,198]])

Result for manual points

It's still not perfect, but it may be closer to what you want to achieve.

To conclude, I'd recommend you to check how you compute your keypoints, and, in case of doubt, to do a manual superposition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.