"TypeError : 정수 스칼라 배열 만 스칼라 인덱스로 변환 될 수 있습니다."float를 배열의 값과 비교할 때

사용자 4302

Gmat[i,j]다음과 같은 비 최대 억제 기능을 실행하려고 할 때 -22.5가 비교되는 줄에서 오류가 발생 합니다.

내가 사용 np.all하고 np.any같은 andor'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이 2 차원 배열로 가정 할 때 3 차원 배열했다.

2 차원 어레이로 변환하고 Gmag 사용 numpy.logical_and하고 numpy.logical_or대신 numpy.anynumpy.all문제 해결.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관