clc;
close all;
imtool close all;
clear;
workspace;
format long g;
format compact;
fontSize = 16;
folder = pwd;
baseFileName = 'arc.png';
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
fullFileName = baseFileName;
if ~exist(fullFileName, 'file')
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
grayImage = rgb2gray(grayImage);
end
subplot(2, 2, 1);
imshow(grayImage);
axis on;
impixelinfo;
caption = sprintf('Original Image : %s', baseFileName);
title(caption, 'FontSize', fontSize);
impixelinfo;
g = gcf;
g.WindowState = 'maximized';
g.NumberTitle = 'off';
g.Name = 'Demo by Image Analyst'
drawnow;
subplot(2, 2, 2);
imhist(grayImage);
grid on;
xlabel('Gray Level', 'FontSize', fontSize);
ylabel('Pixel Count', 'FontSize', fontSize);
threshold = 11000;
xline(threshold, 'Color', 'r', 'LineWidth', 2);
mask = grayImage > threshold;
mask = imfill(mask, 'holes');
subplot(2, 2, 3);
imshow(mask);
title('Initial Mask', 'FontSize', fontSize);
drawnow;
se = true(7, 1);
mask = imclose(mask, se);
mask = bwareafilt(mask, 1);
subplot(2, 2, 4);
imshow(mask);
title('Final Mask with Red Circles Overlaid', 'FontSize', fontSize);
drawnow;
leftCol = zeros(1, columns);
rightCol = zeros(1, columns);
for row = 1 : rows
thisRow = mask(row, :);
t = find(thisRow, 1, 'first');
if ~isempty(t)
leftCol(row) = t;
rightCol(row) = find(thisRow, 1, 'last');
end
end
x = 1 : rows;
noData = leftCol == 0;
x(noData) = [];
leftCol(noData) = [];
rightCol(noData) = [];
[ycLeft, xcLeft, leftRadius, a] = circfit(x, leftCol);
viscircles([xcLeft, ycLeft], leftRadius);
[ycRight, xcRight, rightRadius, a] = circfit(x, rightCol);
viscircles([xcRight, ycRight], rightRadius);
msgbox('Done! Thank you Image Analyst!');
function [xc,yc,R,a] = circfit(x,y)
n=length(x); xx=x.*x; yy=y.*y; xy=x.*y;
A=[sum(x) sum(y) n;sum(xy) sum(yy) sum(y);sum(xx) sum(xy) sum(x)];
B=[-sum(xx+yy) ; -sum(xx.*y+yy.*y) ; -sum(xx.*x+xy.*y)];
a=A\B;
xc = -.5*a(1);
yc = -.5*a(2);
R = sqrt((a(1)^2+a(2)^2)/4-a(3));
end
11 Comments
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/651848-how-do-i-get-two-circles-from-a-thick-noisy-arc#comment_1142388
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/651848-how-do-i-get-two-circles-from-a-thick-noisy-arc#comment_1142388
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/651848-how-do-i-get-two-circles-from-a-thick-noisy-arc#comment_1142408
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/651848-how-do-i-get-two-circles-from-a-thick-noisy-arc#comment_1142408
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/651848-how-do-i-get-two-circles-from-a-thick-noisy-arc#comment_1142463
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/651848-how-do-i-get-two-circles-from-a-thick-noisy-arc#comment_1142463
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/651848-how-do-i-get-two-circles-from-a-thick-noisy-arc#comment_1142798
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/651848-how-do-i-get-two-circles-from-a-thick-noisy-arc#comment_1142798
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/651848-how-do-i-get-two-circles-from-a-thick-noisy-arc#comment_1142873
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/651848-how-do-i-get-two-circles-from-a-thick-noisy-arc#comment_1142873
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/651848-how-do-i-get-two-circles-from-a-thick-noisy-arc#comment_1142918
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/651848-how-do-i-get-two-circles-from-a-thick-noisy-arc#comment_1142918
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/651848-how-do-i-get-two-circles-from-a-thick-noisy-arc#comment_1142973
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/651848-how-do-i-get-two-circles-from-a-thick-noisy-arc#comment_1142973
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/651848-how-do-i-get-two-circles-from-a-thick-noisy-arc#comment_1142983
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/651848-how-do-i-get-two-circles-from-a-thick-noisy-arc#comment_1142983
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/651848-how-do-i-get-two-circles-from-a-thick-noisy-arc#comment_1143033
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/651848-how-do-i-get-two-circles-from-a-thick-noisy-arc#comment_1143033
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/651848-how-do-i-get-two-circles-from-a-thick-noisy-arc#comment_1143788
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/651848-how-do-i-get-two-circles-from-a-thick-noisy-arc#comment_1143788
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/651848-how-do-i-get-two-circles-from-a-thick-noisy-arc#comment_1145413
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/651848-how-do-i-get-two-circles-from-a-thick-noisy-arc#comment_1145413
Sign in to comment.