2016-04-12 3 views
3

Ich habe eine beliebige Form, deren äußere Grenze in MATLAB mit verfolgt wurde. Mit regionprops kann ich die von dieser Form eingeschlossene Gesamtfläche berechnen.So finden Sie den Bereich einer beliebigen Form innerhalb eines Kreises mit MATLAB

Allerdings mag ich für nur die Teile der Form, um die Gegend kennen, die R an den Koordinaten [x1, y1] zentrierte innerhalb eines Kreises von bekannten Radius fallen. Was ist der beste Weg, dies zu erreichen?

+0

und was ist dein Problem? – Piglet

Antwort

3

Es gibt einige Möglichkeiten, dies zu erreichen. Eine Möglichkeit, die Maske vor (oder regionprops) so zu ändern, dass sie nur enthält Pixel, die innerhalb des angegebenen Kreises sind. In diesem Beispiel wird davon ausgegangen, dass Sie bereits über eine logische Matrix M verfügen, die Sie an übergeben.

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 

Und als Beispiel

%// 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 

enter image description here

Verwandte Themen