Ask Your Question

Revision history [back]

Python Open CV Homography giving unexpected results. Pleaase suggest what changes in code needed!!!!

Hi! I am doing homography in Python and am not getting expected Output. I am getting differen matrix

Here is my code's Google Colab link:-

https://colab.research.google.com/drive/1OMWP4faY8dbNRAfryvBs7S7CEuM8oeDz

Here is basic Homography Link (just for your reference):-

https://www.learnopencv.com/image-alignment-feature-based-using-opencv-c-python/

In casw you are not able to open Colab. Here is source code.

import cv2 import numpy as np

if __name__ == '__main__' :

pts_src = np.array([[141.0, 131.0], [480.0, 159.0], [493.0, 630.0],[64.0, 601.0]])  

pts_src=np.asarray(pts_src)                     #not needed actually
pts_src = pts_src.astype('float32')              #not needed actually


pts_dst = np.array([[318.0, 256.0],[534.0, 372.0],[316.0, 670.0],[73.0, 473.0]])
pts_dst=np.asarray(pts_dst)                      #not needed actually
pts_dst= pts_dst.astype('float32')               #not needed actually

h, status = cv2.findHomography(pts_src, pts_dst,cv2.RANSAC, 5.0)

# Wrap source image to destination based on homography


print( h)
print(len(h))

print("------------Printing H------")

print( status)

print("------Printing Status-------------")

print (status.ravel().tolist())

print("--------------------")


print("Source is multiplied first")

#pts_src2 = np.array([[141.0, 131.0,1.0], [480.0, 159.0,1.0], [493.0, 630.0,1.0]])
pts_dst2=np.array([[318.0, 256.0,1.0],[534.0, 372.0,1.0],[316.0, 670.0,1.0]]).transpose()


print("----see---")
print(+len(pts_dst2))
pts_dst2=np.asarray(pts_dst2)                      #not needed actually
pts_dst2= pts_dst2.astype('float32')               #not needed actually
pts_dst2=np.asmatrix(pts_dst2)


h=np.asmatrix(h)


pts=np.dot( h, pts_dst2) 
print(pts)

print("--------END-----------")

Output:-

[ 1.46491654e-01 4.41418278e-01 1.61369294e+02] [-3.62463336e-04 -9.14274844e-05 1.00000000e+00]] 3 ------------Printing H------ [[1] [1] [1] [1]] ------Printing Status-------------

[1, 1, 1, 1]

Source is multiplied first ----see--- 3 [[322.31218638 367.38950309 147.72051431] [320.95671889 403.80343648 503.41090271] [ 0.86133122 0.77243355 0.82420517]] --------END-----------

click to hide/show revision 2
None

updated 2019-07-11 03:04:28 -0600

berak gravatar image

Python Open CV Homography giving unexpected results. Pleaase suggest what changes in code needed!!!!

Hi! I am doing homography in Python and am not getting expected Output. I am getting differen matrix

Here is my code's Google Colab link:-

https://colab.research.google.com/drive/1OMWP4faY8dbNRAfryvBs7S7CEuM8oeDz

Here is basic Homography Link (just for your reference):-

https://www.learnopencv.com/image-alignment-feature-based-using-opencv-c-python/

In casw case you are not able to open Colab. Here is source code.

import cv2
import numpy as np

np if __name__ == '__main__' :

:


    pts_src = np.array([[141.0, 131.0], [480.0, 159.0], [493.0, 630.0],[64.0, 601.0]])  

 pts_src=np.asarray(pts_src)                     #not needed actually
 pts_src = pts_src.astype('float32')              #not needed actually


 pts_dst = np.array([[318.0, 256.0],[534.0, 372.0],[316.0, 670.0],[73.0, 473.0]])
 pts_dst=np.asarray(pts_dst)                      #not needed actually
 pts_dst= pts_dst.astype('float32')               #not needed actually

 h, status = cv2.findHomography(pts_src, pts_dst,cv2.RANSAC, 5.0)

 # Wrap source image to destination based on homography

  print( h)
 print(len(h))
  print("------------Printing H------")
  print( status)
  print("------Printing Status-------------")
  print (status.ravel().tolist())
  print("--------------------")

  print("Source is multiplied first")

 #pts_src2 = np.array([[141.0, 131.0,1.0], [480.0, 159.0,1.0], [493.0, 630.0,1.0]])
 pts_dst2=np.array([[318.0, 256.0,1.0],[534.0, 372.0,1.0],[316.0, 670.0,1.0]]).transpose()

  print("----see---")
 print(+len(pts_dst2))
 pts_dst2=np.asarray(pts_dst2)                      #not needed actually
 pts_dst2= pts_dst2.astype('float32')               #not needed actually
 pts_dst2=np.asmatrix(pts_dst2)

  h=np.asmatrix(h)

  pts=np.dot( h, pts_dst2) 
 print(pts)


print("--------END-----------")

print("--------END-----------")

Output:-

 [ 1.46491654e-01  4.41418278e-01  1.61369294e+02]
 [-3.62463336e-04 -9.14274844e-05  1.00000000e+00]]
3
------------Printing H------
[[1]
 [1]
 [1]
 [1]]
------Printing Status-------------

Status------------- [1, 1, 1, 1]

1] --------------------

Source is multiplied first first

----see---
3
[[322.31218638 367.38950309 147.72051431]
 [320.95671889 403.80343648 503.41090271]
 [  0.86133122   0.77243355   0.82420517]]
--------END-----------

--------END-----------