MATLAB计算图上的形状面积
MATLAB calculate area of shape on plot
我使用 imagesc 创建绘图。 X/Y轴分别是经度和纬度。 Z 值是下图所示图像的强度。我想要做的是计算显示的每个多边形的面积。任何人都可以推荐一个简单的(或任何)方法来完成这个吗?
编辑
忘记包含图片。
这可能会有帮助:http://se.mathworks.com/matlabcentral/answers/35501-surface-area-from-a-z-matrix
他没用过imagesc,不过也是类似的问题。
下面是一个玩具示例。它取决于 Z
值在对象内部与外部不同的假设(此处:不是 0)。同样在这里,我假设第 4 列有一个直线分隔线,但相同的原则(应用掩码)可以应用于其他边界。这还假设值沿 x
和 y
轴等距,但问题并没有相反的说法。如果不是这种情况,则需要使用 bsxfun
做更多的工作。
A = [0 2 0 0 0 2 0
3 5 3 0 1 4 0
1 4 0 0 3 2 3
2 3 0 0 0 4 2
0 2 6 0 1 6 1
0 3 0 0 2 3 0
0 0 0 0 0 0 0];
area_per_pix = 0.5; % or whatever
% plot it
cm = parula(10);
cm(1, :) = [1 1 1];
figure(1);
clf
imagesc(A);
colormap(cm);
% divider
dv_idx = 4;
left_object = A(:, 1:(dv_idx-1));
left_mask = left_object > 0; % threshold object
num_pix_left = sum(left_mask(:));
% right object, different method
right_mask = repmat((1:size(A, 2)) > dv_idx, size(A, 1), 1);
right_mask = (A > 0) & right_mask;
num_pix_right = sum(right_mask(:));
fprintf('The left object is %.2f units large, the right one %.2f units.\n', ...
num_pix_left * area_per_pix, num_pix_right * area_per_pix);
我使用 imagesc 创建绘图。 X/Y轴分别是经度和纬度。 Z 值是下图所示图像的强度。我想要做的是计算显示的每个多边形的面积。任何人都可以推荐一个简单的(或任何)方法来完成这个吗?
编辑
忘记包含图片。
这可能会有帮助:http://se.mathworks.com/matlabcentral/answers/35501-surface-area-from-a-z-matrix 他没用过imagesc,不过也是类似的问题。
下面是一个玩具示例。它取决于 Z
值在对象内部与外部不同的假设(此处:不是 0)。同样在这里,我假设第 4 列有一个直线分隔线,但相同的原则(应用掩码)可以应用于其他边界。这还假设值沿 x
和 y
轴等距,但问题并没有相反的说法。如果不是这种情况,则需要使用 bsxfun
做更多的工作。
A = [0 2 0 0 0 2 0
3 5 3 0 1 4 0
1 4 0 0 3 2 3
2 3 0 0 0 4 2
0 2 6 0 1 6 1
0 3 0 0 2 3 0
0 0 0 0 0 0 0];
area_per_pix = 0.5; % or whatever
% plot it
cm = parula(10);
cm(1, :) = [1 1 1];
figure(1);
clf
imagesc(A);
colormap(cm);
% divider
dv_idx = 4;
left_object = A(:, 1:(dv_idx-1));
left_mask = left_object > 0; % threshold object
num_pix_left = sum(left_mask(:));
% right object, different method
right_mask = repmat((1:size(A, 2)) > dv_idx, size(A, 1), 1);
right_mask = (A > 0) & right_mask;
num_pix_right = sum(right_mask(:));
fprintf('The left object is %.2f units large, the right one %.2f units.\n', ...
num_pix_left * area_per_pix, num_pix_right * area_per_pix);