GVKun编程网logo

使用动态创建的布尔掩码时,numpy“ TypeError:输入类型不支持ufunc'bitwise_and'”

1

在本文中,我们将给您介绍关于使用动态创建的布尔掩码时,numpy“TypeError:输入类型不支持ufunc'bitwise_and'”的详细内容,此外,我们还将为您提供关于bitwise_and方

在本文中,我们将给您介绍关于使用动态创建的布尔掩码时,numpy“ TypeError:输入类型不支持ufunc'bitwise_and'”的详细内容,此外,我们还将为您提供关于bitwise_and 方法上的 OpenCV 断言错误、cv2.bitwise_and:操作既不是'array op array'、Jupyter 中的 Numpy 在打印时出错(Python 版本 3.8.8):TypeError: 'numpy.ndarray' object is not callable、Numpy TypeError: 'numpy.float64' 对象不可迭代 使用的代码错误原因解决方案的知识。

本文目录一览:

使用动态创建的布尔掩码时,numpy“ TypeError:输入类型不支持ufunc'bitwise_and'”

使用动态创建的布尔掩码时,numpy“ TypeError:输入类型不支持ufunc'bitwise_and'”

在numpy中,如果我有一个浮点数组,则动态创建一个布尔掩码,使该数组等于某个特定值,然后对布尔数组进行按位与运算,则会收到错误消息:

>>> import numpy as np>>> a = np.array([1.0, 2.0, 3.0])>>> a == 2.0 & bTraceback (most recent call last):  File "<stdin>", line 1, in <module>TypeError: ufunc ''bitwise_and'' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''''safe''

如果我将比较结果保存到变量中并按位执行AND,则可以:

>>> c = a == 2.0>>> c & barray([False,  True, False], dtype=bool)

但是,每种情况下创建的对象看起来都相同:

>>> type(a == 2.0)<type ''numpy.ndarray''>>>> (a == 2.0).dtypedtype(''bool'')>>> type(c)<type ''numpy.ndarray''>>>> c.dtypedtype(''bool'')

为什么会有所不同?

答案1

小编典典

&具有比更高的优先级==,因此表达式

a == 2.0 & b

是相同的

a == (2.0 & b)

因为and没有为浮点标量和布尔数组定义按位,所以会出现错误。

添加括号以获得您所期望的:

(a == 2.0) & b

bitwise_and 方法上的 OpenCV 断言错误

bitwise_and 方法上的 OpenCV 断言错误

如何解决bitwise_and 方法上的 OpenCV 断言错误

import cv2
import numpy as np

img = cv2.imread("Yash.jpeg")
blank = np.zeros(img.shape[:2])

cv2.imshow("YASH",img)
cv2.imshow("Blank",blank)

mask = cv2.circle(blank,(img.shape[1]//2,img.shape[0]//2),150,255,-1)
cv2.imshow("Mask",mask)

masked = cv2.bitwise_and(img,img,mask = mask)  # Error in this line
cv2.imshow("MASKED",masked)

cv2.waitKey(0)
cv2.destroyAllWindows()

** 回溯(最近一次调用最后一次): 文件“D:/pythonProject1/masing.py”,第 13 行,在 掩码 = cv2.bitwise_or(img,mask = mask) cv2.error: OpenCV(4.5.1) C:\\Users\\appveyor\\AppData\\Local\\Temp\\1\\pip-req-build-i1s8y2i1\\opencv\\modules\\core\\src\\arithm.cpp:250: 错误: ( -215: 断言失败) (mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1) in function ''cv::binary_op'' **

解决方法

只是更正:

blank = np.zeros(img.shape[:2],dtype=''uint8'')

错误是数据类型错误

cv2.bitwise_and:操作既不是'array op array'

cv2.bitwise_and:操作既不是'array op array'

如何解决cv2.bitwise_and:操作既不是''array op array''

我正在努力理解数组的大小是如何相互矛盾的。我从''cv_image''开始得到1和0的canny_image:

    # Convert to Gray --> blur --> canny
    gray_image = cv2.cvtColor(cv_image,cv2.COLOR_BGR2GRAY)
    k_width = 21 # 7
    k_height = 21 # 7
    blurred = cv2.GaussianBlur(gray_image,(k_width,k_height),0)
    threshold_low = 40 # 40
    threshold_high = 20 # 20 
    canny_image = cv2.Canny(blurred,threshold_low,threshold_high)
    kernel = np.ones((7,7),dtype=np.uint8)
    mask = cv2.dilate(canny_image,kernel,iterations =2)

接下来是根据“cv_image”大小创建感兴趣区域:

    ## Region of Interest
    triangle = np.array([
        [(50,height),(590,(320,200)]
        ])
    mask_roi = np.zeros_like(cv_image)
    cv2.fillpoly(mask_roi,triangle,255)


    ## Masking Region of Interest
    masked_image = cv2.bitwise_and(canny_image,mask_roi) 
    masked_image2 = cv2.bitwise_and(canny_image,mask_roi,mask= None) 
    masked_image3 = cv2.bitwise_and(canny_image,canny_image,mask = mask_roi)  

迭代 masked_image2、masked_image3 是出于挫败感。有人可以指出我的错误吗?我以为我有相同的数组大小和 1 个通道...

 The operation is neither ''array op array'' (where arrays have the same size and type),nor ''array op scalar'',nor ''scalar op array'' in function binary_op

  

Jupyter 中的 Numpy 在打印时出错(Python 版本 3.8.8):TypeError: 'numpy.ndarray' object is not callable

Jupyter 中的 Numpy 在打印时出错(Python 版本 3.8.8):TypeError: 'numpy.ndarray' object is not callable

如何解决Jupyter 中的 Numpy 在打印时出错(Python 版本 3.8.8):TypeError: ''numpy.ndarray'' object is not callable?

晚安, 尝试打印以下内容时,我在 jupyter 中遇到了 numpy 问题,并且得到了一个 错误: 需要注意的是python版本是3.8.8。 我先用 spyder 测试它,它运行正确,它给了我预期的结果

使用 Spyder:

import numpy as np
    for i in range (5):
        n = np.random.rand ()
    print (n)
Results
0.6604903457995978
0.8236300859753154
0.16067650689842816
0.6967868357083673
0.4231597934445466

现在有了 jupyter

import numpy as np
    for i in range (5):
        n = np.random.rand ()
    print (n)
-------------------------------------------------- ------
TypeError Traceback (most recent call last)
<ipython-input-78-0c6a801b3ea9> in <module>
       2 for i in range (5):
       3 n = np.random.rand ()
---->  4 print (n)

       TypeError: ''numpy.ndarray'' object is not callable

感谢您对我如何在 Jupyter 中解决此问题的帮助。

非常感谢您抽出宝贵时间。

阿特,约翰”

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

Numpy TypeError: 'numpy.float64' 对象不可迭代 使用的代码错误原因解决方案

Numpy TypeError: 'numpy.float64' 对象不可迭代 使用的代码错误原因解决方案

如何解决Numpy TypeError: ''numpy.float64'' 对象不可迭代 使用的代码错误原因解决方案

我在运行 Python 脚本时遇到的错误:

How to solved TypeError: ''numpy.float64'' object is not iterable

使用的代码

arr = np.array([[3,4,5],[5,6,7],[2,3,4]])


total = sum(sum(arr))

mean = sum(sum(arr))/(3*3)

for i in arr :
    vr= i - mean
    for med in vr**2 :             
        print(sum(med))

解决方法

错误原因

您收到此错误:

TypeError: ''numpy.float64'' 对象不可迭代

您正在使用 for 循环遍历假定的数组:

    for med in vr**2 :             

表达式 vr**2 不是一个 数组,而是一个原始的 float

因此您会收到此错误。

解决方案

删除第二个 for 循环:

import numpy as np

arr = np.array([[3,4,5],[5,6,7],[2,3,4]])

total = sum(sum(arr))
mean = sum(sum(arr))/(3*3)

for i in arr :
    vr = i - mean
    print(sum(vr**2))

每个总和的输出为 3 行:

2
14
5

试试这个代码 demo in IDEone


建议: 始终在问题正文中以纯文本形式发布错误输出(不仅在标题中,而不是图像中)。这样我们就可以复制粘贴了。

,

med 是一个数字(浮点数),因此你不能取它的总和(它的总和是它本身)。此外,使用 Numpy 的主要目的是避免循环并立即进行数组计算。像这样:

arr = np.array([[3,4]])

total = arr.sum()
mean = total/arr.size #equivalent of total=arr.mean()
vr = ((arr - mean)**2).sum() #equivalent of var=np.var(arr)*arr.size

今天关于使用动态创建的布尔掩码时,numpy“ TypeError:输入类型不支持ufunc'bitwise_and'”的讲解已经结束,谢谢您的阅读,如果想了解更多关于bitwise_and 方法上的 OpenCV 断言错误、cv2.bitwise_and:操作既不是'array op array'、Jupyter 中的 Numpy 在打印时出错(Python 版本 3.8.8):TypeError: 'numpy.ndarray' object is not callable、Numpy TypeError: 'numpy.float64' 对象不可迭代 使用的代码错误原因解决方案的相关知识,请在本站搜索。

本文标签: