clc;
close all;
clearvars;
workspace;
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
folder = [];
baseFileName = 'rice green background.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(1, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Original 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';
[riceMask, maskedRGBImage] = createMask(rgbImage);
riceMask = bwareaopen(riceMask, 500);
subplot(1, 2, 2);
imshow(riceMask, []);
axis('on', 'image');
caption = sprintf('Mask Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
props = regionprops(riceMask, 'BoundingBox', 'Area')
allAreas = sort([props.Area])
numRows = ceil(sqrt(length(props)));
hFig = figure;
hFig.WindowState = 'maximized'
for k = 1 : length(props)
thisBox = props(k).BoundingBox;
croppedImage = imcrop(rgbImage, thisBox);
subplot(numRows, numRows, k);
imshow(croppedImage);
caption = sprintf('Blob #%d area = %d', k, props(k).Area);
title(caption, 'FontSize', 8);
drawnow;
end
fprintf('Done running %s.m ...\n', mfilename);
function [BW,maskedRGBImage] = createMask(RGB)
I = rgb2hsv(RGB);
channel1Min = 0.852;
channel1Max = 0.220;
channel2Min = 0.431;
channel2Max = 1.000;
channel3Min = 0.533;
channel3Max = 1.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