Circular detection using OPENCV under Python

Source: Internet
Author: User

write in front of the article

These days because the work needs to learn image detection, stupid I do not want to stare at OPENCV start to learn (; ′⌒ '), even the ability to check information is weak 〒▽〒

Praise My best man ticket (*^▽^*) The man is not the image processing but love my stupid (the "contest")

Let me give him the request (our store?? Omega??)  Our store??? He put the game down and spent a day or two doing it for me and then wrote a detailed manual φ (>ω<*)

Of course, write it down. (?????)?

?? ??? 3?? Learn to go

A. Introduction:

The first time you use Python you must feel the convenience of Python. Just call the class library as a high-level programming language.

OPENCV provides a number of methods for the identification of circular objects .

( attached at the end of this code )

two. Detection steps: 2.1 Reading Images

window 1(initial image not processed)

2.2 Noise reduction processing

Because there is a lot of noise in the image (what is the noise reference

Using noise reduction Method Cv2.blur (IMG, (5,5))

Two of these parameters are horizontal longitudinal blur degree, the larger the value the more blurred

This is the degree of ambiguity of 5,5

This is the degree of ambiguity

Here we use the 5,5 effect to test down the best

2.3 Grayscale

grayscale is often used in color-rich images, similar to the fading operation in PS.

Method: Cv2.cvtcolor (Result,cv2. Color_bgr2gray)

Because the original image is black and white main color, so the color change is not big

2.4 Hoffman Change Circle detection

Previous noise reduction and grayscale are all for this step of detection

Reference article

Here's how:

Cv2. Houghcircles (Gray,cv2. HOUGH_GRADIENT,1,50,PARAM1=80,PARAM2=30,MINRADIUS=15,MAXRADIUS=20)

parameter 1 Image: Passing images

parameter 2 method: Default, no understanding

parameter 3 DP: Default, no understanding

parameter 4 mindist: The minimum distance of different centers, in pixels

parameter 5 relates to the Canny algorithm, here The upper limit of the Canny algorithm, where the Canny the lower limit of the algorithm is automatically set to a maximum of half, immediately introduced canny algorithm

parameter 6 need to understand the above reference article, can be considered as the cumulative quantity to be reached

parameter 7,8 is the minimum radius and maximum radius, avoiding the recognition of white circles

So what is the canny algorithm? In simple terms, the edge detection algorithm

Concrete implementation of the results can be referred to the method: Cv2. Canny (IMG, 27, 54), showing the effect as

The smaller the edge of the parameter, the less we use it, which is set to a maximum of 80

Cv2. Canny (IMG, 40, 80)


Circles=cv2. Houghcircles (Gray,cv2. HOUGH_GRADIENT,1,50,PARAM1=80,PARAM2=30,MINRADIUS=15,MAXRADIUS=20)

all recognized round parameters (center position, radius) are saved to circles

Can be thought of as an array

2.5 marks

Using the () method to circle pictures According to The image information obtained by 2.4

Finished, you can adjust the parameters to achieve satisfactory results.

three. Attached Code
#-*-Coding:utf-8-*-import  cv2# loaded and displayed picture Img=cv2.imread (' 1.jpg ') cv2.imshow (' 1 ', img) #降噪 (obfuscation to reduce defect points) result = Cv2.blur (IMG, (5,5)) cv2.imshow (' 2 ', result) #灰度化, is the de-color (similar to vintage photos) Gray=cv2.cvtcolor (result,cv2. Color_bgr2gray) cv2.imshow (' 3 ', gray) #param1的具体实现 for edge detection    canny = cv2. Canny (IMG, max, cv2.imshow)   (' 4 ', Canny)  #霍夫变换圆检测circles = Cv2. Houghcircles (Gray,cv2. HOUGH_GRADIENT,1,50,PARAM1=80,PARAM2=30,MINRADIUS=15,MAXRADIUS=20) #输出返回值 for easy viewing of type print (circles) #输出检测到圆的个数print ( Len (Circles[0])) print ('-------------I'm a split line-----------------') #根据检测到圆的信息, draw each circle for the circle in Circles[0]:    # The basic information of the Circle    print (circle[2])    #坐标行列 (that is, the center)    X=int (circle[0])    y=int (circle[1])    #半径    R=int ( CIRCLE[2])    #在原图用指定颜色圈出圆, the parameter is set to int so there is an error (IMG, (x, y), R, (0,0,255), 1,8,0) #显示新图像cv2. imshow (' 5 ', img) #按任意键退出cv2. Waitkey (0) cv2.destroyallwindows ()

Circular detection using OPENCV under Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.