clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 20;
folder = pwd;
baseFileName = 'seg.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);
subplot(2, 2, 1);
imshow(rgbImage, []);
title('Original RGB Color Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
[rows, columns, numberOfColorChannels] = size(rgbImage)
if numberOfColorChannels > 1
grayImage = rgbImage(:, :, 2);
else
grayImage = rgbImage;
end
subplot(2, 2, 2);
[counts, binLocations] = imhist(grayImage);
grid on;
title('Histogram of Image', 'FontSize', fontSize, 'Interpreter', 'None');
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
binaryImage = grayImage > 128;
allWhiteRows = all(binaryImage, 2);
binaryImage = binaryImage(~allWhiteRows, :);
allWhiteColumns = all(binaryImage, 1);
binaryImage = binaryImage(:, ~allWhiteColumns);
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Cropped Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
horizontalProfile = sum(binaryImage, 1);
subplot(2, 3, 4);
plot(horizontalProfile, 'b-', 'LineWidth', 2);
grid on;
title('Horizontal Profile', 'FontSize', fontSize, 'Interpreter', 'None');
midColumn = round(columns/2)
[~, leftMaxIndex] = max(horizontalProfile(1:midColumn))
[~, rightMaxIndex] = max(horizontalProfile(midColumn+1:end));
rightMaxIndex = rightMaxIndex + midColumn
hold on;
line([leftMaxIndex, leftMaxIndex], ylim, 'Color', 'r', 'LineWidth', 2);
line([rightMaxIndex, rightMaxIndex], ylim, 'Color', 'r', 'LineWidth', 2);
[~, minIndex] = min(horizontalProfile(leftMaxIndex+1:rightMaxIndex-1));
minIndex = minIndex + leftMaxIndex
line([minIndex, minIndex], ylim, 'Color', 'r', 'LineWidth', 2);
leftImage = binaryImage(:, 1:minIndex);
rightImage = binaryImage(:, minIndex+1:end);
subplot(2, 3, 5);
imshow(leftImage);
title('Left Half Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
subplot(2, 3, 6);
imshow(rightImage);
title('Right Half Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
Best Answer