How to extract color parameters of each rice grain and then how should I compare them?
4 views (last 30 days)
Show older comments
I have an image named 50, I have segmented the iamge and obtained label of each object (rice grain). This image contains rice of different varieties and I want to separate them based on their colour properties because they are similar in shape and size. Can anyone explain me which parmateters will help me to distinguish them.
2 Comments
Accepted Answer
DGM
on 10 Feb 2022
Edited: DGM
on 10 Feb 2022
Consider what information is available. The exposure doesn't seem too bad. There are some grains which seem maybe a bit more colorful than others, but it's hard to tell what's meaningful.
First, strip out the background and adjust the levels so that the grain color is more pronounced.
A = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/890025/50.jpg');
A = rot90(A); % i'm just rotating this so it fits better in the web-view
A = imadjust(A,[0.7 1]);
mask = bwareaopen(rgb2gray(A)>128,1000);
A(~mask(:,:,[1 1 1])) = 0;
imshow(A)
Is that enough information to identify which are which? I'm not sure.
Select only the pixels representing the rice grains. Out of this entire population of pixels, is there any salient group information?
mask = imerode(mask,strel('disk',5)); % erode the mask to ignore edge shading
Am = reshape(A(mask(:,:,[1 1 1])),[],1,3);
Amlab = rgb2lab(Am);
for c = 1:3
subplot(3,1,c)
histogram(Amlab(:,:,c))
end
Maybe? It's hard to tell. Of course, that's the entire population of pixels. What if we consider the average color of each grain?
Alab = rgb2lab(A);
[L N] = bwlabel(mask);
graincolor = zeros(N,3);
for b = 1:N
grainmk = L==b;
Agrain = Alab(grainmk(:,:,[1 1 1]));
graincolor(b,:) = mean(reshape(Agrain,[],3),1);
end
graincolor % average color [L a b] for each grain
figure
for c = 1:3
subplot(3,1,c)
histogram(graincolor(:,c))
end
Now it looks even less like there's a discernable pattern. What if we just look at where those color points are and try to find a pattern by eye?
figure
plot3(graincolor(:,2),graincolor(:,3),graincolor(:,1),'b.')
grid on
Well, I'm not sure if that really helps either. It's hard to say if I'm getting anywhere since I'm not sure which are which to begin with. Let's just say that there's a group defined by L>90. Let's just select that.
figure
grainmask = graincolor(:,1)>90;
S = regionprops(mask,'centroid');
C = vertcat(S.Centroid);
imshow(A); hold on
for b = 1:N
if grainmask(b)
plot(C(b,1),C(b,2),'bo','markersize',25);
else
plot(C(b,1),C(b,2),'ro','markersize',25);
end
end
Are those the right ones?
20 Comments
Image Analyst
on 18 Feb 2022
Hyper spectral could help. Post those images. Take a picture with all one type of rice on the left and all the other type on the right.
I've been designing imaging systems for about 40 years so I know a little bit about it. 🙂 Other than that (using your hyperspectral images) you should have a light booth that excludes ambient light. You should also check the illumination level with a lux meter to make sure it's the same each time. Aim for somewhere between 1000 and 6000 lux. Take a "blank shot" of just the background so you know how the exposure varies with location in the image due to lens shading and light non-uniformities. Then divide your test images by that background image. See attached demo.
But the big problem is your sample presentation. Your rice you said is all mixed together and you don't know which is which. So, using image analysis, let's say you figure out which type each grain of rice is. Now you need to collect those into bags or buckets (each of one type) to separate them. I guess you'd have to do that by hand, which is subject to error. Plus it would be slow and tedious - definitely not a high throughput method where you could sort through hundreds of kg per day. So maybe you could have the rice on a moving belt and analyze just one at a time. Then, depending on what type of rice it is, a blower tube blasts each grain of rice to the right or left (off the belt) into the proper bucket. Then you'd need to have a bucket fill indicator so you can either automatically switch out the bucket or shutdown the system until the user can put in a new bucket.
More Answers (2)
Benjamin Thompson
on 10 Feb 2022
You will have to decide how to separate them based on your intended purpose. See the documentation on the regionprops function in the Image Processing Toolbox for a variety of shape measurements that can be calculated. Your image also is grayscale or very nearly so, which means your ability to separate by color will be difficult. Try running the colorThresholder tool on it to create some masks.
0 Comments
Image Analyst
on 10 Feb 2022
How many colors are you seeing there? Because most of us see one : white. In fact it looks like some of the grains might be saturated (overexposed) so they are clipped at 255. If there are two types of rice there you might try hyperspectral imaging. Maybe as some narrow band of light there will be some brightness differentiation.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!