Calculate the size of the smallest rice grain in the image.
13 views (last 30 days)
Show older comments
Zeeshan Ahmed
on 31 Dec 2017
Commented: Walter Roberson
on 17 Mar 2021
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.
3 Comments
Image Analyst
on 16 Mar 2021
We do not have photos for you but you can easily create your own.
- Get a bag or box of rice.
- Spread some grains out on a uniformly colored surface. Preferably something not the same color as the rice. Black velvet will work very nicely.
- Take your camera (webcam, DSLR, or smartphone) and snap some photos.
Walter Roberson
on 17 Mar 2021
Black velvet might not be suitable for black rice or wild rice ;-) https://en.wikipedia.org/wiki/Black_rice
Accepted Answer
Image Analyst
on 1 Jan 2018
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.
0 Comments
More Answers (2)
Akira Agata
on 2 Jan 2018
Here is another try:
% Read the image
I = imread('ricesam2.jpg');
Igray = rgb2gray(I);
% Extract target regions
BW = edge(Igray);
se90 = strel('line', 2, 90);
se0 = strel('line', 2, 0);
BW2 = imdilate(BW,[se90 se0]);
BWfill = imfill(BW2,'holes');
seD = strel('diamond',3);
BWfinal = imerode(BWfill,seD);
% Measure the statistics
stats = regionprops(BWfinal,{'Area','Centroid'});
stats = struct2table(stats);
% Show the result
figure
imshow(I)
hold on
for kk = 1:height(stats)
text(stats.Centroid(kk,1)+10, stats.Centroid(kk,2),...
num2str(stats.Area(kk)))
end
0 Comments
Walter Roberson
on 1 Jan 2018
regionprops() of MajorAxisLength to get the "size" of the items.
bwareafilt() to do the filtering.
0 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!