Filter löschen
Filter löschen

Find area of a polygon portion of an Image (Code generatable code)

4 Ansichten (letzte 30 Tage)
D D
D D am 19 Dez. 2022
Beantwortet: Karim am 23 Dez. 2022
I want to calculate the area of the a portion of binary image, as shown in example.
I know, there are different different options in MATLAB to do so, roipoly, poly2mask, drawpolygon, regionfill.....
But my problem is, since I work in simulink, I am supposed to add code generatable code.
Could someone please help me with a logic to find area of this polygon portion, with a code generatable code.

Antworten (1)

Karim
Karim am 23 Dez. 2022
For such a shape, you could determine the area via a set of triangles. Insert a new temporary point in the center of the shape. Use this point to find a set of triangles (each time take 2 consecutive points). Then you can vind the area as half the magnitue of the cross product of the sides of the triangle.
See below for a demonstration and an example function:
img = imread("image (1).png");
[h,w,~] = size(img);
h = 182
w = 325
% approx locations of the points
Points = [0.235412585196718, 0.4210111787843795
0.413220458790229, 0.3425540277443474
0.548851196334065, 0.5019042628065483
0.540891377802694, 0.8655011562683762
0.382395775888072, 0.8977730789035103
0.258763417435734, 0.7566641581177962];
% convert point locations into pixel coordinates
Points(:,1) = round(Points(:,1) * w);
Points(:,2) = round((1-Points(:,2)) * h);
% plot the figure to see if we have the points correct
figure
hold on
imshow(img)
scatter(Points(:,1),Points(:,2),'y','filled')
title('Plot approx points in yellow')
% first find the center point
cent_Point = mean(Points,1);
% plot the points and the center point
figure
hold on
scatter(Points(:,1),Points(:,2),'y','filled','MarkerEdgeColor','k')
text(Points(:,1),Points(:,2)," "+num2str((1:size(Points,1))'))
scatter(cent_Point(:,1),cent_Point(:,2),'r','filled')
hold off
grid on
% not that you can see the resembles of a set of triangles in the figure:
% each time take the center point and connect it with two points
% create a list of the triangles, each row represents the vertices of a
% triangle
numPoints = size(Points,1);
Triangles = [repmat(numPoints+1,numPoints-1,1) (1:(numPoints-1))' (2:numPoints)']
Triangles = 5×3
7 1 2 7 2 3 7 3 4 7 4 5 7 5 6
% now the only thing left to do is to sum the area's of the triangles
% we can use the cross-product of two sides of the triangle, since
% the area of a triangle then half the magnitude
% first expand the grid to include the center point, we will also add a 3d
% dimension to vectorize the cross product
Grid = [Points; cent_Point];
Grid(:,3) = 0;
A = Grid(Triangles(:, 2), :) - Grid(Triangles(:, 1), :);
B = Grid(Triangles(:, 3), :) - Grid(Triangles(:, 1), :);
C = cross(A, B, 2);
area = 1/2 * sum(sqrt(sum(C.^2, 2)))
area = 6.5915e+03
so to summarize, you could use the following function:
function area = GetPolyArea(Points)
numPoints = size(Points,1);
% first find the center point
cent_Point = mean(Points,1);
% create the vertex list for the triangles
Triangles = [repmat(numPoints+1,numPoints-1,1) (1:(numPoints-1))' (2:numPoints)']
% set up the grid points
Grid = [Points; cent_Point];
Grid(:,3) = 0;
% evaluate the cross product
A = Grid(Triangles(:, 2), :) - Grid(Triangles(:, 1), :);
B = Grid(Triangles(:, 3), :) - Grid(Triangles(:, 1), :);
C = cross(A, B, 2);
% obtain the area
area = 1/2 * sum(sqrt(sum(C.^2, 2)));
end

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by