current position：Home>Application of Hoff circle detection in Python opencv
Application of Hoff circle detection in Python opencv
20220130 00:48:47 【Dream eraser】
Little knowledge , Great challenge ！ This article is participating in 「 A programmer must have a little knowledge 」 Creative activities
This article has participated in 「 Digging force Star Program 」 , Win a creative gift bag , Challenge creation incentive fund .
Python OpenCV 365 Day study plan , Go into the field of image with the eraser . This blog is the third in this series 35 piece .
Basic knowledge
By searching relevant information , Learned a little superficial knowledge of Hoff circle detection , Its basic content is that any nonzero pixel on the image , Can be a point on a potential circle . By voting , Generate cumulative coordinate plane , Then set a cumulative weight , De positioning circle .
In Cartesian coordinates, the equation of a circle is $(xa)^2 + (y  a)^2=r^2$, among (a,b)
It's the center of the circle ,r
It's the radius , You can draw a picture to show .
And according to the picture , We can see the following results
$x=a+r \cos \theta$ $y=b+ r\sin \theta$ That is, the following expression ： $a=xr \cos \theta$ $b=yr \sin \theta$
The next few sentences are not easy to understand , But there are a few conclusions that probably understand the meaning .
All circles passing through a point in Cartesian coordinate system , Mapping to abr
The coordinate system is a threedimensional curve Or explain it as , For Descartes xy
A point in the plane $x_0,y_0$, Corresponding to abr
Form a threedimensional space , Is a space surface , about abr
A point in the plane , Corresponding to Descartes xy
Plane, it's a circle .
All circles passing through all nonzero pixels in Cartesian coordinate system , Constitute the abr
There are many threedimensional curves in the coordinate system In Cartesian coordinate system, all point equations on the same circle are the same , They map to abr
The same point in the coordinate system , So when this point is accumulated to a certain amount （ Generally, it is set to be greater than a threshold ）, It can be regarded as a circle .
If in xy
Three points on the plane $(x_0,y_0),(x_1,y_1),(x_2,y_2)$, stay abr
Three dimensional space is the corresponding three spatial surfaces ( here abr
It's a constant ）. The above content is described as the following equation ： $(x_0a)^2 + (y_0  a)^2=r^2$ $(x_1a)^2 + (y_1  a)^2=r^2$ $(x_2a)^2 + (y_2  a)^2=r^2$
Solve these three equations , We can get abr
Value . This explanation $(x_0,y_0),(x_1,y_1),(x_2,y_2)$ These three points are determined by abr
On the determined circle （ namely abr
They represent the coordinates of the center of a circle (x,y)
And the radius of the circle r
）.
The above description is the principle of Standard Hough circle transformation , But the amount of calculation in threedimensional space is very large , It is difficult to apply the Standard Hough circle variation to practice . Therefore, the functions involved in this paper , Hoff gradient method , Also called 21 Hough transform (21HT).
The function prototype
python OpenCV Provides HoughCircles
Function to find a circle , The function prototype is as follows ：
circles = cv2.HoughCircles(image, method, dp, minDist[, circles[, param1[, param2[, minRadius[, maxRadius]]]]])
Copy code
The parameters are as follows ：
image
： The input image ;method
： Method of detecting circle , At present, we supportcv2.HOUGH_GRADIENT
;dp
： The accumulator resolution is inversely proportional to the image resolution ,dp
Get bigger , The smaller the accumulator array , The general default is 1;minDist
： The distance between the center of a circle ;param1
： Edge detection gradient value ,Canny High threshold of function , Default 100;param2
：cv2.HOUGH_GRADIENT
Accumulator threshold , The smaller the threshold , The more circles detected , Default 100;minRadius
： Minimum radius , In pixels ;maxRadius
： Maximum radius , In pixels ;
The parameters of each vector in the return value are ： The first element is the abscissa of the circle , The second is the ordinate , The third is the radius size .
Be careful ：minRadius
and maxRadius
You can better choose a circle , If you don't need to , Keep the default 0 that will do .
The test code is as follows , You can directly view the effect when running .
import cv2
src = cv2.imread("./core.jpg")
# Image preprocessing
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
img = cv2.medianBlur(gray, 7)
circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1,
60, param1=190, param2=30, minRadius=50, maxRadius=0)
# TypeError: Argument 'radius' is required to be an integer
for x, y, r in circles[0]:
cv2.circle(src, (int(x), int(y)), int(r), (0, 0, 255), 2, cv2.LINE_AA)
cv2.imshow('circle', src)
cv2.waitKey(0)
cv2.destroyWindow()
Copy code
The operation effect is shown in the figure below , Although there is no problem with the test , But it takes a lot of time to adjust parameters , Mainly in the minDist
Parameters 、param1
Parameter , If not adjusted slightly , The following picture will appear .
Hoff circle detection is sensitive to noise , All Hough circle detection should be conducted with median filtering first . Denoising using Gaussian filter , Modify the code as follows ：
gaussian = cv2.GaussianBlur(gray, (7, 7),0)
circles = cv2.HoughCircles(gaussian, cv2.HOUGH_GRADIENT, 1,
60, param1=220, param2=30, minRadius=50, maxRadius=0)
Copy code
If you want to add edge detection , Code tuning is even more cumbersome , For example, increase Canny operator
gaussian = cv2.GaussianBlur(gray, (7, 7),0)
# utilize Canny Edge detection
edges = cv2.Canny(gaussian,160,180, apertureSize=3)
cv2.imshow("edges",edges)
circles = cv2.HoughCircles(edges, cv2.HOUGH_GRADIENT, 1,
60, param1=1500, param2=30, minRadius=50, maxRadius=0)
Copy code
The final effect is also satisfactory
If the following error occurs during code running , Indicates that no circles were found , Continue to modify parameters .
TypeError: 'NoneType' object is not subscriptable
Copy code
copyright notice
author[Dream eraser],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201300048441823.html
The sidebar is recommended
 Install tensorflow and python 3.6 in Windows 7
 Python collects and monitors system data  psutil
 Getting started with Python  object oriented  special methods
 Teach you how to use Python to transform an alien invasion game
 You can easily get started with Excel. Python data analysis package pandas (VI): sorting
 Implementation of toplevel design pattern in Python
 Using linear systems in python with scipy.linalg
 How to get started quickly? How to learn Python
 Modifying Python environment with Mac OS security
 Better use atom to support jupyter based Python development
guess what you like

Better use atom to support jupyter based Python development

Fast power modulus Python implementation of large numbers

Python architects recommend the book "Python programmer's Guide" which must be read by selfstudy Python architects. You are welcome to take it away

Decoding the verification code of Taobao slider with Python + selenium, the road of information security

Python game development, pyGame module, python implementation of skiing games

Python collects and monitors system data  psutil

Python + selenium automated test: page object mode

You can easily get started with Excel. Python data analysis package pandas (IV): any grouping score bar

Opencv skills  saving pictures in common formats as transparent background pictures (with Python source code)  teach you to easily make logo

Python ThreadPoolExecutor restrictions_ work_ Queue size
Random recommended
 Python generates and deploys verification codes with one click (Django)
 With "Python" advanced, you can catch all the advanced syntax! Advanced function + file operation, do not look at regret Series ~
 At the beginning of "Python", you must see the series. 10000 words are only for you. It is recommended to like the collection ~
 [Python kaggle] pandas basic exercises in machine learning series (6)
 Using linear systems in python with scipy.linalg
 The founder of pandas teaches you how to use Python for data analysis (mind mapping)
 Using Python to realize national secondhand housing data capture + map display
 Python image processing, automatic generation of GIF dynamic pictures
 Pandas advanced tutorial: time processing
 How to make Python run faster? Six tips!
 Django: use of elastic search search system
 Python 3.10 official release
 Python chat room (Tkinter writing interface, streaming, socket to realize private chat, group chat, check chat records, Mysql to store data)
 This pandas exercise must be successfully won
 [algorithm learning] sword finger offer 64 Find 1 + 2 +... + n (Java / C / C + + / Python / go / trust)
 leetcode 58. Length of Last Word（python）
 Problems encountered in writing the HTML content of articles into the database during the development of Django blog
 Understand Python's builtin function and add a print function yourself
 Python implements JS encryption algorithm in thousands of music websites
 leetcode 35. Search Insert Position（python）
 leetcode 1829. Maximum XOR for Each Query（python）
 [introduction to Python visualization]: 12 small examples of complete data visualization, taking you to play with visualization ~
 Learning this Python library can reduce at least 100 lines of code
 leetcode 67. Add Binary（python）
 Regular re parameter replacement of Python 3 interface automation test framework
 V. pandas based on Python
 Only 15 lines of code is needed for face detection! (using Python and openCV)
 [Python crawler Sao operation] you can crawl Sirius cinema movies without paying
 leetcode 69. Sqrt(x)（python）
 Teach you to read the source code of Cpython (I)
 Snowball learning started in the fourth quarter of Python. One needs three meals. I have a new understanding of Python functional programming, processoriented, objectoriented and functional
 leetcode 88. Merge Sorted Array（python）
 Don't you know more about a python library before the end of 2021?
 Python crawler web page parsing artifact XPath quick start teaching!!!
 Use Python and OpenCV to watermark the image
 String and related methods of Python data type introduction
 Heapq module of Python module
 Introduction to beautiful soup of Python crawler weapon, detailed explanation, actual combat summary!!!
 Event loop of Python collaboration series
 Django docking pin login system