def F_affine3d(x, matrix, center=True):
A = matrix[:3,:3]
b = matrix[:3,3]
# make a meshgrid of normal coordinates
coords = Variable(th_iterproduct(x.size(1),x.size(2),x.size(3)).float(),
requires_grad=False)
if center:
# shift the coordinates so center is the origin
coords[:,0] = coords[:,0] - (x.size(1) / 2. + 0.5)
coords[:,1] = coords[:,1] - (x.size(2) / 2. + 0.5)
coords[:,2] = coords[:,2] - (x.size(3) / 2. + 0.5)
# apply the coordinate transformation
new_coords = F.linear(coords, A, b)
if center:
# shift the coordinates back so origin is origin
new_coords[:,0] = new_coords[:,0] + (x.size(1) / 2. + 0.5)
new_coords[:,1] = new_coords[:,1] + (x.size(2) / 2. + 0.5)
new_coords[:,2] = new_coords[:,2] + (x.size(3) / 2. + 0.5)
# map new coordinates using bilinear interpolation
x_transformed = F_trilinear_interp3d(x, new_coords)
return x_transformed
评论列表
文章目录