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!