clc;
close all;
clearvars;
workspace;
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
folder = [];
baseFileName = 'purple spots.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)
subplot(3, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
hp = impixelinfo();
caption = sprintf('Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
hFig1 = gcf;
hFig1.Units = 'Normalized';
hFig1.WindowState = 'maximized';
hFig1.Name = 'Demo by Image Analyst';
[binaryImage, maskedRGBImage] = createMask(rgbImage);
subplot(3, 2, 3);
imshow(binaryImage, []);
hp = impixelinfo();
axis('on', 'image');
title('Initial Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
subplot(3, 2, 4);
imshow(maskedRGBImage, []);
hp = impixelinfo();
axis('on', 'image');
title('Initial Masked Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
props = regionprops(binaryImage, 'Area');
allAreas = sort([props.Area])
binaryImage = bwareaopen(binaryImage, 500);
subplot(3, 2, 5);
imshow(binaryImage, []);
hp = impixelinfo();
axis('on', 'image');
title('Final Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
maskedRGBImage = bsxfun(@times, rgbImage, cast(binaryImage, 'like', rgbImage));
subplot(3, 2, 6);
imshow(maskedRGBImage, []);
hp = impixelinfo();
axis('on', 'image');
title('Final Masked Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
props = regionprops(binaryImage, 'Area');
individualAreas = [props.Area]
totalArea = sum(individualAreas)
numPurpleSpots = numel(props)
subplot(3, 2, 2);
bar(individualAreas);
grid on;
title('Circle Areas', 'FontSize', fontSize);
xlabel('Circle Index', 'FontSize', fontSize);
ylabel('Area in Pixels', 'FontSize', fontSize);
function [BW,maskedRGBImage] = createMask(RGB)
I = RGB;
channel1Min = 0.000;
channel1Max = 240.000;
channel2Min = 82.000;
channel2Max = 228.000;
channel3Min = 0.000;
channel3Max = 219.000;
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
maskedRGBImage = RGB;
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
0 Comments
Sign in to comment.