The solution result of numpy gradient function is inconsistent when the variables are array and matrix.

topic description

use the numpy gradient function to calculate the gradient

related codes

/ / Please paste the code text below (do not replace the code with pictures)

g = img [0 3, 0:3]

g =
array ([[138144141],

   [134, 135, 132],
   [137, 135, 133]], dtype=uint8)

gy, gx = np.gradient (g)
gy=
array ([252. , 247. , 247. ],

   [127.5, 123.5, 124. ],
   [  3. ,   0. ,   1. ]])

gx=
array ([6,1.5,253. ],

   [  1. , 127. , 253. ],
   [254. , 126. , 254. ]])
   

arr=np.array ([[138144141], [134135132], [137135133]])
arr=
array ([[138144141],

)
   [134, 135, 132],
   [137, 135, 133]])
   

gy, gx = np.gradient (arr)
gy=
array ([- 4. 9. 9. ],

   [-0.5, -4.5, -4. ],
   [ 3. ,  0. ,  1. ]])

gx=
array ([6. 1. 5,-3. ],

   [ 1. , -1. , -3. ],
   [-2. , -2. , -2. ]]) 

what result do you expect? What is the error message actually seen?

Why are the two results inconsistent? how is the gradient calculated under the matrix type?

Apr.05,2021

data overflow, convert the matrix type to float and then calculate the gradient

Menu