这是一个基于 Lakesh 想法的更具体的例子。然而,这将处理任意数量的旋转。首先从中间有一条带的基本圆形图像开始。完成此操作后,只需运行for
循环将所有这些旋转图像堆叠在一个网格中,该网格类似于我们在该矩阵中看到的每个旋转角度在旋转值矩阵中看到的角度。
诀窍是弄清楚如何定义基本方向图像。因此,我们定义一个白色正方形,中间有一个黑色圆圈。我们还将在中间定义一条红色条带。现在,我们假设基本方向图像为 51 x 51。因此,我们可以这样做:
%// Define a grid of points between -25 to 25 for both X and Y
[X,Y] = meshgrid(-25:25,-25:25);
%// Define radius
radius = 22;
%// Generate a black circle that has the above radius
base_image = (X.^2 + Y.^2) <= radius^2;
%// Make into a 3 channel colour image
base_image = ~base_image;
base_image = 255*cast(repmat(base_image, [1 1 3]), 'uint8');
%// Place a strip in the middle of the circle that's red
width_strip = 44;
height_strip = 10;
strip_locs = (X >= -width_strip/2 & X <= width_strip/2 & Y >= -height_strip/2 & Y <= height_strip/2);
base_image(strip_locs) = 255;
有了上面的内容,这就是我得到的:
现在,您需要做的就是创建一个最终的输出图像,该图像的图像数量与矩阵中的行和列一样多。鉴于您的旋转矩阵值存储在M
, 我们可以用imrotate http://www.mathworks.com/help/images/ref/imrotate.html从图像处理工具箱并指定'crop'
标志以确保输出图像与原始图像大小相同。然而,随着imrotate
,无论旋转后图像中没有出现什么值,它都默认为0
。您希望它在您的示例中显示为白色,因此我们必须做一些工作。你需要做的是创建一个logical
矩阵的大小与输入图像相同,然后以与基本图像相同的方式旋转它。无论像素显示为黑色(也是false
)在这个旋转的白色图像中,这些是我们需要设置为白色的值。像这样:
%// Get size of rotation value matrix
[rows,cols] = size(M);
%// For storing the output image
output_image = zeros(rows*51, cols*51, 3);
%// For each value in our rotation value matrix...
for row = 1 : rows
for col = 1 : cols
%// Rotate the image
rotated_image = imrotate(base_image, M(row,col), 'crop');
%// Take a completely white image and rotate this as well.
%// Invert so we know which values were outside of the image
Mrot = ~imrotate(true(size(base_image)), M(row,col), 'crop');
%// Set these values outside of each rotated image to white
rotated_image(Mrot) = 255;
%// Store in the right slot.
output_image((row-1)*51 + 1 : row*51, (col-1)*51 + 1 : col*51, :) = rotated_image;
end
end
让我们尝试几个角度来确保这是正确的:
M = [0 90 180; 35 45 60; 190 270 55];
通过上面的矩阵,这就是我得到的图像。这存储在output_image
:
如果您想将此图像保存到文件中,只需执行以下操作imwrite(output_image, 'output.png');
, where output.png
是您要保存到磁盘的文件的名称。我选择了PNG
因为它是无损的,并且与其他无损标准(保存 JPEG 2000)相比,文件大小相对较小。
编辑以在角度为 0 时不显示线条
如果您希望使用上面的代码,并且只想在角度约为 0 时显示黑色圆圈,则只需插入一个if
里面的声明for
循环并创建另一个图像,其中包含一个没有条带的黑色圆圈。当。。。的时候if
如果满足条件,您可以将此新图像放置在正确的网格位置,而不是带有红色条带的黑色圆圈。
因此,使用上面的代码作为基线执行如下操作:
%// Define matrix of sample angles
M = [0 90 180; 35 45 60; 190 270 55];
%// Define a grid of points between -25 to 25 for both X and Y
[X,Y] = meshgrid(-25:25,-25:25);
%// Define radius
radius = 22;
%// Generate a black circle that has the above radius
base_image = (X.^2 + Y.^2) <= radius^2;
%// Make into a 3 channel colour image
base_image = ~base_image;
base_image = 255*cast(repmat(base_image, [1 1 3]), 'uint8');
%// NEW - Create a black circle image without the red strip
black_circle = base_image;
%// Place a strip in the middle of the circle that's red
width_strip = 44;
height_strip = 10;
strip_locs = (X >= -width_strip/2 & X <= width_strip/2 & Y >= -height_strip/2 & Y <= height_strip/2);
base_image(strip_locs) = 255;
%// Get size of rotation value matrix
[rows,cols] = size(M);
%// For storing the output image
output_image = zeros(rows*51, cols*51, 3);
%// NEW - define tolerance
tol = 5;
%// For each value in our rotation value matrix...
for row = 1 : rows
for col = 1 : cols
%// NEW - If the angle is around 0, then draw a black circle only
if M(row,col) >= -tol && M(row,col) <= tol
rotated_image = black_circle;
else %// This is the logic if the angle is not around 0
%// Rotate the image
rotated_image = imrotate(base_image, M(row,col), 'crop');
%// Take a completely white image and rotate this as well.
%// Invert so we know which values were outside of the image
Mrot = ~imrotate(true(size(base_image)), M(row,col), 'crop');
%// Set these values outside of each rotated image to white
rotated_image(Mrot) = 255;
end
%// Store in the right slot.
output_image((row-1)*51 + 1 : row*51, (col-1)*51 + 1 : col*51, :) = rotated_image;
end
end
变量tol
上面的代码定义了一个容差,其中任何内容-tol <= angle <= tol
已绘制黑色圆圈。这是为了在比较时允许浮点容差,因为直接对浮点值执行相等运算从来都不是一个好主意。通常,在您想要测试相等性的某个容差范围内进行比较是公认的做法。
将上述修改后的代码与角度矩阵一起使用M
如前面的示例所示,我现在得到了这张图片:
请注意,矩阵左上角的条目的角度为 0,因此如我们所期望的那样,它被可视化为一个没有条带穿过的黑色圆圈。