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

问题描述:在进行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。

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"ValueError: The truth value of an array with more than one element is ambiguous."这个错误是由于在使用if语句或布尔运算符(如==、!=、<等)时,对于包含多个元素的numpy数组,无法明确地确定其真值造成的。 具体来说,在Python中,对于一个单个元素的变量,可以使用if语句来检查其真值。但是,当变量是一个numpy数组时,由于numpy数组可能包含多个元素,所以无法直接使用if语句来检查其真值。这是因为存在多种可能的解释:数组中的所有元素都为真时,数组中的任意一个元素为真时,或者数组中的所有元素都为假时。因此,需要使用numpy提供的函数来明确指定判断条件,并避免出现歧义。 在这种情况下,可以使用numpy的any()函数或all()函数来判断数组的真值。any()函数用于判断数组中是否存在一个或多个元素为真,而all()函数则用于判断数组中的所有元素是否都为真。通过使用这两个函数,可以避免出现上述的ValueError错误。 需要注意的是,使用这些函数时需要将numpy数组作为参数传递给它们,而不能直接使用if语句或布尔运算符对数组进行判断。这样可以确保对多维数组的判断结果是准确的并避免出现歧义。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [python-numpy-ValueError: The truth value of an array with more than one element is ambiguous.](https://blog.csdn.net/PSpiritV/article/details/124339801)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.al](https://blog.csdn.net/m0_57236802/article/details/128714704)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值