Display sum of areas of different categories for multiple polygons in a legend
4 views (last 30 days)
Show older comments
I have drawn multiple polygons representing different view-out categories, and have calculated the area of each individual polygon. Now i want to display the sum of the areas for each category. I have 7 categories (Other, Sky, Building, Traffic, Car, Ground and Greenery) but many of the polygons belongs to the same category (Building, Car and Greenery).
Is there a way to easily show the sum of the areas for each category in a legend on the plot? If I run the code now, it will just give me the area for each polygon instead of the area for the category, see image below.
figure;
imagesc([-pi, pi], [-1, 1], bg_img);
V = readtable('polygons_brogade_foraar.csv');
V = table2array(V);
hold on;
polygons = {
'Other', [-pi 1; -pi -1; pi -1; pi 1], 'white';
'Sky', [V(:,1) V(:,2)], '#4DBEEE';
'Building 1', [V(:,3) V(:,4)], '#A2142F';
'Building 2', [V(:,5) V(:,6)], '#A2142F';
'Traffic', [V(:,7) V(:,8)], '#0072BD';
'Car 1', [V(:,9) V(:,10)], '#EDB120';
'Car 2', [V(:,11) V(:,12)], '#EDB120';
'Car 3', [V(:,13) V(:,14)], '#EDB120';
'Ground', [V(:,15) V(:,16)], '#D95319';
'Greenery 1', [V(:,17) V(:,18)], '#77AC30';
'Greenery 2', [V(:,19) V(:,20)], '#77AC30';
'Greenery 3', [V(:,21) V(:,22)], '#77AC30';
'Greenery 4', [V(:,23) V(:,24)], '#77AC30';
'Greenery 5', [V(:,25) V(:,26)], '#77AC30';
};
Q = polyshape(zeros(0,2));
n = size(polygons, 1);
A = zeros(1, n);
P = cell(1, n);
for k = n:-1:1
Pk = polyshape(polygons{k, 2});
A(k) = area(subtract(Pk, Q));
P{k} = Pk;
Q = union(Q, Pk);
end
for k = 1:n
plot(P{k}, 'FaceAlpha', 1, 'FaceColor', polygons{k, 3});
end
for k = 1:n
fprintf('Addition area polygon %d = %.2f\n', k, A(k));
end
legendStrings = cell(n, 1);
for k = 1:n
legendStrings{k} = sprintf('%s (Area: %.2f)', polygons{k, 1}, A(k));
end
legend(legendStrings, 'Location', 'best');
axis([-pi, pi, -1, 1]);
xticks(-pi:pi/2:pi);
xticklabels({'-\pi', '-\pi/2', '0', '\pi/2', '\pi'});
title('Region of Interests (ROIs) of the 360° Video');
xlabel('Longitude');
ylabel('Latitude');
ax = gca;
set(ax, 'FontSize', 15, 'FontName', 'Bookman Old Style');
Hope I explained it well enough.
Thank you in advance,
Louis H
0 Comments
Accepted Answer
Voss
on 6 Aug 2023
Edited: Voss
on 6 Aug 2023
figure;
% imagesc([-pi, pi], [-1, 1], bg_img);
% since I don't have your variable bg_img, I set YDir 'reverse' like imagesc does
set(gca(),'YDir','reverse')
V = readtable('polygons_brogade_foraar.csv');
V = table2array(V);
hold on;
polygons = {
'Other', [-pi 1; -pi -1; pi -1; pi 1], 'white';
'Sky', V(:,[1 2]), '#4DBEEE';
'Building 1', V(:,[3 4]), '#A2142F';
'Building 2', V(:,[5 6]), '#A2142F';
'Traffic', V(:,[7 8]), '#0072BD';
'Car 1', V(:,[9 10]), '#EDB120';
'Car 2', V(:,[11 12]), '#EDB120';
'Car 3', V(:,[13 14]), '#EDB120';
'Ground', V(:,[15 16]), '#D95319';
'Greenery 1', V(:,[17 18]), '#77AC30';
'Greenery 2', V(:,[19 20]), '#77AC30';
'Greenery 3', V(:,[21 22]), '#77AC30';
'Greenery 4', V(:,[23 24]), '#77AC30';
'Greenery 5', V(:,[25 26]), '#77AC30';
};
Q = polyshape(zeros(0,2));
n = size(polygons, 1);
A = zeros(1, n);
P = cell(1, n);
for k = n:-1:1
Pk = polyshape(polygons{k, 2});
A(k) = area(subtract(Pk, Q));
P{k} = Pk;
Q = union(Q, Pk);
end
[category_names, h_plot_idx, category_idx] = unique( regexprep( polygons(:,1), ' \d+$', ''), 'stable');
category_sums = splitapply(@sum, A(:), category_idx);
h_plot = zeros(1,n);
for k = 1:n
h_plot(k) = plot(P{k}, 'FaceAlpha', 1, 'FaceColor', polygons{k, 3});
end
for k = 1:n
fprintf('Addition area polygon %d = %.2f\n', k, A(k));
end
m = numel(category_names);
legendStrings = cell(m, 1);
for k = 1:m
legendStrings{k} = sprintf('%s (Area: %.2f)', category_names{k}, category_sums(k));
end
legend(h_plot(h_plot_idx), legendStrings, 'Location', 'best');
axis([-pi, pi, -1, 1]);
xticks(-pi:pi/2:pi);
xticklabels({'-\pi', '-\pi/2', '0', '\pi/2', '\pi'});
title('Region of Interests (ROIs) of the 360° Video');
xlabel('Longitude');
ylabel('Latitude');
ax = gca;
set(ax, 'FontSize', 15, 'FontName', 'Bookman Old Style');
2 Comments
More Answers (0)
See Also
Categories
Find more on Lighting, Transparency, and Shading in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!