clc;
close all;
workspace;
format long g;
format compact;
fontSize = 25;
baseFileName = '2_Ink_LI.jpg';
folder = fileparts(which(baseFileName));
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
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage);
subplot(2, 2, 1);
imshow(rgbImage, []);
axis on;
caption = sprintf('Original Color Image, %s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
hp = impixelinfo();
[rows, columns, numberOfColorChannels] = size(rgbImage)
if numberOfColorChannels > 1
grayImage = rgbImage(:, :, 1);
end
grayImage([1:150, 750:end], :) = 255;
grayImage(:, [1:150, 750:end]) = 255;
subplot(2, 2, 2);
imshow(grayImage, []);
axis on;
caption = sprintf('Gray Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
drawnow;
binaryImage = grayImage < 128;
subplot(2, 2, 3);
imshow(binaryImage, []);
axis on;
caption = sprintf('Binary Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
drawnow;
props = regionprops(binaryImage, 'Centroid');
centroids = [props.Centroid]
xCentroids = centroids(1:2:end)';
yCentroids = centroids(2:2:end)';
[indexes, rowcenters] = kmeans(yCentroids, 6)
hold on;
for k = 1 : length(rowcenters)
line([1, columns], [rowcenters(k), rowcenters(k)], 'Color', 'r');
end
subplot(2, 2, 4);
imshow(rgbImage, []);
axis on;
caption = sprintf('Original Color Image, %s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hold on;
xFit = 1 : columns;
for k = 1 : length(rowcenters)
theseIndexes = indexes == k;
theseX = xCentroids(theseIndexes)
theseY = yCentroids(theseIndexes)
coefficients = polyfit(theseX, theseY, 1);
yFit = polyval(coefficients, xFit);
plot(xFit, yFit, 'g-');
end