First I tried color detection using 3 colored LEDs (red, green, blue) and light dependent resistor. Results were very bad, differences in measured color intensities were too small to reliably detect different colored beads. Even after calibration there were too many errors.
Then I decided to go overkill and replace the tri-color detector by webcam and computer vision software. Although it is now computer dependent (with color LEDs computer was not needed, everything was programmed in FITKit), results improved radically. This was my first computer-vision project, so it was useful to learn something new.
BACKcommands and rotates the upper disc so the bead falls through desired hole. Then it takes new bead from container and positions it under the camera.
For correct position detection, there is black tape mark on the rotating disc and LED-photoresistor detector (I used the former color detector, this is why there are 3 LEDs instead of 1).
Everything is under control of Java program, that captures image from webcam
and detects bead colors. It uses BoofCV computer vision library. Detection is
done by Canny edge detector - finds position of bead on the captured image. From
detected region pixels are thresholded by saturation and mean color hue is calculated.
Calculating mean of hue values is not trivial, because hue represents an angle, with red color on both 0° and 360°. (It took me a while to find out why red beads are detected as turquoise color - turquoise is in the middle of hue spectrum). To solve it, I calculated sine and cosine values of each pixel's hue (X and Y coordinates of corresponding points on unit circle) and then calculated the average of these points, finally converted it back to angle - this is the average hue. (For details, see wikipedia) For each bead, hue is then compared against preset value and bead is directed towards correct bucked. (The sorter only separates one color from all other colors).