How to fit circles in concave parts of the polygon or binary Image ?
5 views (last 30 days)
Show older comments
I have written the following code, but now I'm stuck. I need to find radius of curvature, which fits best at the concave parts of the figure. Please help.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/324484/image.jpeg)
if true
% cod
% Reading the Image
clc;
close all
img = imread('black.jpg');
figure(1);
imshow(img);
title('
Original Image');
% Gray Image
imgGray = rgb2gray(img);
figure(2);
imshow(imgGray);
title('Gray Image');
% Binary Image
imgbw = imbinarize(imgGray,0.36);
figure(3);
imshow(imgbw);
imgbw= imcomplement(imgbw);
BW2 = imfill(imgbw,'holes');
title('Binary Image');
%boundary of original
boundaries1 = bwboundaries(BW2);
thisBoundary1= boundaries1{1};
xo = thisBoundary1(:,2);
yo = thisBoundary1(:,1);
% smoothening the image
windowSize = 51;
kernel = ones(windowSize) / windowSize ^ 2;
blurryImage = conv2(single(BW2), kernel, 'same');
binaryImage = blurryImage > 0.5; % Rethreshold
figure (4)
imshow(binaryImage)
title('Smoothened Image');
% boundary of smoothened image
boundaries = bwboundaries(binaryImage);
thisBoundary = boundaries{1};
xb = thisBoundary(:,2);
yb = thisBoundary(:,1);
%finding centroid of smoothened Image
measurements = regionprops(binaryImage , 'Centroid');
centroidx = measurements(1).Centroid(1);
centroidy = measurements(1).Centroid(2);
% polygon approximation
p_reduced = reducepoly(thisBoundary,0.001);
xp=p_reduced(:,2);
yp=p_reduced(:,1);
end
I want something like this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/324487/image.jpeg)
4 Comments
Narges Kheradmandi
on 18 Nov 2022
Many thanks for your reply. However, I think your code gives us the fitted circle for each point on the boundary of the abject but we only need the circles which fitted to the curved part of the object(as can be seen in the image that I have uploaded. I need the radius of the green circles in the picture. Do you know how can I extract them?
Image Analyst
on 18 Nov 2022
What is the link to your new question? Have you posted it yet? You can reference this discussion in it, but let's not bug @Deepank Singh with emails about activity on his question anymore. Let's start your own discussion thread.
Answers (1)
Image Analyst
on 28 Jun 2020
I'd get the boundary points for the shape, then use the FAQ:
to fit a circle to a sliding window of points. So you get a radius of curvature for every single boundary point for that one window size. Then I'd do it for every window size in a reasonable range. For each point for each window size, I'd compute the residuals (distance from point to fitted circle). So now you know what window size gave the best fit. And you also know the radius at that location for that window size. Conceptually pretty simple. Give it a try, since I don't have any demo code for that all ready to hand to you. Probably within an hour you could get it done.
5 Comments
Image Analyst
on 16 Nov 2022
The code gives radii all along the border. What is your definition of the "best" circle radii?
See Also
Categories
Find more on Image Processing and Computer Vision 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!