将浮点数与数组中的值进行比较时,“ TypeError:只能将整数标量数组转换为标量索引”

用户4302

Gmat[i,j]当我尝试运行以下非最大抑制函数时,在与-22.5进行比较的那一行出现错误

我使用np.alland np.anyasandor会给出一个称为'ValueError的错误:具有多个元素的数组的真值是不明确的。使用a.any()或a.all()'

def non_max_suppression(Gmag, Gmat):
   nms_img = np.zeros(Gmag.shape)
   for i in range (1, int(Gmag.shape[0])-1):
       for j in range (1, int(Gmag.shape[1])-1):
           if np.any(
                   np.all(
                       Gmat[i,j] >=
                       -22.5 ,
                       Gmat[i,j] <=
                       22.5) , #doesnt go past here, this is where the error occurs
                   np.all(Gmat[i,j] <= -157.5, Gmat[i,j] >= 157.5)
           ):
               if np.logical_and(Gmag[i,j] > Gmag[i,j+1] , Gmag[i,j] > Gmag[i,j-1]):
                   nms_img[i,j] = Gmag[i,j]
               else:
                   nms_img[i,j] = 0

           if np.logical_or(
                   np.logical_and(Gmat[i,j] >= 22.5 , Gmat[i,j] <= 67.5) ,
                   np.logical_and(Gmat[i,j] <= -112.5 , Gmat[i,j] >= -157.5)
           ):
               if np.logical_and(Gmag[i,j] > Gmag[i+1,j] , Gmag[i,j] > Gmag[i-1,j-1]):
                   nms_img[i,j] = Gmag[i,j]
               else:
                   nms_img[i,j] = 0

           if np.logical_or(
                   np.logical_and(Gmat[i,j] >= 67.5 , Gmat[i,j] <= 112.5) ,
                   np.logical_and(Gmat[i,j] <= -67.5 , Gmat[i,j] >= -112.5)
           ):
               if np.logical_and(Gmag[i,j] > Gmag[i+1,j] , Gmag[i,j] > Gmag[i-1,j]):
                   nms_img[i,j] = Gmag[i,j]
               else:
                   nms_img[i,j] = 0

           if np.logical_or(
                   np.logical_and(Gmat[i,j] >= 112.5 , Gmat[i,j] <= 157.5) ,
                   np.logical_and(Gmat[i,j] <= -22.5 , Gmat[i,j] >= -67.5)
           ):
               if np.logical_and(Gmag[i,j] > Gmag[i,j+1] , Gmag[i,j] > Gmag[i-1,j+1]):
                   nms_img[i,j] = Gmag[i,j]
               else:
                   nms_img[i,j] = 0
   return nms_img

Gmag和Gmat的值分别为以下值:

 Mag = {ndarray: (262, 393, 3)} [[[0. 0. 0.],  [0. 0. 0.],  [0. 0. 0.],  ...,  [0. 0. 0.],  [0. 0. 0.],  [0. 0. 0.]],, [[0. 0. 0.],  [0. 0. 0.],  [0. 0. 0.],  ...,  [0. 0. 0.],  [0. 0. 0.],  [0. 0. 0.]],, [[0. 0. 0.],  [0. 0. 0.],  [0. 0. 0.],  ...,  [0. 0. 0.],  [0. 0. 0.],  [0. 0. 0.]]
 min = {float64} 0.0
 max = {float64} 1.7116336200484585
 shape = {tuple: 3} (262, 393, 3)
 dtype = {dtype: 0} float64
 size = {int} 308898
 array = {NdArrayItemsContainer} <pydevd_plugins.extensions.types.pydevd_plugin_numpy_types.NdArrayItemsContainer object at 0x00000275D41FAC08>

 Gmat = {ndarray: (262, 393, 3)} [[[0. 0. 0.],  [0. 0. 0.],  [0. 0. 0.],  ...,  [0. 0. 0.],  [0. 0. 0.],  [0. 0. 0.]],, [[0. 0. 0.],  [0. 0. 0.],  [0. 0. 0.],  ...,  [0. 0. 0.],  [0. 0. 0.],  [0. 0. 0.]],, [[0. 0. 0.],  [0. 0. 0.],  [0. 0. 0.],  ...,  [0. 0. 0.],  [0. 0. 0.],  [0. 0. 0.]]
 min = {float64} -135.0
 max = {float64} 45.0
 shape = {tuple: 3} (262, 393, 3)
 dtype = {dtype: 0} float64
 size = {int} 308898
 array = {NdArrayItemsContainer} <pydevd_plugins.extensions.types.pydevd_plugin_numpy_types.NdArrayItemsContainer object at 0x00000275D46BE048>

完整的错误:

TypeError                                 Traceback (most recent call last)

<ipython-input-104-991cd7244b0e> in <module>
      1 #============================ 10 Apply Non-Maximum Suppression
----> 2 img_NMS = non_max_suppression(Mag, Gmat)
      3 img_NMS = normalize(img_NMS)
      4 
      5 plt.imshow(img_NMS, cmap = plt.get_cmap('gray'))

<ipython-input-103-2ec719b48bd0> in non_max_suppression(Gmag, Gmat)
      9                         -22.5 ,
     10                         Gmat[i,j] <=
---> 11                         22.5) ,
     12                     np.all(Gmat[i,j] <= -157.5, Gmat[i,j] >= 157.5)
     13             ):

<__array_function__ internals> in all(*args, **kwargs)

c:\users\user\appdata\local\programs\python\python37\lib\site-packages\numpy\core\fromnumeric.py in all(a, axis, out, keepdims)
   2409 
   2410     
-> 2411     return _wrapreduction(a, np.logical_and, 'all', axis, None, out, keepdims=keepdims)
   2412 
   2413 

c:\users\user\appdata\local\programs\python\python37\lib\site-packages\numpy\core\fromnumeric.py in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
     85                 return reduction(axis=axis, out=out, **passkwargs)
     86 
---> 87     return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
     88 
     89 

TypeError: only integer scalar arrays can be converted to a scalar index
用户4302

Gmag是一个三维阵列,当它被认为是一个二维数组。

将Gmag转换为2d数组,并使用numpy.logical_andnumpy.logical_or代替numpy.anynumpy.all修复了该问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将浮点数与数组中的值进行比较时,“ TypeError:只能将整数标量数组转换为标量索引”

来自分类Dev

TypeError:在转换数据帧“ to_datetime”时,只能将整数标量数组转换为标量索引

来自分类Dev

TypeError:在计算RMSE时,只能将整数标量数组转换为标量索引

来自分类Dev

Python TypeError:只能将整数标量数组转换为标量索引

来自分类Dev

Scipy peak_widths返回TypeError:只能将整数标量数组转换为标量索引

来自分类Dev

在Python-TypeError中按列对分组进行计数时出错:只能将整数标量数组转换为标量索引

来自分类Dev

TensorFlow 对象检测 API 中的“TypeError:只有整数标量数组可以转换为标量索引”

来自分类Dev

如何解决Matplotlib TypeError:仅整数标量数组可以转换为标量索引

来自分类Dev

(Python) TypeError:只有整数标量数组可以转换为标量索引

来自分类Dev

绘制Wavefunction:TypeError:只能将Size-1数组转换为Python标量

来自分类Dev

Matplotlib条形图错误-TypeError:只能将size-1数组转换为Python标量

来自分类Dev

这是什么意思-TypeError:只能将length-1数组转换为Python标量?

来自分类Dev

熊猫:尝试合并数据帧时,出现“ TypeError:仅整数标量数组可以转换为标量索引”

来自分类Dev

Python:具有可变范围的有限和“TypeError:只有整数标量数组可以转换为标量索引”

来自分类Dev

使用Matlab键时,只能将length-1数组转换为Python标量

来自分类Dev

错误:更改变量顺序时,[只能将长度为1的数组转换为Python标量]

来自分类Dev

类型错误:只能将长度为1的数组转换为Python标量

来自分类Dev

在Sage中使用scipy.optimize“只能将长度为1的数组转换为Python标量”

来自分类Dev

熊猫-只能将大小为1的数组转换为Python标量

来自分类Dev

只能将size-1数组转换为Python标量python

来自分类Dev

我该如何解决?只能将size-1数组转换为Python标量

来自分类Dev

ValueError:只能将大小为1的数组转换为Numpy Select的Python标量

来自分类Dev

Python:类型错误:只能将长度为 1 的数组转换为 Python 标量

来自分类Dev

Python 错误:只能将大小为 1 的数组转换为 Python 标量

来自分类Dev

如何将浮点数与其组成部分是 Python 中的间隔的数组进行比较?

来自分类Dev

TypeError:只能使用NUMPY将长度为1的数组转换为Python标量

来自分类Dev

将 int 转换为数组中的浮点数

来自分类Dev

比较浮点数和浮点数元素数组时出错

来自分类Dev

为什么在scala中,将整数和浮点数(例如71 == 71.0)进行比较是正确的?

Related 相关文章

  1. 1

    将浮点数与数组中的值进行比较时,“ TypeError:只能将整数标量数组转换为标量索引”

  2. 2

    TypeError:在转换数据帧“ to_datetime”时,只能将整数标量数组转换为标量索引

  3. 3

    TypeError:在计算RMSE时,只能将整数标量数组转换为标量索引

  4. 4

    Python TypeError:只能将整数标量数组转换为标量索引

  5. 5

    Scipy peak_widths返回TypeError:只能将整数标量数组转换为标量索引

  6. 6

    在Python-TypeError中按列对分组进行计数时出错:只能将整数标量数组转换为标量索引

  7. 7

    TensorFlow 对象检测 API 中的“TypeError:只有整数标量数组可以转换为标量索引”

  8. 8

    如何解决Matplotlib TypeError:仅整数标量数组可以转换为标量索引

  9. 9

    (Python) TypeError:只有整数标量数组可以转换为标量索引

  10. 10

    绘制Wavefunction:TypeError:只能将Size-1数组转换为Python标量

  11. 11

    Matplotlib条形图错误-TypeError:只能将size-1数组转换为Python标量

  12. 12

    这是什么意思-TypeError:只能将length-1数组转换为Python标量?

  13. 13

    熊猫:尝试合并数据帧时,出现“ TypeError:仅整数标量数组可以转换为标量索引”

  14. 14

    Python:具有可变范围的有限和“TypeError:只有整数标量数组可以转换为标量索引”

  15. 15

    使用Matlab键时,只能将length-1数组转换为Python标量

  16. 16

    错误:更改变量顺序时,[只能将长度为1的数组转换为Python标量]

  17. 17

    类型错误:只能将长度为1的数组转换为Python标量

  18. 18

    在Sage中使用scipy.optimize“只能将长度为1的数组转换为Python标量”

  19. 19

    熊猫-只能将大小为1的数组转换为Python标量

  20. 20

    只能将size-1数组转换为Python标量python

  21. 21

    我该如何解决?只能将size-1数组转换为Python标量

  22. 22

    ValueError:只能将大小为1的数组转换为Numpy Select的Python标量

  23. 23

    Python:类型错误:只能将长度为 1 的数组转换为 Python 标量

  24. 24

    Python 错误:只能将大小为 1 的数组转换为 Python 标量

  25. 25

    如何将浮点数与其组成部分是 Python 中的间隔的数组进行比较?

  26. 26

    TypeError:只能使用NUMPY将长度为1的数组转换为Python标量

  27. 27

    将 int 转换为数组中的浮点数

  28. 28

    比较浮点数和浮点数元素数组时出错

  29. 29

    为什么在scala中,将整数和浮点数(例如71 == 71.0)进行比较是正确的?

热门标签

归档