Introduction: Detecting Circles With OpenCV and Python

Inspiration :-

The Idea for this came when I was tinkering with OpenCV and it's various functions. Then a friend asked to help him develop an Algorithm which can detect a circle from a FPV Camera fitted to a RC Plane and adjust the alignment of the Plane correspondingly.

Having taken an interest in OpenCV, I agreed to help him. The idea was to detect the Circle and it's center so that a Payload could be dropped inside it.

However, using OpenCV autonomously in Real Time is a bad idea. Using it to aid the Human Observer on the other hand is not.

This code that I'm attaching however is a more general purpose one. This will detect circles in Real Time and in common scenarios. I changed the code to work in a more general scenario, as making it work for a FPV System required a lot of calibration on my part( As a result it worked well in the FPV scenario, but not quite in the Common general World). Hence, there was a need to re-adjust some parameters to make it capable of detecting circles in a general day-to-day scenario.

Step 1: Setting Up OpenCV and Python ( Windows )

Installing OpenCV from prebuilt binaries :


Below Python packages are to be downloaded and installed to their default locations :

1a. Python-2.7.x. ( https://python.org/ftp/python/2.7.5/python-2.7.5.m... )

1b. Numpy ( http://sourceforge.net/projects/numpy/files/NumPy/... )

1c. Matplotlib ( https://downloads.sourceforge.net/project/matplotl... )

(Matplotlib is optional, but recommended since you will need it at some point in time).

2. Install all packages into their default locations.

3. Python will be installed to C:/Python27/.

4. After installation, open Python IDLE.

5. Enter import numpy and make sure Numpy is working fine.

6. Download latest OpenCV release from sourceforge site and double-click to extract it.

7. Go to opencv/build/python/2.7 folder.

8. Copy cv2.pyd to C:/Python27/lib/site-packages.

9. Open Python IDLE and type following codes in Python terminal.

>>> import cv2
>>> print cv2.__version__

If the results are printed out without any errors, congratulations !!! You have installed OpenCV-Python successfully.

Building OpenCV from Source :

To built OpenCV from Source, check out the following tutorial :-

http://docs.opencv.org/master/d5/de5/tutorial_py_s...

Step 2: Setting Up OpenCV and Python ( Linux )

OpenCV-Python can be installed in Fedora in two ways,

1) Install from pre-built binaries available in fedora repositories,

2) Compile from the source

Another important thing is the additional libraries required. OpenCV-Python requires only Numpy (in addition to other dependencies). One can use Matplotlib for some easy and nice plotting purposes (which I feel much better compared to OpenCV). Matplotlib is optional, but highly recommended.

Similarly other things like IPython, an Interactive Python Terminal, is also highly recommended. We are not using this here though.

Installing OpenCV-Python from Pre-built Binaries :
Install all packages with following command in terminal as root.

1 $ yum install numpy opencv*

Open Python IDLE (or IPython) and type following codes in Python terminal.

1 >>> import cv2

2 >>> print cv2.__version__

If the results are printed out without any errors, congratulations !!! You have installed OpenCV-Python successfully.

It is quite easy. But there is a problem with this. Yum repositories may not contain the latest version of OpenCV always. For example, at the time of writing this tutorial, yum repository contains 2.4.5 while latest OpenCV version is 2.4.6. With respect to Python API, latest version will always contain much better support. Also, there may be chance of problems with camera support, video playback etc depending upon the drivers, ffmpeg, gstreamer packages present etc.

So my personnel preference is next method, i.e. compiling from source. Also at some point of time, if you want to contribute to OpenCV, you will need this.

Installing OpenCV from source :

For a detailed tutorial on how to build OpenCV from source, check this tutorial link :-

http://docs.opencv.org/master/dd/dd5/tutorial_py_s...

Step 3: Add Python to the Set of Environmental Variables ( Windows )

Before you can start using Python in Windows, you need to add the python interpreter to the set of Environmental Variables :

1. In Windows 8/8.1, go to the right corner of your screen and click on Settings.

2. Then select PC Info.

3. In Windows 10, go to File Explorer. Right click on This PC, and select Properties. ( This also works for Windows 7/8/8.1)

4. Click on Advanced System Settings.

5. Next, click on Environment Variables.

6. In System Variables, find `Path` variable. Select it and click on Edit.

7. In the end of the list add C:\Python27 and C:\Python27\Scripts ( Adding Python27\Scripts is not mandatory though ).

8. Click OK.

Congratulations, now you can use Python from cmd and Windows Powershell as well.

Step 4: Download the Python Script

Now that you've set up OpenCV and Python, we are ready to do some Image Processing.

1. I'm attaching the code file. It's called FinalWebcamTest.py.

2. Download the file to the desired location.

3. The codes are pretty well documented so that you'll be able to understand a bit. If you do not know a lot about Image Processing, leave everything as it is.

4. Refer to Step 6 if you want to make some changes to the Python code.

Step 5: Running the Python Script

Now that we are all set up , we can finally run the Python Script for Circle Detection.

1. Open up cmd or Powershell ( Windows) or Terminal ( Linux )

2. Go to the directory where you downloaded the file by using `cd` commands.

3. Type :

python FinalWebcamTest.py

Congratulations!! You now can detect circles in Real Time.

Step 6: Modifying the Code to Suit Your Needs

The Code applies some filters to the Image like Median and Gaussian to reduce noise.

I also applied Guassian Adaptive Thresholding to focus more on the circle and aid the Canny Edge Detection Process.

With a little tinkering around with the Code, and by changing some of
the Parameters in the cv2.HoughCircles function, you can adjust the Code for different settings. For more information check out the official Documentation :-

cv2.HoughCircles Documentation ( http://docs.opencv.org/2.4/modules/imgproc/doc/fea... )

However, try not to change the Parameters of the other Filters unless you know what you are doing, as this might lead to unexpected results.

I'll be making timely modifications and improvements to the Code.

It is available on :

https://github.com/ShubhamCpp/Circle-Detection-in-...

Feel free to commit changes and suggest improvements. Fork it if you feel the need to change something, much appreciated.

Don't feel afraid to try out different things with the code.

The World is your Playground and the Sky is the Limit.

Enjoy.

Step 7: Contact Me

If you have any queries or you ran into some problems or you have some suggestions, feel free to contact me.
My Name is Shubham Chopra and my email id is :-

shubham.chopra2906@gmail.com

Fork me on GitHub :- My Profile is https://github.com/ShubhamCpp

Have a Nice Day !!!!!!!