StatCounter

Friday, July 25, 2014

OpenCV Shapes Detection


In this blog post, I will show how I used Python on OpenCV to recognize shapes when displayed on a webcam.  I chose Python because it is easy to understand, but C/C++ is slightly harder and executes quicker. My code (at bottom) is pretty accurate (~85%) at detecting shapes, but it has problems recognizing some blue hues.

    You will need to install OpenCV and numpy python modules before this. There are guides on the internet available for this topic. In addition, you need Python 2, not 3 to run this. You access this by going to terminal and typing 'python' or 'python2'. Also the newest OpenCV distro doesn't work with Python- 2.7.8, so use 2.7.3 instead.

The general steps are:

1. Set up your webcam
2. Read a frame of the picture
3. Erode and Dilate the picture to remove unnecessary noise
4. Set a color range to only get shapes of a specific color (in this case I chose blue)
5. Check your picture if pixels are in that color range
6. Get the Contours (basically lines that join areas with similar colors)
7. Iterate through them and get only the biggest one
8. Approximate the biggest contours' number of sides (cv2.approxPolyDP) to find how much sides
9. Correspond this number of sides to shapes' names (quad, triangle, etc.)


The code is available here on github if you want to try it out. This code I wrote only accepts BLUE shapes, so keep that in mind while printing test-cards.

Here's a video of me trying out different shapes:


Here is me testing each shape:

Square/Quadrilateral:


Triangle:


Pentagon:

7-sided figure (Arrow):




No comments:

Post a Comment