clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 22;
folder = pwd;
baseFileName = 'image.jpeg';
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
fullFileNameOnSearchPath = baseFileName;
if ~exist(fullFileNameOnSearchPath, 'file')
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
grayImage = rgb2gray(grayImage);
end
binaryImage = imbinarize(grayImage);
subplot(3, 1, 1);
imshow(binaryImage, []);
impixelinfo;
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
hFig = gcf;
hFig.WindowState = 'maximized';
drawnow;
for col = 1 : columns
thisCol = binaryImage(:, col);
biggestBlob = bwareafilt(thisCol, 1);
thisCol(biggestBlob) = false;
binaryImage(:, col) = thisCol;
end
subplot(3, 1, 2);
imshow(binaryImage)
grid on;
title('Without bottom arc', 'FontSize', fontSize, 'Interpreter', 'None');
[y, x] = find(binaryImage);
x = x + 0.001 * rand(1, length(x));
[x, sortOrder] = sort(x, 'ascend');
y = y(sortOrder);
ySmoothed = movmean(y, 5);
subplot(3, 1, 3);
imshow(binaryImage)
grid on;
title('With Fitted Curve in Red', 'FontSize', fontSize, 'Interpreter', 'None');
hold on
plot(x, ySmoothed, 'r-', 'LineWidth', 3);
Best Answer