OpenCV  4.5.3-dev Open Source Computer Vision
Hough Circle Transform

## Goal

In this chapter,

• We will learn to use Hough Transform to find circles in an image.
• We will see these functions: cv.HoughCircles()

## Theory

A circle is represented mathematically as $$(x-x_{center})^2 + (y - y_{center})^2 = r^2$$ where $$(x_{center},y_{center})$$ is the center of the circle, and $$r$$ is the radius of the circle. From equation, we can see we have 3 parameters, so we need a 3D accumulator for hough transform, which would be highly ineffective. So OpenCV uses more trickier method, Hough Gradient Method which uses the gradient information of edges.

The function we use here is cv.HoughCircles(). It has plenty of arguments which are well explained in the documentation. So we directly go to the code.

import numpy as np
import cv2 as cv
img = cv.medianBlur(img,5)
cimg = cv.cvtColor(img,cv.COLOR_GRAY2BGR)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
# draw the outer circle
cv.circle(cimg,(i,i),i,(0,255,0),2)
# draw the center of the circle
cv.circle(cimg,(i,i),2,(0,0,255),3)
cv.imshow('detected circles',cimg)

Result is shown below: image
{"ft_lang_label":"__label__en","ft_lang_prob":0.8593229,"math_prob":0.99771184,"size":667,"snap":"2021-31-2021-39","text_gpt3_token_len":172,"char_repetition_ratio":0.116138764,"word_repetition_ratio":0.0,"special_character_ratio":0.25937033,"punctuation_ratio":0.12686567,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99964213,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-07-30T04:24:38Z\",\"WARC-Record-ID\":\"<urn:uuid:96374d3b-292c-484d-b3e2-b32185e2d3d3>\",\"Content-Length\":\"9426\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:34bffea4-93b8-4b1f-9463-ea388975d07a>\",\"WARC-Concurrent-To\":\"<urn:uuid:e54ad736-d178-47d1-a4c9-1c8e7abd2756>\",\"WARC-IP-Address\":\"207.38.86.214\",\"WARC-Target-URI\":\"https://docs.opencv.org/master/da/d53/tutorial_py_houghcircles.html\",\"WARC-Payload-Digest\":\"sha1:PTVXGAAS53625GEJ7H3LU3RE2ZTTEZRB\",\"WARC-Block-Digest\":\"sha1:FKMEZSWNLGUGYX5VUDRODB6FKYG3WSWC\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-31/CC-MAIN-2021-31_segments_1627046153931.11_warc_CC-MAIN-20210730025356-20210730055356-00421.warc.gz\"}"}