Close

RGB vs HSL vs HSI

A project log for Colour sorting machine for Lego

Automatically sort random Lego pieces by colour!

robgRobG 11/24/2025 at 15:260 Comments

I got a bit sidetracked.

The new vibe feeder is under way, but stalled waiting for parts to arrive from the east.

In the meantime I've added a window over the colour sensor and, as predicted, the sensing got messed up and the machine wouldn't sort properly any more. So that prompted a whole bunch of work looking at the RGB sensor again:

- a proper calibration routine to scale the sensor outputs so that they are better matched and less affected by changes in ambient lighting.

- a dive into alternative colour spaces, such as HSV, HSI and HSL which, at least to my eye, seem to make the sample data easier to classify: white, grey and black have low saturation and can be distinguished by their brightness, and the other colours can mostly be classified by ranges of hue. I was able very quickly to put together a decision-tree classifier in HSL-space which, on a sample set of 2700+ bricks, achieves about 90% accuracy. Most of the remaining errors come down to the big variations in lighting each brick receives while it tumbles past the sensor. If I could slow them down and stop them tumbling, that should improve.

- a bit of research into other classifiers, such as k-NN and SVM, which might do better, but I'm not sure I can justify the effort just yet.

Right now the machine works really well. I can leave it pretty much unattended and it...well...sorts bricks!

Discussions