clc;
fprintf('Beginning to run %s.m ...\n', mfilename);
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 15;
s = load('c2.mat')
binaryImage = s.preenc;
subplot(2, 3, 1)
imshow(binaryImage);
title('Original Image', 'FontSize', fontSize);
xlabel('x', 'FontSize', fontSize);
ylabel('y', 'FontSize', fontSize);
horizontalProfile = sum(binaryImage, 1);
subplot(2, 3, 2)
plot(horizontalProfile, 'b-', 'LineWidth', 2);
grid on;
title('Sum of Pixels in Each Column', 'FontSize', fontSize);
xlabel('Column', 'FontSize', fontSize);
ylabel('Count of Pixels In This Column', 'FontSize', fontSize);
subplot(2, 3, 3)
cProfile = cumsum(horizontalProfile);
cProfile = cProfile / cProfile(end) * 100;
plot(cProfile, 'b-', 'LineWidth', 2);
title('Normalized Count of Pixels', 'FontSize', fontSize);
xlabel('Column', 'FontSize', fontSize);
ylabel('Percentage', 'FontSize', fontSize);
yline(50, 'Color', 'r', 'LineWidth', 2);
grid on;
lastCol = find(cProfile < 50, 1, 'last');
xline(lastCol, 'Color', 'r', 'LineWidth', 2);
leftHalf = binaryImage(:, 1:lastCol);
rightHalf = binaryImage(:, lastCol + 1 : end);
subplot(2, 4, 5)
imshow(leftHalf);
title('Left Half Image, Based on Area', 'FontSize', fontSize);
subplot(2, 4, 6)
imshow(rightHalf);
title('Right Half Image, Based on Area', 'FontSize', fontSize);
fprintf('Left Half goes from column 1 to column %d, based on areas.\n', lastCol);
[r, c] = find(binaryImage);
col1 = min(c);
col2 = max(c);
midColumn = (col1 + col2) / 2
leftHalf = binaryImage(:, 1:floor(midColumn));
rightHalf = binaryImage(:, ceil(midColumn) + 1 : end);
subplot(2, 4, 7)
imshow(leftHalf);
title('Left Half Image, Based on Width', 'FontSize', fontSize);
subplot(2, 4, 8)
imshow(rightHalf);
title('Right Half Image, Based on Width', 'FontSize', fontSize);
fprintf('Left Half goes from column 1 to column %d, based on caliper width.\n', floor(midColumn));
g = gcf;
g.WindowState = 'maximized'