how to fill the interior of a closed surface?

hi dear community members,
please guide me, i have a closed boundary surface. i want to fill the interior.
i am using for loop but i could not succeed.
regards

 Risposta accettata

Star Strider
Star Strider il 5 Feb 2021
Modificato: Star Strider il 5 Feb 2021
Try this:
D = readmatrix('Boundary_closed_1s_3s.txt');
figure
[c,h] = contour(D, [1 1]*0.5);
Levels = h.LevelList;
idx = find(c(1,:) == Levels(1));
Len = c(2,idx);
for k = 1:numel(idx)
x{k} = c(1,idx+1:Len(k));
y{k} = c(2,idx+1:Len(k));
end
[b,v] = boundary(x{1}(:), y{1}(:)); % Introduced In R2014b
figure
fill(x{1}(b), y{1}(b), 'r')
title('Boundary')
figure
fill(x{1}, y{1}, 'g')
title('Filled Contour')
It uses the contour function to extract the outlines of the triangle, then first boundary (R2014b and later) to fill the triangle, and then a second one to fill the contour. Choose the one you want.
For figure(1):
.
EDIT — (5 Feb 2021 at 03:06)
The ‘v’ output of boundary is the area it encloses. I assume it corresponds to pixels, however I cannot determine that. Nothing in the original Question mentioned anything about pixels. It might be possible to use ‘x{1}(b)’ and ‘y{1}(b)’ with the original data to outline tha area in the image. The ‘y’ axis direction in the image will be reversed from the plot direction.

8 Commenti

Thanks Dear Star for guidacne. i am trying it. could you please guide how to count the number of interior cells used to fill the polygon like triangle.
it cannot be run because braces brackets are not supported.
As I mentioned, the ‘v’ output of boundary is the area, that I assume would be the same for pixels. There does not appear to be any morphological operation that I can find in the Image Processing Toolbox documentation that would calculate the area of the interior of the triangle. It only calculates the area of the closed outline, for example:
bin = imbinarize(D);
figure
imagesc(D)
hold on
imagesc(bin)
hold off
Area1 = bwarea(bin)
Area2 = bwarea(bwperim(bin))
I would just go with the calculated area provided by boundary. The only other option would be to trace the boundary yourself, and then do morphological operations on it.
EDIT — (5 Feb 2021 at 4:50)
Anyway, try this:
bin = imbinarize(D);
[c,h] = imcontour(bin);
Lvls = h.LevelList;
Levels = h.LevelList;
idx = find(c(1,:) == Levels(1));
Len = c(2,idx);
for k = 1:numel(idx)
x{k} = c(1,idx+1:Len(k));
y{k} = c(2,idx+1:Len(k));
end
[b,v] = boundary(x{1}(:), y{1}(:)); % Introduced In R2014b
fprintf(1,'Pixel Area = %9.0f\n',v)
producing:
Pixel Area = 15277
That is as good as it is likely to get.
Thanks Star Strider for your cooperation. it works. Regards.
Star Strider, could you please explain to me the steps.
As always, my pleasure!
My code first draws the contour to determine the general outline. The ‘x’ and ‘y’ coordinates of the contours are given in the ‘c’ output of the function (the function properties for that particular data set are given in the ‘h’ output). Extracting this information is not at all straightforward, however not difficult with a bit of experience. The ‘idx’ results are the locations of the start of the requested contour, and the ‘Len’ variable is the length of the requested contours. (Those details are explained in the contour documentation, and better than I can describe it, so I refer you to it.) It then uses the boundary function to draw the general outline of the outer part of the contour, with the associated area. The rest of the code uses the boundary output and the contour information to create the closed polygon and draw the boundary. The fill function is then used to fill the interior of the polygon created by combing those results.
Thanks Star Strider. Regards.
As always, my pleasure!

Accedi per commentare.

Più risposte (1)

David Hill
David Hill il 5 Feb 2021
Modificato: David Hill il 5 Feb 2021
Look at polyshape() (assuming your have the coordinates of your surface)
pgon = polyshape([0 0 1 3], [0 3 3 0]);
a=plot(pgon);
a.FaceColor=[0 1 0];

3 Commenti

Hi David, thanks for reply. if the shape is irregular. can we apply it as well.
Also, can we count the pixels those were colored. i need to count them.
regards
Look at the documentation. Polyshape can handle irregular shapes but the coordinates need to be in order.
doc polyshape
Don't understand your pixel question.
Dear David, i mean, using a.FaceColor=[0 1 0]; we got a colored polygon. can we count how many pixels were used in colored area.

Accedi per commentare.

Categorie

Prodotti

Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by