# OpenCV Circle Detection

**URL:** https://forums.openmv.io/t/opencv-circle-detection/691
**Category:** OpenMV Boards
**Created:** [May 11, 2018, 11:15pm UTC](https://forums.openmv.io/t/opencv-circle-detection/691 "2018-05-11T23:15:00Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![lab](https://avatars.discourse-cdn.com/v4/letter/l/ba8739/32.png) [@lab](https://forums.openmv.io/u/lab)
#### Post date: [May 11, 2018, 11:15pm UTC](https://forums.openmv.io/t/opencv-circle-detection/691/1 "2018-05-11T23:15:00Z")

</div>

I am interested in implementing the OpenCV’s circle finding algorithm, i.e:

cv2.HoughCircles(img,cv2.HOUGH\_GRADIENT,dp,minDist,param1=,param2=,minRadius=,maxRadius=)

to do circle detection. In my application, there are multiple circle sizes, so it is important to set the minimum and maximum radii in order to restrict the range of sizes to be able to efficiently find circles. I played around with find\_circles included in OpenMV, but it will find circles of different sizes.

Can you please help me import cv2’s HoughCircles source code into OpenMV? I followed your instructions to set up the OpenMV Firmware Development on my Linux virtual machine, but I’m not sure how to implement the source code in OpenMV. I searched on GitHub and this seems to be the source code that I want to use: opencv/modules/imgproc/src/hough.cpp

---

<div class="post-metadata">

### Author: ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)
#### Post date: [May 12, 2018, 12:01am UTC](https://forums.openmv.io/t/opencv-circle-detection/691/2 "2018-05-12T00:01:55Z")

</div>

Hi, the circle detection code on OpenMV already is based on the OpenCV one. The OpenCV method will not fit on the OpenMV Cam due to memory constraints.

Anyway, it sound like you want to improve the speed for the find circles algorithm on the OpenMV Cam. This can be done by adding some more parameters to our current method to look for particular radiuses.

Please see this code here:

[https://github.com/openmv/openmv/blob/master/src/omv/img/hough.c#L678](https://github.com/openmv/openmv/blob/master/src/omv/img/hough.c#L678)

Just made the min and Max value of R a parameter and you’ll have it run much faster like the OpenCV one.

---

<div class="post-metadata">

### Author: ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)
#### Post date: [May 12, 2018, 12:02am UTC](https://forums.openmv.io/t/opencv-circle-detection/691/3 "2018-05-12T00:02:19Z")

</div>

I will add improving this to my list of things to do.

---

<div class="post-metadata">

### Author: ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)
#### Post date: [May 12, 2018, 12:02am UTC](https://forums.openmv.io/t/opencv-circle-detection/691/4 "2018-05-12T00:02:50Z")

</div>

To help us, generating a high quality PR which adds this feature would be appreciated.

---

<div class="post-metadata">

### Author: ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)
#### Post date: [May 13, 2018, 11:34pm UTC](https://forums.openmv.io/t/opencv-circle-detection/691/5 "2018-05-13T23:34:45Z")

</div>

Okay, your request is implemented. Please add “r\_min”, “r\_max”, and “r\_step” as keyword arguments to find\_circles to speed up the algorithm. I easily got a speed up to 17 FPS by reducing the radius.

Attached is the firmware. Use the tools-\>run bootloader option to update the firmware. Either the firmware.bin or the openmv.dfu file will work. Try the firmware.bin file first.  
[firmware.zip](https://forums.openmv.io/uploads/short-url/aI93BnfOolf20I2zK6Pq0sHzNly.zip) (2.16 MB)

---

<div class="post-metadata">

### Author: ![lab](https://avatars.discourse-cdn.com/v4/letter/l/ba8739/32.png) [@lab](https://forums.openmv.io/u/lab)
#### Post date: [May 14, 2018, 4:53pm UTC](https://forums.openmv.io/t/opencv-circle-detection/691/6 "2018-05-14T16:53:12Z")

</div>

Thanks! I tested it and the ability to distinguish between different circle sizes is much improved.
