[numpy问题]The truth value of an array with more than one element is ambiguous.

  • Post author:
  • Post category:其他


问题描述:在进行Hough圆变换时,需要输出一个圆的坐标:

circles = cv2.HoughCircles(canny, cv2.HOUGH_GRADIENT, 2, 40, param1=30, param2=30, minRadius=0, maxRadius=20)

接下来会根据这个圆的坐标来画圆心:

for circle in circles[0]:
    x=int(circle[0])
    y=int(circle[1])
    r=int(circle[2])
    #crop = img.copy()
    crop=cv2.circle(crop,(x,y),r,(255,255,255),1)
    crop = cv2.circle(crop, (x, y), 1, (255, 255, 255), -1)
    cv2.imshow("crop", crop)

这时候如果hough圆变换没有检测出来圆的话会报错会显示circles是NoneType,如果加一个判定

if circles!=None:

则会报错

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()


意思上一个判断语句有些模棱两可,circles是很多个圆,不一定所有的都是NoneType。

根据提示修改判断语句为:

if  np.all(circles!=None):
    for circle in circles[0]:
    x=int(circle[0])
    y=int(circle[1])
    r=int(circle[2])
    #crop = img.copy()
    crop=cv2.circle(crop,(x,y),r,(255,255,255),1)
    crop = cv2.circle(crop, (x, y), 1, (255, 255, 255), -1)
    cv2.imshow("crop", crop)

表示circles里面所有的圆都不是Nonetype格式时,判断为真。解决掉了这个bug。



版权声明:本文为weixin_39449466原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。