# 用Python解决单应性

I tried to get the homography matrix using the function `cv2.findHomography(args1,args2,args2)` Opencv 4.0.0. and separately by solving equations using least saquare fit. But both answers are not same. Below is the code snippet.

``````import cv2
import numpy as np
from numpy import  linalg as LA

points2 = np.array([[318,256],[534,372],[316,670],[73,473]])
points1 = np.array([[141,131],[480,159],[493,630],[64,601]])

#writing equations from points2-> points1
A=np.array([[318,256,1,0,0,0,-44838,-41658,141],
[0,0,0,318,256,1,-81408,-33536,131],
[534,372,1,0,0,0,-256320,-178560,480],
[0,0,0,534,372,1,-84906,-59148,159],
[493,630,1,0,0,0,-155788,-330310,493],
[0,0,0,493,630,1,-199080,-422100,630],
[73,473,1,0,0,0,-43072,-30272,64],
[0,0,0,73,473,1,-43873,-284273,601],
[0,0,0,0,0,0,0,0,1]]).astype(np.float64)

A_t = np.transpose(A)
#x=np.array([[141,131,480,159,493,630,64,601]]).astype(np.float64)
x=np.array([[0,0,0,0,0,0,0,0,1]]).astype(np.float64)
x= np.transpose(x)
h1=np.matmul(np.matmul(LA.inv(np.matmul(A_t,A)),A_t),x)
H1=h1.reshape((3,3))
print('H1',H1)
H2,status =  cv2.findHomography(points2,points1,0)# from points2 -> points1  & 0 flag means Least square
print(H2)
``````