In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how Python OpenCV based on Hoff circle transformation algorithm to detect circles in the image, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Chapter 1: Hough transform detection circle ① example demonstration 1
This is the effect of setting the radius range from 0 to 50.
② example demo 2
This is the effect after setting the radius range of 50-70, because the original image is a little larger and the radius is also a little larger.
Analysis of ③ Hough transform function
Cv.HoughCircles () method
Parameters are: image, method, dp, minDist, param1, param2, minRadius, maxRadius
Where:
Image is a grayscale image
The method used by method is Hough gradient method. At present, there are two known methods: HOUGH_GRADIENT and HOUGH_GRADIENT_ALT, and the accuracy of the latter is a little higher.
Dp is the inverse ratio of the accumulator resolution to the image resolution.
MinDist is the minimum distance between the centers of two circles
Param1 represents the threshold for incoming canny edge detection for CV_HOUGH_GRADIENT
Param2 indicates to CV_HOUGH_GRADIENT the cumulative threshold of the center of the circle in the detection phase. The smaller the value is, the more circles can be detected, and the higher the value is, the fewer circles will be detected, but the circles detected will be more round and more perfect than those that have not been detected.
MinRadius is the minimum radius
MinRadius is the maximum radius
First of all, the interference points are eliminated by mean shift filtering to improve the accuracy of recognition, and then gray processing is carried out.
# mean shift filtering noise reduction mean_filter_img = cv.pyrMeanShiftFiltering (image, 10,100) cv.imshow ("mean_filter_img", mean_filter_img) # Image grayscale processing gray_img = cv.cvtColor (mean_filter_img, cv.COLOR_BGR2GRAY) # Hough Circle transform # parameters are: image, method, dp, minDist, param1, param2, minRadius, maxRadius# where image is a grayscale image, and the method used by method is Hough gradient method The minimum distance between the centers of two circles of minDist circles = cv.HoughCircles (gray_img, cv.HOUGH_GRADIENT, 1,30, param1=50, param2=30, minRadius=0 MaxRadius=50) Chapter 2: Python + opencv complete detection code ① source code #-*-coding:utf-8-*-# 2021-12-author: small blue jujube # opencv circle detection import cv2 as cvimport numpy as npdef detect_circle (image):''function: circle detection parameters: need to detect circle picture return: detect circle information''# both Value offset filtering noise reduction process mean_filter_img = cv.pyrMeanShiftFiltering (image 10,100) cv.imshow ("mean_filter_img", mean_filter_img) # Image Grayscale processing gray_img = cv.cvtColor (mean_filter_img, cv.COLOR_BGR2GRAY) # Hough Circle transform # parameters are: image, method, dp, minDist, param1, param2, minRadius, maxRadius # where: image is a grayscale image The method used by method is Hough gradient method. The minimum distance between the centers of two circles of minDist circles = cv.HoughCircles (gray_img, cv.HOUGH_GRADIENT, 1,30, param1=50, param2=30, minRadius=0, maxRadius=50) # rounding the data print ("str (circles)) circles = np.uint16 (np.around (circles)) print (" after rounding: "str (circles)) return circles def draw_circle (img) Circles):''function: draw a circle according to the circle information in the picture parameter 1: original picture information parameter 2: circle coordinate information return: none' for i in circles [0,:]: # draw the outer circle of the circle # parameters are: circle center, radius, color, wireframe width cv.circle (img, (I [0]) I [1]), I [2], (0,0,255), 2) # draw the center cv.circle (img, (I [0], I [1]), 2, (255,0,0), 2) cv.imshow ("draw_circle_img" Img) # read picture information img = cv.imread (". / image/meixi.jpg") # Settings window cannot be resized (parameters include: WINDOW_AUTOSIZE, WINDOW_NORMAL, WINDOW_OPENGL) cv.namedWindow ("original image", cv.WINDOW_AUTOSIZE) cv.imshow ("original image", img) # check circle circles = detect_circle (img) # draw circle draw_circle (img, circles) cv.waitKey (0) cv.destroyAllWindows () ② run effect diagram
Original picture:
Effect after noise reduction:
Hough transform detects the effect of circle processing:
Rounding effect picture:
These are all the contents of the article "how to detect circles in an image based on the Python OpenCV algorithm based on Hough circle transformation". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.