用MATLAB实现图像变换后的作物边界



如何在转换(旋转,平移,缩放和剪切)图像后自动裁剪黑色边框?

我正在使用基于强度的自动图像配准(imregtform, optimizer, imregister....)来旋转一个图像以适应另一个图像。现在在旋转之后,我需要裁剪两个图像,使其具有相同的大小,并且仍然对齐。

旋转后的图像的最简单的表示形式是应用变换矩阵的白色正方形。

我使用的一组实际图像的示例可以在这里看到。

我假设这样一个事实,即图像的边缘可能有零值像素,这可能会使操作复杂化,尽管我假设由于噪声,整个图像将是非零的。

也许我可以使用变换矩阵来实际计算需要裁剪的边界。在上面的例子中,矩阵是:

 0,999428374496743      0,00888472048904662   0
-0,00888472048904659    0,999428374496743     0
 3,79626401832983      -0,493066986575474     1

矩阵列在工作空间中的"1x1仿射2d"对象中。我没能找到使用它的语法。

我已经找到了一种方法,尽管它不是很优雅,因此我仍然对解决这个问题的好方法感兴趣。

可以将变换矩阵应用于图像的四个角点,并使用这些新点作为裁剪限制。应该确保裁剪角在原始图像内,或者需要固定到边缘。

% 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]);

最新更新