clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 20;
folder = pwd;
baseFileName = '91901.jpg';
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)
if numberOfColorChannels > 1
grayImage = rgbImage(:, :, 2);
else
grayImage = rgbImage;
end
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
subplot(2, 2, 2);
imhist(grayImage);
grid on;
title('Histogram of Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
for windowSize = 31 : 2 : 61
edgeImage = imtophat(grayImage, true(windowSize));
subplot(2, 2, 3);
imshow(edgeImage, []);
caption = sprintf('Top Hat Filter with size %d', windowSize);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
binaryImage = imbinarize(edgeImage);
binaryImage = bwareafilt(binaryImage, 1);
subplot(2, 2, 4);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
topRows = zeros(1, columns);
for col = 1 : columns
row = find(binaryImage(:, col), 1, 'first');
if ~isempty(row)
topRows(col) = row;
end
end
firstCol = find(topRows, 1, 'first');
topRows(1 : firstCol) = topRows(firstCol);
lastCol = find(topRows, 1, 'last');
topRows(lastCol : end) = topRows(lastCol);
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
hold on;
plot(topRows, 'r-', 'LineWidth', 3);
hold off;
promptMessage = sprintf('Showing filter window size of %d\n\nDo you want to enlarge the window,\nor Accept this size?', windowSize);
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Next', 'Accept and continue', 'Next');
if contains(buttonText, 'Accept', 'IgnoreCase', true)
break;
end
end
topRows
Best Answer