使用 MATLAB 进行图像变换后裁剪边框
Crop borders after image transformation using MATLAB
如何在转换(旋转、平移、缩放和剪切)图像后自动裁剪黑边?
我正在使用基于强度的自动图像配准(imregtform、优化器、imregister....)来旋转一张图像以适合另一张图像。
现在旋转后,我需要将两个图像裁剪成相同的大小,并且仍然排成一行。
旋转图像的最简单表示形式是 a white square to which the transformation matrix was applied.
一个例子什么的一组the actual images I am using would look like can be seen here.
我假设图像边缘可能具有零值像素这一事实可能会使操作复杂化,尽管我假设由于噪声,整个图像将是非零的。
也许我可以使用转换矩阵来实际计算需要裁剪的边框。在上面的例子中,矩阵是:
0,999428374496743 0,00888472048904662 0
-0,00888472048904659 0,999428374496743 0
3,79626401832983 -0,493066986575474 1
矩阵列在工作区的“1x1 仿射二维”对象中。我无法从那里找到使用它的语法。
我已经找到了一种方法,虽然它不是很优雅,因此我仍然对解决这个问题的好方法感兴趣。
可以将变换矩阵应用于图像的四个角点,并将这些新点用作裁剪限制。应确保裁剪角在原始图像内或需要固定到边缘。
% 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]);
如何在转换(旋转、平移、缩放和剪切)图像后自动裁剪黑边?
我正在使用基于强度的自动图像配准(imregtform、优化器、imregister....)来旋转一张图像以适合另一张图像。 现在旋转后,我需要将两个图像裁剪成相同的大小,并且仍然排成一行。
旋转图像的最简单表示形式是 a white square to which the transformation matrix was applied.
一个例子什么的一组the actual images I am using would look like can be seen here.
我假设图像边缘可能具有零值像素这一事实可能会使操作复杂化,尽管我假设由于噪声,整个图像将是非零的。
也许我可以使用转换矩阵来实际计算需要裁剪的边框。在上面的例子中,矩阵是:
0,999428374496743 0,00888472048904662 0
-0,00888472048904659 0,999428374496743 0
3,79626401832983 -0,493066986575474 1
矩阵列在工作区的“1x1 仿射二维”对象中。我无法从那里找到使用它的语法。
我已经找到了一种方法,虽然它不是很优雅,因此我仍然对解决这个问题的好方法感兴趣。
可以将变换矩阵应用于图像的四个角点,并将这些新点用作裁剪限制。应确保裁剪角在原始图像内或需要固定到边缘。
% 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]);