【问题标题】:How to find the area of an arbitrary shape contained within a circle using MATLAB如何使用MATLAB找到包含在圆内的任意形状的面积
【发布时间】:2016-04-13 02:07:22
【问题描述】:

我有一个任意形状,它的外部边界已在 MATLAB 中使用bwboundaries 进行了跟踪。使用regionprops,我可以计算出这个形状所包围的总面积。

但是,我想知道位于以坐标[x1, y1] 为中心的已知半径R 的圆内的形状部分的面积。实现这一目标的最佳方法是什么?

【问题讨论】:

  • 你的问题是什么?

标签: matlab image-processing


【解决方案1】:

有几种方法可以解决这个问题。一种方法是在执行bwboundaries(或regionprops)之前更改掩码,使其包含给定圆圈内的像素。

此示例假定您已经有一个逻辑矩阵 M 并传递给 bwboundaries

function [A, boundaries] = traceWithinCircle(M, x1, y1, R);

    %// Get pixel centers
    [x,y] = meshgrid(1:size(M, 1), 1:size(M, 2));

    %// Compute their distance from x1, y1
    distances = sqrt(sum(bsxfun(@minus, [x(:), y(:)], [x1, y1]).^2, 2));

    %// Determine which are inside of the circle with radius R
    isInside = distances <= R;

    %// Set the values outside of this circle in M to zero
    %// This will ensure that they are not detected in bwboundaries
    M(~isInside) = 0;

    %// Now perform bwboundaries on things that are 
    %// inside the circle AND were 1 in M
    boundaries = bwboundaries(M);

    %// You can, however, get the area by simply counting the number of 1s in M
    A = sum(M(:));

    %// Of if you really want to use regionprops on M
    %// props = regionprops(M);
    %// otherArea = sum([props.Area]);
end

举个例子

%// Load some example data
data = load('mri');
M = data.D(:,:,12) > 60;

%// Trace the boundaries using the method described above
B = traceWithinCircle(M, 70, 90, 50);

%// Display the results
figure;
hax = axes();
him = imagesc(M, 'Parent', hax);
hold(hax, 'on');
colormap gray
axis(hax, 'image');

%// Plot the reference circle
t = linspace(0, 2*pi, 100);
plot(x1 + cos(t)*R, y1 + sin(t)*R);

%// Plot the segmented boundaries
B = bwboundaries(M);

for k = 1:numel(B)
    plot(B{k}(:,2), B{k}(:,1), 'r');
end

【讨论】:

    猜你喜欢
    • 2016-09-04
    • 2011-06-22
    • 1970-01-01
    • 2019-07-28
    • 1970-01-01
    • 2010-11-23
    • 2014-11-07
    • 2017-03-20
    相关资源
    最近更新 更多