fprintf('Beginning to run %s.m ...\n', mfilename);
baseFileName = 'assorted objects.jpeg';
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));
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage)
caption = sprintf('Original RGB Image : "%s"\n%d rows by %d columns', baseFileName, rows, columns);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hFig1.Units = 'Normalized';
hFig1.WindowState = 'maximized';
hFig1.Name = 'Demo by Image Analyst';
[mask, maskedRGBImage] = createMask(rgbImage);
mask = imfill(mask, 'holes');
props = regionprops(mask, 'Area')
allAreas = sort([props.Area], 'Descend')
histogram([props.Area], 100)
mask = bwareaopen(mask, 20);
title('Initial Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
props = regionprops(mask, 'BoundingBox', 'Centroid', 'Image')
numRegions = length(props)
title('Final Mask', 'FontSize', fontSize, 'Interpreter', 'None');
title('Objects Outlined', 'FontSize', fontSize, 'Interpreter', 'None');
boundaries = bwboundaries(mask);
numberOfBoundaries = size(boundaries, 1);
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k};
plot(x, y, 'b-', 'LineWidth', 2);
caption = sprintf('%d Outlines, from bwboundaries()', numberOfBoundaries);
title(caption, 'FontSize', fontSize);
plotRows = ceil(sqrt(numRegions));
thisImage = imcrop(rgbImage, props(k).BoundingBox);
thisMask = props(k).Image;
thisMask(end+1, end+1) = false;
thisImage = bsxfun(@times, thisImage, cast(thisMask, 'like', rgbImage));
subplot(plotRows, plotRows, k);
function [BW,maskedRGBImage] = createMask(RGB)
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;