Draw a circle within an irregular polygon using viscircle and MinFeretDiameter

1 view (last 30 days)
I have created a scrript that allows me to measure the minimum feret diameter along with the rradius and place a circle within the irregular polygon. However the circle sometimes is drawn outside of the polygon. how do i superimpose the circle within the polygon without changing the feret diameter manually?
%Upload the image
[Filename, Dirpath] = uigetfile({'*.tif;*.jpeg;*.jpg;*.bmp;*.png','All Image Files'},'Select an image file');
if isequal(Filename,0)
return
end
I = imread(fullfile(Dirpath, Filename));
%Black and white image
bw = im2bw(I);
%Filter The Image
%When using bwareafilt you need to know number of mesh, smallest to largest
%are of mesh or
bw2 = bwareafilt(bw,[10000 Inf]); %use bw2 = bwareafilt(bw,[min diameter max diameter])when known range of mesh
imshow (bw2)
%Creates Boundaries of the pores to recognize them
[B,L] = bwboundaries(bw2,'noholes');
imshow(L);
hold on
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'b', 'LineWidth', 2)
end
hold on
%Crreates a table of specified regions
analysis = regionprops(bw2, {'Centroid','MinFeretProperties','MaxFeretProperties','PixelList'}) %'table
table = struct2table(analysis)
hold on
%Plots the centriod
for k = 1:length(analysis)
%Rename points in centriod
Xc = analysis(k).Centroid(1);
Yc = analysis(k).Centroid(2);
centers = [Xc Yc];
%Change Points of coordinates found from vector to a scalar
xmin = [analysis(k).MinFeretCoordinates(1,1) analysis(k).MinFeretCoordinates(2,1)];
ymin = [analysis(k).MinFeretCoordinates(1,2) analysis(k).MinFeretCoordinates(2,2)];
%xmax = [analysis(k).MaxFeretCoordinates(1,1) analysis(k).MaxFeretCoordinates(2,1)];
%ymax = [analysis(k).MaxFeretCoordinates(1,2) analysis(k).MaxFeretCoordinates(2,2)];
diameter = sqrt(((analysis(k).MinFeretCoordinates(2,1)- analysis(k).MinFeretCoordinates(1,1))^2)+...
((analysis(k).MinFeretCoordinates(2,2)- analysis(k).MinFeretCoordinates(1,2))^2));
radii = diameter/2;
%Draw Circle in Specified points
viscircles(centers, radii);
%plot(xmax,ymax);
plot(xmin,ymin);
plot(centers (:,1), centers(:,2), 'b*') %Plots the centriod
end

Answers (1)

Suraj Kumar
Suraj Kumar on 24 May 2024
Hi Simeon,
To solve the problem of drawing circles that sometimes extend outside the polygons, we can use a modified approach to ensure that the circles are drawn within the polygon boundaries. The solution involves calculating the maximum inscribed circle for each polygon, which guarantees that the circle will fit inside the polygon.
You can follow these steps:
1. First, identify the boundaries of each polygon in the image using bwboundaries and then create a binary mask that isolates the polygon from the rest of the image.
[B, ~] = bwboundaries(bw2,'noholes');
% Loop through each object and find the maximum inscribed circle
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'b', 'LineWidth', 2)
% Create a mask for the current object
objMask = poly2mask(boundary(:,2), boundary(:,1), size(bw2,1), size(bw2,2));
2. Then, apply distance transformation on each binary mask using “bwdist” which calculates the distance from each pixel to the nearest boundary pixel, effectively identifying the center and radius of the maximum inscribed circle.
D = bwdist(~objMask);
% Find the pixel with the maximum distance value
[MaxDist, idx] = max(D(:));
[cy, cx] = ind2sub(size(D), idx); % Convert linear index to x, y coordinates
radius = MaxDist; % The maximum distance is the radius of the inscribed circle
3. Finally, draw the circles using the identified centers and radii for each polygon, ensuring they fit perfectly inside the polygon boundaries.
viscircles([cx, cy], radius, 'EdgeColor', 'r');
plot(cx, cy, 'r*');
You can refer the output below for better understanding:
For more information on thebwboundaries”,” bwdist” and “viscircles” functions in MATLAB, you can go through the following documentations:
  1. https://www.mathworks.com/help/images/ref/bwboundaries.html
  2. https://www.mathworks.com/help/images/ref/bwdist.html
  3. https://www.mathworks.com/help/images/ref/viscircles.html
Hope this helps!

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!