MATLAB: Calculate the size of the smallest rice grain in the image.

Image Processing Toolboximage segmentationsize of rice grain

Hi,I want to calculate the size of the smallest rice grain in the image
so that I can use it in my function to get a decision that, if the size is less than I want, to treat it as noise and don't count it. Please guide me.

Best Answer

Try this:
% function testRGBImage()
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'ricesam2.jpg';
folder = fileparts(which(baseFileName)); % Determine where demo folder is (works with all versions).
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
%=======================================================================================
% Read in demo image.
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(rgbImage);
% Display the original image.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis on;
caption = sprintf('Original Color Image, %s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Get a mask of the blue channel
binaryImage = rgbImage(:,:,3) < 173;
% Get rid of junk near edge of image.
binaryImage = imclearborder(binaryImage);
% Display the mask image.

subplot(2, 2, 2);
imshow(binaryImage, []);
axis on;
caption = sprintf('Binary Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
% Filter out blobs smaller than 100 or bigger than 1000
binaryImage = bwareafilt(binaryImage, [100, 1000]);
% Display the mask image.
subplot(2, 2, 3);
imshow(binaryImage, []);
axis on;
caption = sprintf('Size Filtered Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
% Find the areas of what's left.
props = regionprops(binaryImage, 'Area');
allAreas = [props.Area]
% Display the distribution of areas.
subplot(2, 2, 4);
histogram(allAreas);
axis on;
grid on;
title('Histogram of Areas', 'FontSize', fontSize, 'Interpreter', 'None');
message = sprintf('The smallest area = %.1f pixels', min(allAreas));
uiwait(helpdlg(message));
You could do a better job by improving the contrast with better lighting or a contrasting color (e.g. black) background material, and reducing the background illumination non-uniformity by using a better lens or by dividing your image by the image of a blank white sheet.