MATLAB: Naming Colors in Picture

colorcolor differencecolor segmentationImage Processing ToolboxMATLABnaming colors

Hello there
i have several images of Smarties like the first one. I need to name the color of each Smartie. To find each one i used "imfindcircles" and used the avg color of each one to redraw a nicer picture of them without background or relexions. I also noticed that working in HSV color space is the best. How do i give each Smartie a color-name (only using img-pross. toolbox – no AI) like Smartie #1 is Blue, Smartie #2 is red and so on…
I allready tried to use RGB and compare the color of the Smartie to a reference color but its really hard to tell the difference between orange-brown, blue-violett and green and yellow. Is there a reliable way to compare HSV Color and name it?

Best Answer

What I would do is to call rgb2lab() and convert your RGB image into LAB color space. Then call impixelinfo and mouse around over some known colors to get their LAB value. Then make a list of those. Then I'd create a Delta E image by taking the Euclidean distance of your LAB image from each of your known reference colors. Something like (untested)
labImage = rgb2lab(rgbImage);
imshow(labImage);
impixelinfo;
[rows, columns, numberOfColorChannels] = size(rgbImage)
minDeltaE = 1000000 * ones(rows, columns) % Keeps track of the min (so far) color difference at each pixel.
classifiedImage = zeros(rows, columns); % Keeps track of what reference color number each pixel is.
for k = 1 : numReferenceColors
L = labImage(:, :, 1) - lReference(k); % lReference is defined in advance.
a = labImage(:, :, 2) - aReference(k); % aReference is defined in advance.
b = labImage(:, :, 3) - bReference(k); % bReference is defined in advance.
thisDeltaEImage = sqrt(L.^2 + a.^2 + b.^2);
mask = thisDeltaEImage < minDeltaE;
% Assign this color
minDeltaE(mask) = thisDeltaEImage(mask);
classifiedImage(mask) = k; % These pixels are this color.
end
Whichever reference color has a lowest Delta E is the one that Smartie is.
I'm attaching a Delta E demo for you to adapt.
For naming the colors, you need to see this page: