#Robotics Better Colour Detection by Converting RGB to HSI Values

Accurately detecting colours without needing calibration has always been an issue with robots for both Robocup Junior Rescue Line and Rescue Maze competitions.
If the sensor can detect colours itself, it might have issues depending on the range of shades for each colour. For example: Lego EV3 colour sensors are known for being bad at detecting shades of green other than Lego Dark Green.
Using the RGB (Red, Green and Blue) values from a colour sensor does allow the developer to interpret the values and decide which colour that represents. However, using HSI (Hue, Saturation and Intensity) makes this exercise simple and accurate.
Historically, I have suggested decided colours by using normalised (self calibrating) values for Red, Green and Blue and checking if you are seeing more of one colour in relation to the other colours.
A better method is to convert the RGB values to HSI (Hue, Saturation, Intensity) mode. This makes the detection of colours very simple as long as the Saturation is not too low (grey) and the Intensity not at the low (black) or high (white) extremes, you can just use a range on the Hue value to decide what the colour is.
This method also separates the colour component from the Intensity and so is not affected by changing lighting conditions and does not need continuous calibration after an initial check of the readings.
You can also use the Intensity value for line following if desired or just use one of the single colour values, such as Red or Green. From experience Blue is not recommended for line following.
Methods to convert RGB to HSI can be found with a search on the internet. The following image and formula are based on the following article, please read it for more information:
The article above assumes the RGB values are normalised between 0 to 255, but the formulas works for 0 to 100 or even 0 to 1024 as well.
Mindstorms EV3: If using Lego Mindstorms EV3 with a PC, you can get to the RGB values for the colour sensor using the custom block in the EV3-G software or using EV3 Basic. At this stage, the EV3 Classroom software does not provide this sensor block for any platform. So, if you are using Apple devices (Mac or iPad) with EV3, you won’t have the option to use RGB or RGB to HSI conversion. [Edit] Microsoft MakeCode does allow access to the RGB values from the colour sensors for EV3 robots.
Spike Prime: If using Lego Spike Prime, be sure to enable “More Sensors” when using Word Blocks (Scratch style), so you can get the Raw values for Red, Green and Blue (for all platforms). These values are between 0 and 1024 but can be divided by 4 for a range between 0 and 255.
The steps below can be replicated in any language.
Using R, G, and B as the normalised Red, Green, and Blue values of a color.
The Intensity (with a range that matches the input values), I, is an average given by the equation:
- I = (R + G + B) / 3.
Calculate the value of the minimum value, M, among R, G, and B:
- M = R
- if G < M then M = G
- if B < M then M = B
Note: EV3 Basic has a Math.Min() function which can be used (and nested) to get the minimum value M, instead of using if statements.
Then Saturation value (between 0 and 1), S, of a color is given by the equation:
- if I > 0 then S = 1 – M / I else S = 0
To get the Saturation as a percentage value (between 0 and 100), just multiply by 100:
- if I > 0 then S = (1 – M / I) * 100 else S = 0
To convert a colour’s overall hue, H, to an angle or degrees measurement, use the following equations:
- H = cos-1[ (R – 0.5*G – 0.5*B) / √(R² + G² + B² – RG – RB – GB) ]
- if B > G then H = 360 – H
To make the above formula’s easier to add into code you can separate the numerator, N, and denominator, D, into separate variables and also use the unexpanded version of the denominator:
- D = √( (R – G)*(R – G) + (R – B)*(G – B) )
- N = R – 0.5*G – 0.5*B
- H = arcos( N / D )
- if B > G then H = 360 – H
Note: Where the inverse cosine output is in degrees. EV3 Basic uses Radians for its Trigonometric functions so you will need to use Math.GetDegrees() to convert to degrees. It would be worth breaking up the Hue formula in stages and checking that the (R² + G² + B² – RG – RB – GB) value is greater than 0 so you do not attempt to get the square root of a negative number. EV3 Basic has the Math.Max() function that could be used for this. If the denominator is zero, then set the Hue value to 0 rather than attempting to perform the division calculation and getting a divide by zero error or (NaN = Not a Number) result.
Once you have the Hue (H), Saturation (S) and Intensity (I) values you can round them to remove any decimal places and then use them to decide what the sensors are seeing.
Taking the Hue value (and Saturation is above about 10% and Intensity is between about 10% and 90% of the input range), it can be checked to see what colour range it falls into.
Red is around the 0/360 degrees mark, Green is around 120 degrees and Blue is around 240 degrees, as per the colour wheel above. use a range either side of the test readings you take to make sure you can easily detect shades.
Sorry, not going to give you the exact code for these calculations, but you should be able to use the information above to write the code yourself.
More Information
For more information on robotics and the EV3 Basic extensions to Microsoft Small Basic, check out the following links:
- #Robotics Identifying Silver with Lego Spike Prime
- David’s Robotics Portal
- Microsoft Small Basic
- EV3 Basic extensions
Hope you find this information useful.
David
29-Aug-2024: Added extra notes about how RGB values are not available for EV3 when using the EV3 Classroom software. There are alternative software options for PC, but not for Apple (Mac or iPad) platforms.
07-Oct-2025: Added note that Microsoft MakeCode does provide access to the RGB values for EV3 robots.
29-Jun-2026: Added links to article on enabling “More Sensors” for Spike Prime.
This article was originally posted on https://www.winthropdc.com/blog.


0 Comments on “#Robotics Better Colour Detection by Converting RGB to HSI Values”