# How to calculate the area percentage of the polygons?

15 views (last 30 days)
SUSHMA MB on 29 Jun 2017
Commented: Image Analyst on 30 Jun 2017
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

Image Analyst on 30 Jun 2017
Edited: Image Analyst on 30 Jun 2017
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;
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);

John BG on 30 Jun 2017
Hi IA
the way you sum the polygons you are assuming that they are not overlapping.
If you zoom in you will realise that some polygons actually overlap, therefore it's not correct to simply keep adding the area of all the polygons in the .mat file
John BG
Image Analyst on 30 Jun 2017
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.

John BG on 29 Jun 2017
Edited: John BG on 30 Jun 2017
Hi Sushma
All polygons in polygons_coordinates.mat are within the rectangle [x_limit y_limit]
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
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.

Jan on 30 Jun 2017
Edited: Jan on 30 Jun 2017
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 Comment

Image Analyst on 30 Jun 2017
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.