How to Refine Color Identification Code?

1 view (last 30 days)
Brendan Burke
Brendan Burke on 30 Nov 2015
Commented: Walter Roberson on 30 Nov 2015
For this project, I am trying to write code that can identify different colored skittles in a jpeg image and then report a count of (the number of) each different color skittle. So far, I have code that works decently using a binary method, but there are still issues; the count is often off somewhat. Also, before processing, the code first reduces the info in the image by pixelating it. This helps, but I have to modify how I resize the image for any given picture, which isn't ideal. The code should be able to work on any provided skittles image without being modified. I am not allowed to use loops of any kind. I have included my code. What are some ways I can refine it to work more precisely on any provided image?
% clear;clc;close all
Published=datestr(now, 21)
figure(1)
m = imread('skittles1.jpg');
imshow(m)
[X,map]= rgb2ind(m,5);
figure(2)
imshow(X,map);
RGB = ind2rgb(X,map);
figure
imshow(RGB)
BW = im2bw(RGB);
r=m(:,:,1);
g=m(:,:,2);
b=m(:,:,3);
y = [r > 185 & r < 260 & g > 190 & g < 255 & b > -1 & b < 100];
yt=sum(y(:));
o = [r > 165 & r < 260 & g > 15 & g < 90 & b > -1 & b < 30];
ot=sum(o(:));
gr = [r > 20 & r < 95 & g > 150 & g < 230 & b > -1 & b < 70];
grt=sum(gr(:));
re = [r > 100 & r < 230 & g > 3 & g < 35 & b > 5 & b < 55];
ret=sum(re(:));
p = [r > 5 & r < 117 & g > 0 & g < 130 & b > -1 & b < 150];
pt=sum(p(:));
loc = find(re==0);
loc1 = find(re==1);
r(loc1)=0;
g(loc1)=0;
b(loc1)=0;
r(loc)=255;
g(loc)=255;
b(loc)=255;
new=cat(3,r,g,b);
figure
BW = im2bw(new);
ComplementImage=imcomplement(BW);
HolesClearedImage = imfill(ComplementImage,'holes');
cc = bwconncomp(HolesClearedImage,18);
number = cc.NumObjects;
[labeledImage, numberOfObject] = bwlabel(HolesClearedImage);
measurements = regionprops(HolesClearedImage, 'Centroid', 'Area');
numberOfCircles = length(measurements);
imshow(HolesClearedImage)
end

Answers (2)

John D'Errico
John D'Errico on 30 Nov 2015
Well, I suppose you could use my fuzzycolor tool. It identifies all pixels in an image that lie inside given color name boundaries. The approach there was to use a 3-d lookup table.
Of course, for a student project, this is probably not an option.

Image Analyst
Image Analyst on 30 Nov 2015
I understand why you want to improve it. It's not a very robust method at all. First of all, don't do processing in RGB color space - it's too dependent on the overall light level and exposure. A red Skittle might be red with one light level but classified as brown for a lower light level. It's better to use rgb2hsv and have predefined ranges for the different colors.
And I'm not sure how robust your segmentation algorithm is. How robust does it need to be? Are the pieces always on a uniform background? How about a cluttered background, maybe with stuff in there the same color but not round? Does it have to extract only round pieces and reject square or arbitrarily-shaped pieces? Does it need to be in a certain size range? What if you have red circles that are 20, 100, and 400 pixels in diameter? Do you want to extract only those in a certain size range? (Like my Image Segmentation Tutorial shows: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862)
Why don't you attach a "bad" picture and a "good" one?

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!