def AffineFromPoints(src1,src2,dst1,dst2,new_size,filter=BILINEAR):
'''
An affine transform that will rotate, translate, and scale to map one
set of points to the other. For example, to align eye coordinates in face images.
Find a transform (a,b,tx,ty) such that it maps the source points to the
destination points::
a*x1-b*y1+tx = x2
b*x1+a*y1+ty = y2
The mapping between the two points creates a set of four linear equations
with four unknowns. This set of equations is solved to find the transform.
@param src1: the first link.Point in the source image.
@param src2: the second link.Point in the source image.
@param dst1: the first link.Point in the destination image.
@param dst2: the second link.Point in the destination image.
@param new_size: new size for the image.
@param filter: PIL filter to use.
'''
# Compute the transformation parameters
A = [[src1.X(),-src1.Y(),1,0],
[src1.Y(),src1.X(),0,1],
[src2.X(),-src2.Y(),1,0],
[src2.Y(),src2.X(),0,1]]
b = [dst1.X(),dst1.Y(),dst2.X(),dst2.Y()]
A = array(A)
b = array(b)
result = solve(A,b)
a,b,tx,ty = result
# Create the transform matrix
matrix = array([[a,-b,tx],[b,a,ty],[0,0,1]],'d')
return AffineTransform(matrix,new_size,filter)
评论列表
文章目录