问题描述:在利用opencv中霍夫圆检测方法来检测图像中圆形物体时,代码如下:
# 1 读取图像,并转换为灰度图 planets = cv.imread("./xingqiu.png") gay_img = cv.cvtColor(planets, cv.COLOR_BGRA2GRAY) # 2 进行中值模糊,去噪点 img = cv.medianBlur(gay_img, 7) # 3 霍夫圆检测 circles = cv.HoughCircles(img, cv.HOUGH_GRADIENT, 1, 200, param1=100, param2=30, minRadius=0, maxRadius=100) # 4 将检测结果绘制在图像上 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)
错误描述:
cv2.error: OpenCV(4.5.4-dev) :-1: error: (-5:Bad argument) in function 'circle' > Overload resolution failed: > - Can't parse 'center'. Sequence item with index 0 has a wrong type > - Can't parse 'center'. Sequence item with index 0 has a wrong type
错误原因:cv.circle()函数中的圆心坐标不能是浮点数的类型, 要转换成整数才行,于是就使用int()强制类型转换,之后代码成功运行。如下所示:
cv.circle(planets, (int(i[0]), int(i[1])), int(i[2]), (0, 255, 0), 2) cv.circle(planets, (int(i[0]), int(i[1])), 2, (0, 0, 255), 3)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)