clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 25;
baseFileName = 'L4.jpg';
folder = [];
fullFileName = fullfile(folder, baseFileName);
grayImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
grayImage = rgb2gray(grayImage);
end
subplot(2, 2, 1);
imshow(grayImage, []);
axis on;
axis image;
caption = sprintf('Original Gray Scale Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
[pixelCount, grayLevels] = imhist(grayImage);
subplot(2, 2, 2);
bar(grayLevels, pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Gray Level', 'FontSize', fontSize);
ylabel('Pixel Count', 'FontSize', fontSize);
xlim([0 grayLevels(end)]);
mask = grayImage < 125;
subplot(2, 2, 3);
imshow(mask);
axis on;
axis image;
title('Binary Image Mask', 'fontSize', fontSize);
drawnow;
mask = imclearborder(mask);
mask = bwareafilt(mask, 1);
subplot(2, 2, 4);
imshow(mask);
axis on;
axis image;
title('Lobster-only Mask', 'FontSize', fontSize);
drawnow;
subplot(2, 2, 4);
mask = ~bwareaopen(~mask, 1000);
imshow(mask);
axis on;
axis image;
title('Final Cleaned Mask', 'FontSize', fontSize);
drawnow;
Best Answer