使用opencv 识别圆圈

import cv2 as cv
import numpy as np

#读取文件
img =  'C:/Users/DELL/Desktop/pic2/1.png'
planets = cv.imread(img)

#转成灰度图片
gray = cv2.cvtColor(planets, cv2.COLOR_RGB2GRAY)

#sobel边缘检测
sobel_x = cv2.Sobel(gray, cv2.CV_8U, 1, 0)
#霍夫圆检测
circles = cv.HoughCircles(sobel_x, cv.HOUGH_GRADIENT, 1, 50, param1=50, param2=30, minRadius=0, maxRadius=30)

circles = np.uint16(np.around(circles))

#将检测到的圆圈标上去
for i in circles[0, :]:  # 遍历矩阵每一行的数据
    cv.circle(planets, (i[0], i[1]), i[2], (0, 255, 0), 2)
    cv.circle(planets, (i[0], i[1]), 2, (0, 0, 255), 3)

#显示图像
cv.imshow("gay_img", planets)
cv.waitKey(0)
cv.destroyAllWindows()

你可能感兴趣的:(机器人视觉,opencv,计算机视觉,python)