# What is the optimal way to select by color?

I need to select all pixels in a image that are similar in color to a given one.

My current approach is to get the hsv values for this pixel and calculate the distance to each pixel in the image being

distance = abs(ref.h-pix.h) + abs(ref.s-pix.s) + abs(ref.v-pix.v);


And select the pixels bellow certain distance.

Is there any other preferred method?

• "Optimal" obviously demands you have a metric for goodness. So you probably want to define a measure for local and global "similarity" and use that. Mar 6, 2017 at 19:12
• If you are interested in "colour" then you could apply the distance criterion to the RGB model directly. With the HSV, you can simply look at the H component to "guess" the colour and the S,V to judge if it is in the shadow or light areas of the image.
– A_A
Mar 7, 2017 at 8:37

The Wikipedia page Color difference gives several formulas, the most advanced being CIEDE2000. They are all based on Euclidean distance $\Delta E$ (straight line distance) in the approximately perceptually uniform CIE Lab color space, with various corrections.