From 2b7cd09ecae36426b7e2c3972700bbb82de30260 Mon Sep 17 00:00:00 2001
From: Peter Abeles
* Color conversion between RGB and HSV color spaces. HSV stands for Hue-Saturation-Value. "Hue" has a range of [0,2*PI]
* and "Saturation" has a range of [0,1], the two together represent the color. While "Value" has the same range as the
- * input pixels and represents how light/dark the color is.
+ * input pixels and represents how light/dark the color is. Original algorithm taken from [1] and modified slightly.
*
* NOTE: The hue is represented in radians instead of degrees, as is often done.
+ * NOTE: Hue will be set to NaN if it is undefined. It is undefined when chroma is zero, which happens when the input
+ * color is a pure gray (e.g. same value across all color bands).
+ *
+ * RGB to HSV:
+ *
+ * min = min(r,g,b)
+ * max = max(r,g,b)
+ * delta = max-min // this is the chroma
+ * value = max
+ *
+ * if( max != 0 )
+ * saturation = delta/max
+ * else
+ * saturation = 0;
+ * hue = NaN
+ *
+ * if( r == max )
+ * hue = (g-b)/delta
+ * else if( g == max )
+ * hue = 2 + (b-r)/delta
+ * else
+ * hue = 4 + (r-g)/delta
+ *
+ * hue *= 60.0*PI/180.0
+ * if( hue < 0 )
+ * hue += 2.0*PI
+ *
+ *
+ *
+ * [1] http://www.cs.rit.edu/~ncs/color/t_convert.html *
* * @author Peter Abeles @@ -53,7 +87,6 @@ This doesn't seem to improve the runtime noticeably and makes the code uglier. */ - public class ColorHsv { // 60 degrees in radians diff --git a/main/ip/test/boofcv/alg/color/TestColorHsv.java b/main/ip/test/boofcv/alg/color/TestColorHsv.java index 35eb85e765..037d3f2ac5 100644 --- a/main/ip/test/boofcv/alg/color/TestColorHsv.java +++ b/main/ip/test/boofcv/alg/color/TestColorHsv.java @@ -52,6 +52,10 @@ public void backAndForth_F64_and_F32() { check(0.25, 0.5, 0.75); check(0.8, 0.1, 0.75); check(151, 151, 151); + check(151, 120, 120); + check(120, 151, 120); + check(120, 120, 151); + for( int i = 0; i < 50; i++ ) { double r = rand.nextDouble();