1

I'm trying to convert the raw RGB values from a Colour Sensor into HSL but it doesn't seem to be working:

R = rgbn[INPUT_RED];
G = rgbn[INPUT_GREEN];
B = rgbn[INPUT_BLUE];

var_R = ( R/255 );
var_G = ( G/255 );
var_B = ( B/255 );

float H = 0;
float S = 0;
float V = 0;

float var_Max = var_R;
if (var_G > var_Max) var_Max = var_G; 
if (var_B > var_Max) var_Max = var_B;

float var_Min = var_R;
if (var_G > var_Min) var_Min = var_G;
if (var_B > var_Min) var_Min = var_B;

del_Max = var_Max - var_Min;

// Calculating H
if (del_Max == 0) {H=0;}
else if (var_Max = var_R) {H=(60*((var_G-var_B)/del_Max))%6;}
else if (var_Max = var_G) {H=(60*((var_B-var_R)/del_Max))+2;}
else if (var_Max = var_B) {H=(60*((var_R-var_G)/del_Max))+4;}

// Calculating S
if (var_Max == 0) {S=0;}
else if (var_Max != 0) {S=del_Max/var_Max;}

// Calculating V
V=var_Max;

NumOut(0,LCD_LINE6, H);
NumOut(0,LCD_LINE7, S);
NumOut(0,LCD_LINE8, V);

And once I've converted it to HSL how can I recognize the colour?

Here is my code that gets raw RGB data:

#define       COLORSENSOR SENSOR_3
#define       S_CLR     IN_3
#define       FLT_MAX
#define       FLT_MIN

unsigned int rgbn[4];

void color_sensor_on() {
  SetSensorColorFull(S_CLR);
  ResetSensor(S_CLR);
}


task main(){
  color_sensor_on();

  while(true)
  {
    ReadSensorColorRaw(S_CLR, rgbn);       //Raw data
    RGBToHSL();
  }
}
4
  • While I think Thaddeus Hughes has the answer it might help if you could also post some sample inputs and outputs (i.e. "for R255, G128, B0 I'm getting H0, S0, V0", etc.) - are you getting an HSV of 0? or some wildly different values Commented Mar 31, 2015 at 22:58
  • Also, note that HSV and HSL are not quite the same, and instead of calculating V (as var_Max) you should calculate L (as 0.5(var_Max + var_Min)) Commented Mar 31, 2015 at 23:05
  • 1
    I'm voting to close this question as off-topic because this belongs on codereview.stackexchange.com
    – Nick2253
    Commented Apr 1, 2015 at 17:39
  • 1
    @Nick2253 - I think this would be off topic at Code Review: They explicitly don't want questions about "Troubleshooting, debugging, or understanding code snippets." - they go on to require that code samples in questions "work as intended" (but need improvement). It's more on-topic at StackOverflow (I have some code and it doesn't work), but we should be able to answer this here ;) Commented Apr 1, 2015 at 20:46

1 Answer 1

2

Not sure what language you are using, or calls you are making before this, but:

float var_Min = var_R; if (var_G > var_Min) var_Min = var_G; if (var_B > var_Min) var_Min = var_B;

Should be (note the use of < rather than >)

float var_Min = var_R; if (var_G < var_Min) var_Min = var_G; if (var_B < var_Min) var_Min = var_B;

Your posted code is putting the maximum value into the minimum variable, messing up things down the line...

Another thing that will help is to not use the RawColorSensor call. Change your main task to this:

ColorSensorReadType csr;

task main(){
   csr.Port=S3;
   SetSensorColorFull(csr.Port);
   while(true){
       SysColorSensorRead(csr);
       RGBToHSL();
   }
}  

And the first few lines of your RGBToHSL call to this:

R = csr.NormalizedArray[0];
G = csr.NormalizedArray[1];
B = csr.NormalizedArray[2];

This will normalize the values between the values of 0,255 (or at least roughly). I've had more luck with this call since it also tries to calibrate the values.

After this, you'll need to find the color values you're using with a tool like http://www.colorpicker.com/ . For example, orange would have HSV (HSB on that site) of (40,100,100). (The scaling on these values may differ). So, to detect orange, check if: Hue>30 AND Hue<50 AND saturation>90 AND value>90. Make sure there is play with your thresholds, otherwise the check will work once in a hundred tries.

18
  • I am using NXC language. Thanks for searched bug in my code. I forgot to mention that the raw value is greater than 255, maybe I had to divided it with 1024, because I read somewhere that these values increase in 1024.
    – Timbelion
    Commented Apr 1, 2015 at 6:50
  • Should my variables to be float?
    – Timbelion
    Commented Apr 1, 2015 at 7:07
  • When I cross the red color my values are: H35 S0.04 L0.80. Whats wrong??
    – Timbelion
    Commented Apr 1, 2015 at 7:14
  • Hue looks good. Varianles should be float. Commented Apr 1, 2015 at 13:13
  • Verify that the coloe sensor is in the right spot (about 1.5 studs away) and nothing like direct sunlight is hitting it. Commented Apr 1, 2015 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.