Friday, March 6, 2020

Calibration - Seeing Red



So our Pi Wars robot needs to "see red". Forest Fighters are not going to need to pick up different colours as we are not doing the Eco-Disaster challenge autonomously.

However we do want to attempt the Minesweeper challenge which requires detection of red.

Detecting a colour is simplest when the colour space is viewed in HSV as the Hue for a particular colour remains in a tight range independent of the Saturation and Value (Brightness)


HSV_color_solid_cylinder.png: SharkDderivative work: SharkD  Talk - HSV_color_solid_cylinder.png, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=9801673

Normally red hue values appear across the beginning and end of the range which means typically you have to compare across two ranges, twice as much computation, slowing your frame rate and ultimately slowing the robot. In the above example with a cylinder we will assume that red starts at 340 degrees and finishes at 20 degrees creating two ranges 340 to 359 and 0 to 20.
However there is a very neat trick you can do to move the red range to where the blue is by turning the cylinder. So here is the trick....

Rather than telling Open CV that the camera frame is BGR we do an RGB to HSV conversion instead. This means the red hue value rather than being centred on zero is moved to the blue and centered around 170. Making finding red objects much neater.

Take an BGR image from the Pi Camera and in Python Open CV use:
     imgHSV = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)

Then look for your red object where the blue would be. Simples!!!

Colour detection needs good lighting  ( or at least constant lighting ) and at the event the lighting will be very different to how we have been testing, so we have a spare Pi with camera,a 3.5" touch screen and power bank attached to do some calibration testing.

Note the HSV range is displayed across the top of the screen and some on screen "buttons" allow us to change the values.

Pi Calibration - Ready to "See Red"

Some video of the calibration program in action.






No comments:

Post a Comment