clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 20;
folder = pwd;
baseFileName = 'problem.png';
grayImage = imread(baseFileName);
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
grayImage = min(grayImage, [], 3);
end
subplot(2, 3, 1);
imshow(grayImage, []);
axis('on', 'image');
title('Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
hFig = gcf;
hFig.WindowState = 'maximized';
drawnow;
subplot(2, 3, 2);
imhist(grayImage);
grid on;
grayImage = adapthisteq(grayImage);
subplot(2, 3, 3);
imshow(grayImage);
axis('on', 'image');
title('Flattened Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
verticalProfile = mean(grayImage, 2);
subplot(2, 3, 4);
plot(verticalProfile);
grid on;
threshold = 110;
yline(threshold, 'Color', 'r', 'LineWidth', 2);
title('Vertical Profile', 'FontSize', fontSize);
binaryImage = imfill(grayImage < threshold, 'holes');
binaryImage = bwareafilt(binaryImage, 3);
binaryImage = imopen(binaryImage, true(1, 3));
binaryImage = bwareafilt(binaryImage, 3);
subplot(2, 3, 5);
imshow(binaryImage);
axis('on', 'image');
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
binaryProfile = (verticalProfile > threshold)';
bandStarts = strfind(binaryProfile, [0, 1])
bandStops = strfind(binaryProfile, [1, 0])
for k = 1 : length(bandStarts)
yline(bandStarts(k), 'Color', 'r', 'LineWidth', 2);
yline(bandStops(k), 'Color', 'r', 'LineWidth', 2);
end
Best Answer