Calculating distance in HSV color space

10 views (last 30 days)
Kent Yeap
Kent Yeap on 22 Mar 2017
Edited: DGM on 22 Oct 2022
Hi, I would like to know if there is a way to measure color distance in HSV color space, a method which works like the Delta E in LAB color space. For example, I want a measure for the similarity of colors, i.e., pink is more similar to red than blue, based on the closeness in the HSV color space.
Thank you.
Thank you.

Answers (1)

DGM
DGM on 22 Oct 2022
Edited: DGM on 22 Oct 2022
I think the simple answer is no ... but yes ... with a lingering quizzical look.
HSV is not a color space. It's simply a mapping of RGB -- usually we'd be talking about sRGB, but not necessarily. With that in mind, if you want to find distances between color points in the underlying color space, you're just finding euclidean distance in RGB. I think that's the most justifiable "yes" part.
A person might still be inclined to make some naive interpretation of HSV as a cylindrical volume and then try to find distances within that, but you'll end up with nonsense results. Consider the example:
% say we have two pairs of color points in HSV
% white versus red
% dark gray versus dark red
% each pair shares the same H and V
hsv = [0 0 1; 0 1 1; 0 0 0.1; 0 1 0.1];
% convert to RGB
rgb = hsv2rgb(hsv)
rgb = 4×3
1.0000 1.0000 1.0000 1.0000 0 0 0.1000 0.1000 0.1000 0.1000 0 0
% display the colors
imshow(permute(rgb,[1 3 2]))
% conversion from a naive cylindrical interpretation of HSV
% to rectangular coordinates
[x y z] = pol2cart(hsv(:,1)*2*pi,hsv(:,2),hsv(:,3));
xyz = [x y z];
% calculate distances between point pairs in HSV
Dhsv1 = norm(xyz(1,:)-xyz(2,:),2)
Dhsv1 = 1
Dhsv2 = norm(xyz(3,:)-xyz(4,:),2)
Dhsv2 = 1
Obviously, the apparent distance between those color pairs is not identical as the first calculation suggests. It might be intuitive to think of HSV as a cylindrical space, but it's not really. It might be more appropriate to think of it as a projection of the cube into an odd hexagonal cone shape. You might get slightly less silly results if you scaled S by V to form a simple cone:
% conversion from a conical interpretation of HSV
% to rectangular coordinates
[x y z] = pol2cart(hsv(:,1)*2*pi,hsv(:,2).*hsv(:,3),hsv(:,3));
xyz = [x y z];
% calculate distances between point pairs in HSV
Dhsv1 = norm(xyz(1,:)-xyz(2,:),2)
Dhsv1 = 1
Dhsv2 = norm(xyz(3,:)-xyz(4,:),2)
Dhsv2 = 0.1000
... but again, you could also just find the distances in RGB or use DeltaE or something and be done with it
% calculate distances between same pairs in RGB
Drgb1 = norm(rgb(1,:)-rgb(2,:),2)
Drgb1 = 1.4142
Drgb2 = norm(rgb(3,:)-rgb(4,:),2)
Drgb2 = 0.1414
% calculate DeltaE instead
DE1 = deltaE(rgb(1,:),rgb(2,:))
DE1 = 114.5317
DE2 = deltaE(rgb(3,:),rgb(4,:))
DE2 = 11.5767
Things to note:
In a conical HSV:
  • the distance between any primary or secondary color and white is 1.
  • the distance between any primary or secondary color and black is sqrt(2).
  • the distance between an adjacent primary and secondary is 1.
  • the distance between white and black is 1 (i.e. black is closer to white than it is to blue)
  • the distance between any two complementary corners is 2 (i.e. twice the distance from black to white)
In RGB:
  • the distance between any primary and white is sqrt(2)
  • the distance between any secondary and white is 1
  • the distance between any primary and black is 1
  • the distance between any secondary and black is sqrt(2)
  • the distance between an adjacent primary and secondary is 1.
  • the distance between white and black is sqrt(3)
  • the distance between any two complementary corners is sqrt(3)
In LAB:
  • the aforementioned distances are not common across all primary/secondary colors, because why would they be?

Community Treasure Hunt

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

Start Hunting!