Azzera filtri
Azzera filtri

How to fit circles in concave parts of the polygon or binary Image ?

5 visualizzazioni (ultimi 30 giorni)
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.
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:
  4 Commenti
Narges Kheradmandi
Narges Kheradmandi il 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
Image Analyst il 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.

Accedi per commentare.

Risposte (1)

Image Analyst
Image Analyst il 28 Giu 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 Commenti
Deepank Singh
Deepank Singh il 29 Giu 2020
Hey, I tried what you said, but still don't know how to get the best circle,
if true
% concave portion detection
x=0;
for i=1:length(D(:,1))-4
% lineseg1 = [ D(i,1) D(i,2);D(i+1,1) D(i+1,2) ];
% lineseg2 = [ D(i,1) D(i,2);D(i+2,1) D(i+2,2) ];
% [in,out1] = intersect(poly1,lineseg1);
% [in,out2] = intersect(poly1,lineseg2);
% if(isempty(out1))
% if(isempty(out2))
[xr, yr, r, a] = circfit(D(i:i+2,1),D(i:i+2,2));
th = 0:pi/50:2*pi;
xunit = r * cos(th) + xr;
yunit = r * sin(th) + yr;
poly0 = polyshape(xunit(1:end-1),yunit(1:end-1));
poly2 = intersect(poly0,poly1);
A1 = area(poly0);
A2 =area(poly2);
overlap = A2/A1;
if(r<1.0*Inradius && inpolygon(xr,yr,xp,yp) && overlap>0.98)
x=x+1;
roc(x,:)= [xr yr r];
end
[xr, yr, r, a] = circfit(D(i:i+3,1),D(i:i+3,2));
th = 0:pi/50:2*pi;
xunit = r * cos(th) + xr;
yunit = r * sin(th) + yr;
poly0 = polyshape(xunit(1:end-1),yunit(1:end-1));
poly2 = intersect(poly0,poly1);
A1 = area(poly0);
A2 =area(poly2);
overlap = A2/A1;
if(r<1.0*Inradius && inpolygon(xr,yr,xp,yp) && overlap>0.98)
x=x+1;
roc(x,:)= [xr yr r];
end
[xr, yr, r, a] = circfit(D(i:i+4,1),D(i:i+4,2));
th = 0:pi/50:2*pi;
xunit = r * cos(th) + xr;
yunit = r * sin(th) + yr;
poly0 = polyshape(xunit(1:end-1),yunit(1:end-1));
poly2 = intersect(poly0,poly1);
A1 = area(poly0);
A2 =area(poly2);
overlap = A2/A1;
if(r<1.0*Inradius && inpolygon(xr,yr,xp,yp) && overlap>0.98)
x=x+1;
roc(x,:)= [xr yr r];
end
% end
% end
end
end
This is what I got
Image Analyst
Image Analyst il 16 Nov 2022
The code gives radii all along the border. What is your definition of the "best" circle radii?

Accedi per commentare.

Categorie

Scopri di più su Image Processing and Computer Vision in Help Center e File Exchange

Prodotti


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by