How to calculate the area percentage of the polygons?
3 次查看(过去 30 天)
显示 更早的评论
I have x_limit and y_limit as [69.5,70.5] and [21.2,21.8] inside which their i have plotted many polygons (the coordinates of those polygons are attached with this text). Now i want to calculate the percentage of area occupied by the polygons inside the particular x_limit and y_limit.
Thank you
0 个评论
采纳的回答
Image Analyst
2017-6-30
编辑:Image Analyst
2017-6-30
Try this, using polyarea(). Note though that I am not deducting area for any overlapping portions. In other words, I assume no polygons overlap when computing area fraction:
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 long g;
format compact;
fontSize = 15;
s = load('polygons_coordinates.mat')
output = s.output;
x1 = 69.5
x2 = 70.5;
y1 = 21.2;
y2 = 21.8;
boxArea = (x2-x1) * (y2-y1)
numPolygons = length(output);
colors = hsv(numPolygons);
totalArea = 0;
for k = 1 : length(output)
thisPolygon = output{k};
x = thisPolygon(:, 1);
y = thisPolygon(:, 2);
thisArea = polyarea(x, y);
totalArea = totalArea + thisArea;
patch(x, y, colors(k, :));
hold on;
end
hold off;
grid on;
areaFraction = totalArea / boxArea
caption = sprintf('%d total polygons. Area Fraction = %f', ...
numPolygons, areaFraction);
title(caption, 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
2 个评论
Image Analyst
2017-6-30
John, I agree the question is somewhat ambiguous now that you mention it. For "area fraction" I used the area of the rectangle in the denominator to get the area fraction of the rectangle that is covered by polygons, but it could have been possible that she wanted the area fraction of polygons that were covered by the rectangle, so the total polygon area would have been in the denominator instead of the rectangle area. I think you and I each interpreted it a different way. And you're right that my code does not consider overlap, or polygons that are totally or partially outside the rectangle. That gets a lot trickier so I left that fine tuning to the poster to do on her own.
更多回答(2 个)
John BG
2017-6-29
编辑:John BG
2017-6-30
Hi Sushma
All polygons in polygons_coordinates.mat are within the rectangle [x_limit y_limit]
load('polygons_coordinates','.mat','output')
O=output
xlim=[69.5 70.5];ylim=[21.2 21.8];
[s1 s2]=size(O);
figure(1);hold all
kout=[]
for k=1:1:s2
L=O{k};
Lx=L(:,1);Ly=L(:,2);
plot(Lx,Ly)
if sum(Lx<xlim(1)') || sum(Lx>xlim(2)') || sum(Lx<xlim(1)') || sum(Lx>xlim(2)')
kout=[kout k];
end
end
% the fence
plot([xlim(1) xlim(2) xlim(2) xlim(1) xlim(1)],[ylim(1) ylim(1) ylim(2) ylim(2) ylim(1)],'r','LineWidth',1.5)
Apc=0
for k=1:1:kout
% for each polygon with at least one vertex outside fence find partial percentage area
% add up
Apc=Apc+A0
end
Apc
kout contains the index to the cell of the polygon that has at least 1 vertex outside the rectangle [x_limit y_limit].
kout is empty.
0 个评论
Jan
2017-6-30
编辑:Jan
2017-6-30
If the overlap matters and you have the Mapping toolbox, you can start with the rectangle as polygone, then subtract all polygones by polybool iteratively:
[Rectx, Recty] = polybool('subtraction', Rectx, Recty, x, y);
Finally the area of the remaining polygone can be dertmined by polyarea.
I cannot post some working code, because I do not have the Mapping Toolbox.
1 个评论
Image Analyst
2017-6-30
Nice find Jan. I don't have the mapping toolbox either. I guess there is no equivalent built into base MATLAB - too bad. Determining overlap is easy if you quantize the coordinates onto a digital image and just AND the images together, but to do it analytically with just the coordinates would be very tricky I think.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!