Skip to content

Lab 4: Ball detection

Maya Cakmak edited this page Apr 14, 2020 · 2 revisions

In this lab you will write an algorithm to detect a ball in images captured by Cozmo.


Step 1: As a first step get the latest labs repository from GitHub to obtain the starter code for Labs 4-6. For Lab 4 you are provided the following directory of files:

  • find_ball.py – this is the main file where you will enter your solution. Complete the function find_ball() to return the location and radius of the ball in the image (if any). You may edit any part of this file and add helper functions as needed, but make sure your code is self-contained because this is the only file you will be submitting.
  • autograder.py – this autograder is provided to help you verify your solution.
  • imgs/ - directory containing images and a text file listing the true location of the ball in each image. Images are obtained from Cozmo's built in camera within a white arena built for Cozmo and contain a small black ball.

The code converts images into OpenCV format to allow for easy manipulation and editing. Verify that when you run autograder.py the output for all images is None, with the final line reading: score = 10. Note that you might need to install some new Python libraries.


Step 2: Complete the code to detect the ball location. We recommend using the HoughCircles() function in OpenCV. Your function should return [x, y, r] where x and y are the coordinates of the center of the ball, and r is the radius of the ball (all measured in pixels). If no ball is found, return [0,0,0] or None.

We will not be testing your code for grading but we recommend spending time improving performance on previously captured images before moving onto Lab 6 where you will integrate the ball detector into the robot's behavior. In preparation for Lab 6, find a suitable environment for your Cozmo (e.g. uniform background) and a ball (different color or size should work fine). You can optionally create a test/ directory with images directly captured from your Cozmo in this environment and update the autograder to verify your algorithm before moving onto Lab 6.

Clone this wiki locally