clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 20;
startingFolder = pwd
if ~exist(startingFolder, 'dir')
startingFolder = pwd;
end
defaultFileName = fullfile(startingFolder, '*.jpg');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
return;
end
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
subplot(1, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
drawnow;
binaryImage = grayImage > 50;
binaryImage = imclearborder(binaryImage);
binaryImage = bwareafilt(binaryImage, 1);
subplot(1, 2, 2);
imshow(binaryImage, []);
title('Binary Image with line down it', 'FontSize', fontSize, 'Interpreter', 'None');
hold on;
minRequiredDistance = 10;
x = NaN(rows, 1);
y = NaN(rows, 1);
for row = 1: rows
col1 = find(binaryImage(row,:), 1, 'first');
col2 = find(binaryImage(row,:), 1, 'last');
if col2-col1 > minRequiredDistance
x(row) = (col1 + col2) / 2;
y(row) = row;
end
end
plot(x, y, 'r-', 'MarkerSize', 8, 'LineWidth', 2);
helpdlg('Done!');
Best Answer