Table of Contents
Question 1c: Rice Grain Analysis ........................................................................................................... 1
Question 1c: Rice Grain Analysis
clc; clear; close all;
% 1. Load and Preprocess
I = imread('[Link]');
background = imopen(I, strel('disk', 15));
I2 = I - background; % Remove uneven background
I3 = imadjust(I2); % Increase contrast
% 2. Binarize
bw = imbinarize(I3);
bw = bwareaopen(bw, 50); % Remove noise
% 3. Find Objects
cc = bwconncomp(bw, 4);
labeled = labelmatrix(cc);
RGB_label = label2rgb(labeled, 'spring', 'c', 'shuffle');
figure; imshow(RGB_label); title('Detected Rice Grains');
% 4. Calculate Statistics
graindata = regionprops(cc, 'basic');
grain_areas = [[Link]];
histogram(grain_areas); title('Histogram of Rice Grain Area');
disp(['Total Rice Grains Found: ' num2str([Link])]);
Total Rice Grains Found: 95
1
Published with MATLAB® R2025b