clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 14;
folder = 'D:\Temporary Stuff';
baseFileName = 'USM16.jpg';
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
fullFileName = baseFileName;
if ~exist(fullFileName, 'file')
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorBands] = size(rgbImage);
subplot(3, 3, 1);
imshow(rgbImage);
title('Original Color Image', 'FontSize', fontSize);
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0, 1, 1]);
hsv = rgb2hsv(rgbImage);
hImage = hsv(:, :, 1);
sImage = hsv(:, :, 2);
vImage = hsv(:, :, 3);
subplot(3, 3, 4);
imshow(hImage, []);
title('Hue Image', 'FontSize', fontSize);
subplot(3, 3, 5);
imshow(sImage, []);
title('Saturation Image', 'FontSize', fontSize);
subplot(3, 3, 6);
imshow(vImage, []);
title('Value Image', 'FontSize', fontSize);
[pixelCount, grayLevels] = imhist(hImage);
subplot(3, 3, 7);
bar(grayLevels, pixelCount);
grid on;
title('Histogram of Hue image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]);
[pixelCount, grayLevels] = imhist(sImage);
subplot(3, 3, 8);
bar(grayLevels, pixelCount);
grid on;
title('Histogram of Saturation image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]);
[pixelCount, grayLevels] = imhist(vImage);
subplot(3, 3, 9);
bar(grayLevels, pixelCount);
grid on;
title('Histogram of Value image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]);
hueBinary = hImage > 0.2 | hImage < 0.1;
saturationBinary = sImage > 0.22;
valueBinary = vImage > 0.28 & vImage < 0.9;
binaryImage = hueBinary & saturationBinary & valueBinary;
binaryImage = bwareaopen(binaryImage, 400);
binaryImage = imfill(binaryImage, 'holes');
subplot(3, 3, 2);
imshow(binaryImage, []);
axis on;
title('Cells Image', 'FontSize', fontSize);
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0, 1, 1]);
[labeledImage, numberOfBlobs] = bwlabel(binaryImage, 8);
coloredLabelsImage = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');
subplot(3, 3, 3);
imshow(coloredLabelsImage);
title('Labeled Image', 'FontSize', fontSize);
blobMeasurements = regionprops(labeledImage, 'all')
numberOfBlobs = size(blobMeasurements, 1)