7 views (last 30 days)

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;

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);

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 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]

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.

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.

Image Analyst
on 30 Jun 2017

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
## 0 Comments

Sign in to comment.