DiddyBorg redfollowball example on PiZero

I tried my hardest to get the PiZero+zeroborg version of the redfollowball example to work but it seems that it just never detects the ball. I tried a red ball first, then figured out what my HSV values would be for a pink ball, but in both cases it says there's no ball or it's too far away. The code as it is, modified for ZeroBorg, doesn't work with openCV 3.1, specifically in this area:

def ProcessImage(self, image):
        # Get the red section of the image
        image = cv2.medianBlur(image, 5)
        image = cv2.cvtColor(image, cv2.COLOR_RGB2HSV) # Swaps the red and blue channels!
        red = cv2.inRange(image, numpy.array((115, 127, 64)), numpy.array((125, 255, 255)))
        # Find the contours
        contours,hierarchy = cv2.findContours(red, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

It gives an error about too many variables, for cv2.findCountours
I changed it to read _, countours, hierarchy, = cv2.findContours (and various variations of that first variable) so that the program at least runs without an error. However, it just doesn't seem to be detecting anything.

Any suggestions on what might have changed in openCV 3.1 that I could apply here?

piborg's picture

As far as I am aware the change to the findContours line as you describe is correct:

 _, countours, hierarchy = cv2.findContours(red, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

It is a little irritating that the returned value list has changed, but the function should be the same otherwise.

The most likely explanation is that the colour range we are using is wrong for the ball you have. This is a bit fiddly to change because of two quirks:

  1. In OpenCV the hue values seem to be from 0 to 180 instead of 0 to 360
  2. As per the comment on the cvtColor line we are swapping the red and blue channel

The best way to figure out the correct colours will be to start from an actual camera image. You can use the raspistill command to take an image like this:

raspistill -w 320 -h 240 -o ~/photo.jpg

This will create an image called photo.jpg in the /home/pi directory.

You can swap the colour channels in the image using Gimp. First download the swap-red-and-blue.txt file attached below. Open the image and on the menu go to:
Colors > Components > Channel Mixer
Press the Open button and select the downloaded file (the colours should change in the preview). Now press Ok.

To get the hue value in Gimp after swapping use the Color Picker tool (looks like a dropper) and select a part of the ball. Now click on the colour in the toolbox to see the HSV values. The H value will need to be halved for the script, we would try a +/- 5 range around the half value. For example if the H value in Gimp is 78 then the hue range for OpenCV would probably be:
(78 ÷ 2) - 5 = 34 to (78 ÷ 2) + 5 = 44.

I would try this first with the same saturation and value ranges and see if it works. For the example above this would be:

red = cv2.inRange(image, numpy.array((34, 127, 64)), numpy.array((44, 255, 255)))

which happens to be a green ball I have in the office :)

Attachments: 

Thanks a bunch, finding the H value in GIMP made the difference. It turns out that the program I was using to figure out the HSV values didn't swap the red and blue so naturally it wasn't going to give me the correct values for the program. Now to up test the limits of the framerate on the Zero to see what responsiveness I can get.

Thanks for replying, I'll give that method a shot. I was using someone else's Python program for figuring out the HSV values of various color balls using sliders for the lower and upper values (I tried red, pink, and yellow). However, I'm terribly new at openCV and was hoping it would "just work".

I am using the yetiborg ball following code. I do get feedback but it looks like the readings are below the expected values. E.g. Drive Left 0.29 and Drive Right 0.27 at 60 cm and 0.70 and 0.71 at 1.5m. The motors are not activated. Using Zeroborg and ZeroCam. Any suggestions?

piborg's picture

This is a bug in yetiFollowBall.py.

See the reply to this post: Ball following motors not activated

Subscribe to Comments for "DiddyBorg redfollowball example on PiZero"