我是一名晶体学家,试图从多达 5000 个文件中分析晶体取向。Matlab 可以在如下所示的表格中转换角度值:
进入一个看起来像这样的表?:
我是一名晶体学家,试图从多达 5000 个文件中分析晶体取向。Matlab 可以在如下所示的表格中转换角度值:
进入一个看起来像这样的表?:
这是一个基于 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
从图像处理工具箱中使用并指定'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');
,output.png
您要保存到磁盘的文件的名称在哪里。我之所以选择PNG
它是因为它是无损的,并且与其他无损标准(保存 JPEG 2000)相比,它的文件大小相对较小。
如果您希望使用上面的代码,如果您只想在角度为 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,因此可以将其可视化为一个黑色圆圈,正如我们所期望的那样,没有条纹穿过它。
解决您的问题的总体思路:
1. Store two images, 1 for 0 degrees and 180 degrees and another for 90 and 270 degrees.
2. Read the data from the file
3. if angle = 0 || angle == 180
image = image1
else
image = image2
end
处理任何角度:
1. Store one image. E.g image = imread('yourfile.png')
2. angle = Read the data from the file
3. B = imrotate(image,angle)