![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1401549/image.png)
how to calculate the area of each bin of intersection of a circle and mesh grid?
2 次查看(过去 30 天)
显示 更早的评论
I have divided a circle of D=16mm into a grid of 7X7. (8 vertical lines by 8 horizontal lines)
How can I estimate the area of each bin which is enclosed in the circle?
grid layout:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1398809/image.png)
bin size = 16mm/7
0 个评论
采纳的回答
Image Analyst
2023-6-2
Try this:
% Demo by Image Analyst to separate point sets using the convex hull.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 16;
markerSize = 16;
% https://matlab.fandom.com/wiki/FAQ#How_do_I_create_a_circle?
% Create a logical image of a circle with specified
% diameter, center, and image size.
% First create the image.
imageSizeX = 1600;
imageSizeY = 1600;
[columnsInImage rowsInImage] = meshgrid(1:imageSizeX, 1:imageSizeY);
% Next create the circle in the image.
centerX = 800;
centerY = 800;
radius = 800;
circlePixels = (rowsInImage - centerY).^2 ...
+ (columnsInImage - centerX).^2 <= radius.^2;
% circlePixels is a 2D "logical" array.
% Now, display it.
subplot(2, 2, 1);
image(circlePixels) ;
colormap([0 0 0; 1 1 1]);
title('Binary image of a circle');
axis('on', 'image')
% Now chop it up into 7 by 7 by drawing black lines through it.
blackRows = round(linspace(1, 1600, 8))
img = circlePixels; % Make a copy.
for k = 1 : length(blackRows)
thisRow = blackRows(k);
img(thisRow, :) = false;
img(:, thisRow) = false;
end
% Now, display it.
subplot(2, 2, 2);
imshow(img) ;
title('Chopped into 7x7 grid');
axis('on', 'image')
% Label each blob with 8-connectivity, so we can make measurements of it
[labeledImage, numberOfBlobs] = bwlabel(img, 4);
% Apply a variety of pseudo-colors to the regions.
coloredLabelsImage = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');
% Display the pseudo-colored image.
hFig = figure;
imshow(coloredLabelsImage);
% Get all the blob properties. Can only pass in originalImage in version R2008a and later.
blobMeasurements = regionprops(labeledImage, 'Area', 'Centroid');
numberOfBlobs = size(blobMeasurements, 1);
% Go along each blob showing the area
cmPerPixel = 16 / 1600
for k = 1 : numel(blobMeasurements)
thisArea = blobMeasurements(k).Area * cmPerPixel^2; % In calibrated units, not pixels.
xt = blobMeasurements(k).Centroid(1);
yt = blobMeasurements(k).Centroid(2);
str = sprintf('Area=\n%.2f', thisArea);
text(xt, yt, str, 'Color','w', 'FontSize',10,'FontWeight','bold','HorizontalAlignment','center','VerticalAlignment','middle')
end
hFig = gcf;
hFig.WindowState = 'maximized';
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1401549/image.png)
Areas are rough estimates because the image is discrete/digitized.
0 个评论
更多回答(2 个)
Image Analyst
2023-5-31
Is that 7 mm by 7 mm? Or pixels? If pixels you need to know how many pixels are in a mm.
If you want individual areas of each "bin", then it depends on how the grid lies in the circle.
viscircles([8, 8], 8);
grid on;
xticks(0:7:16)
yticks(0:7:16);
axis equal
% A rough estimate of the average area
aveArea = pi*8^2 / 8
but again, the number of "bins" inside the circle depends on exactly how your grid is lying in the circle. There could just as easily be 4 more bins (total of 12) if the circle above were shifted down and to the left a little bit.
3 个评论
Image Analyst
2023-5-31
It's easy if you have a digital image and knowledge of where the gridlines are. Do you have that? If so, attach the image and let us know what line halfway between rows and columns are the dividing gridlines.
If not, it's a mathematical/geometrical computation. Maybe it could be done by the Symbolic Toolbox but I don't know how. Do you have that toolbox?
Matt J
2023-6-2
编辑:Matt J
2023-6-2
The efficient way would be to first eliminate the grid squares which are wholly inside or outside the circle, which you can determine by testing whether the vertices of a square are all inside/outside the circle.
Once you've narrowed the computation down to the squares straddling the circumference of the circle, you can use polyshape methods to compute intersection area, e.g.,
circle=nsidedpoly(1000); %circular polyshape
squares=[nsidedpoly(4,'Center',[0,1]), nsidedpoly(4,'Center',[0,-1.2])]; %two squares
Areas =area(intersect(circle,squares))
plot([circle,squares]); axis equal
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!