def align(l1, l2, axis):
if axis == 1: #horizontal alignment, we do not care about the right line end
#cw = min(l2.shape[1],l1.shape[1])
#l1 = l1[:,:cw]
#l2 = l2[:,:cw]
#compute correlation
sc1 = np.sum(l1, axis=1-axis)
sc2 = np.sum(l2, axis=1-axis)
cor = np.correlate(sc1,sc2,"same")
posErr = np.argmax(cor)-sc1.shape[0]/2
#place at right position
if posErr > 0:
l2c = l2.copy()
l2c[:]=0
l2c[:,posErr:] = l2[:,:-posErr]
l2 = l2c
elif posErr < 0:
l1c = l1.copy()
l1c[:]=0
l1c[:,-posErr:] = l1[:,:posErr]
l1=l1c
else: #vertical alignment, we cate about both ends
#compute correlation
sc1 = np.sum(l1, axis=1-axis)
sc2 = np.sum(l2, axis=1-axis)
cor = np.correlate(sc1,sc2,"same")
posErr = np.argmax(cor)-sc1.shape[0]/2
#place at right position
if posErr > 0:
l2c=l2.copy()
l2c[:]=0
l2c[posErr:,:] = l2[:-posErr,:]
l2 = l2c
elif posErr < 0:
l1c=l1.copy()
l1c[:]=0
l1c[-posErr:,:]=l1[:posErr,:]
l1 = l1c
return posErr, l1, l2
docompare.py 文件源码
python
阅读 28
收藏 0
点赞 0
评论 0
评论列表
文章目录