MATLAB: Check if two colors are similar

colorcompareimageimage processingrgb

How can I compare two RGB colors to see if they look the same (to human eyes). And I'd like to be able to control the amount of similarity.
Say for example 15. What is the best way to compare the colors to have more colors in the range.
I have two idea in my mind but I don't know if they are the best options:
% (colorN(1) to colorN(3) are the RGB value of the color)
% Method 1:
if abs(double(color1(1)) - double(color2(1))) < 15 ...
&& abs(double(color1(2)) - double(color2(2))) < 15 ...
&& abs(double(color1(3)) - double(color2(3))) < 15
% The colors are similar

end
% Method 2
if sum(abs(double(color1(1)) - double(color2(1))), ...
abs(double(color1(2)) - double(color2(2))), ...
abs(double(color1(3)) - double(color2(3)))) < 15
% The colors are similar
end
% Maybe method 3 which is a combination of the two.
I've tried these a little bit by myself but I wasn't happy with the results. Anyone knows a better way to compare? Does Matlab have a function for that?

Best Answer

It depends on the definition of similarity. The comparison is not trivial: There are "a lot of similar" greens, but only a small set of what is received as white. Neither the Euclidean distance nor the maximum distance between the channel of the RGB values will solve this reliably. I assume that this works in HSV or LAB color more intuitive:
hsv1 = rgb2hsv(color1);
hsv2 = rgb2hsv(color2);
DeltaE = sqrt(sum((lab1 - lab2) .^ 2)) % [EDITED, parentheses added]
describes the distance, but I find limits of 1.0 and 2.3 for a "JND" (just noticeable difference).
Let's wait what Image Analyst post as answer. ;-)