我已经想出了一种方法,虽然它不是很优雅,因此我仍然对解决这个问题的好方法感兴趣。
可以将变换矩阵应用于图像的四个角点,并将这些新点用作裁剪限制。应确保裁剪角在原始图像内或需要固定到边缘。
% width and length of the input image
width = size(img_fixed,1);
height = size(img_fixed,2);
% transform the four corners of the image to find crop area
[x1,y1] = transformPointsForward(T,0,0);
[x2,y2] = transformPointsForward(T,width,0);
[x3,y3] = transformPointsForward(T,width,height);
[x4,y4] = transformPointsForward(T,0,height);
% find inner most borders for a rectangular crop
if max([x1,x4]) < 0
x_left = 0;
else
x_left = ceil(max([x1,x4]));
end
if min([x2,x3]) > width
x_right = width;
else
x_right = floor(min([x2,x3]));
end
if max([y1,y2]) < 0
y_top = 0;
else
y_top = ceil(max([y1,y2]));
end
if min([y3,y4]) > height
y_bottom = height;
else
y_bottom = floor(min([y3,y4]));
end
img_fixed_crop = imcrop(img_fixed,[x_left y_top x_right-x_left y_bottom-y_top]);
img_moving_crop = imcrop(img_moving,[x_left y_top x_right-x_left y_bottom-y_top]);