Solved

# find closest color in hsv space

Posted on 2011-10-05
680 Views
Last Modified: 2012-05-12
Given an array of hsv values and one color in hsv, how would I find the closest match in the array?
0
Question by:hbiz
8 Comments

LVL 37

Expert Comment

Depends on what you mean by "closest match." Just calculate the distance for each as
``````distance[i] = abs(array[i].h - color.h)
+ abs(array[i].s - color.s)
+ abs(array[i].v - color.v))
``````
and pick the lowest.
0

LVL 37

Expert Comment

If a difference in one is more impacting that in another you could use weights.
For example if a difference in v is twice as bad as a difference in s and s and h are the same, then you could do it with
``````distance[i] = abs(array[i].h - color.h)
+ abs(array[i].s - color.s)
+ 2*abs(array[i].v - color.v))
``````
Or if you want to scale it to 0-1 (and assuming the max value of each value is 255) use this
``````distance[i] = (.25*abs(array[i].h - color.h)
+ .25*abs(array[i].s - color.s)
+ .5*abs(array[i].v - color.v))/255
``````
0

LVL 6

Expert Comment

How about treating the three HSV values as X Y and Z in 3D space.  In this case we can say we have two HSV values H1S1V1 and H2S2V2.  The distance is calculated as sqrt((H2-H1)^2+(S2-S1)^2+(V2-V1)^2).
0

Author Comment

Unfortunately the vector distance (3d space) is not the same as perceptual color difference for the human eye. I was hoping for an algorithm to mimick how the eye would determine that the two colors are the same.

Thanks for all the responses so far
0

LVL 37

Expert Comment

To get an idea of the complexity of your problem see this article:
http://adsabs.harvard.edu/abs/2003SPIE.5227...51V

You could use the formula here and assume they know what they are talking about:
http://www.ee.columbia.edu/ln/dvmm/researchProjects/MultimediaIndexing/VisualSEEk/acmmm96/node8.html
0

LVL 37

Accepted Solution

Since that's hard to read, you could get the same result by using
``````distance[i] = pow(array[i].v - color.v, 2)
+ pow(array[i].s*cos(array[i].h) - color.s*cos(color.h), 2)
+ pow(array[i].s*sin(array[i].h) - color.s*sin(color.h), 2)
``````
and finding the smallest
0

LVL 37

Expert Comment

using pow(x,2) to mean x^2 or x*x
0

Author Comment

wow, thank you!  I'll give it a try and post back shortly
0

## Write Comment

Please enter a first name

Please enter a last name

We will never share this with anyone.

## Featured Post

Okay. So what exactly is the problem here? How often have we come across situations where we need to know if two strings are 'similar' but not necessarily the same? I have, plenty of times. Until recently, I thought any functionality like that wo…
One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp a…
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

#### 761 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!