Python OpenCV 霍夫圆变换检测图像中的圆:示例代码
在Python中,可以使用OpenCV库来实现霍夫圆变换检测图像中的圆。下面是一个示例代码:\n\npython\nimport cv2\nimport numpy as np\n\n# 读取图像\nimage = cv2.imread('image.jpg', 0)\n\n# 预处理图像\nimage = cv2.medianBlur(image, 5)\nimage = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)\n\n# 运行霍夫圆变换\ncircles = cv2.HoughCircles(image, cv2.HOUGH_GRADIENT, 1, 20,\n param1=50, param2=30, minRadius=0, maxRadius=0)\n\n# 将检测到的圆画出来\nif circles is not None:\n circles = np.round(circles[0, :]).astype("int")\n for (x, y, r) in circles:\n cv2.circle(image, (x, y), r, (0, 255, 0), 4)\n\n# 显示结果\ncv2.imshow("Detected Circles", image)\ncv2.waitKey(0)\ncv2.destroyAllWindows()\n\n\n在上述代码中,首先读取图像并进行一些预处理操作,然后使用cv2.HoughCircles()函数进行霍夫圆变换,参数param1和param2分别控制圆心检测的敏感度和圆度检测的严格程度。最后,通过循环遍历检测到的圆,并使用cv2.circle()函数画出圆的轮廓。最后,使用cv2.imshow()函数显示结果。
原文地址: https://www.cveoy.top/t/topic/pQYH 著作权归作者所有。请勿转载和采集!