def AffineFromPointsLS(src,dst,new_size,filter=BILINEAR, normalize=True):
'''
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
This method minimizes the squared error to find an optimal fit between the
points.
@param src: a list of link.Points in the source image.
@param dst: a list of link.Points in the destination image.
@param new_size: new size for the image.
@param filter: PIL filter to use.
'''
if normalize:
# Normalize Points
src_norm = AffineNormalizePoints(src)
src = src_norm.transformPoints(src)
dst_norm = AffineNormalizePoints(dst)
dst = dst_norm.transformPoints(dst)
# Compute the transformation parameters
A = []
b = []
for i in range(len(src)):
A.append([src[i].X(),-src[i].Y(),1,0])
A.append([src[i].Y(), src[i].X(),0,1])
b.append(dst[i].X())
b.append(dst[i].Y())
A = array(A)
b = array(b)
result,resids,rank,s = lstsq(A,b)
a,b,tx,ty = result
# Create the transform matrix
matrix = array([[a,-b,tx],[b,a,ty],[0,0,1]],'d')
if normalize:
matrix = dot(dst_norm.inverse,dot(matrix,src_norm.matrix))
return AffineTransform(matrix,new_size,filter)
评论列表
文章目录