我试图计算图像渐变的中位数,但继续得到numpy中值函数的误差 . 以下是我使用的示例代码:
# Compute gradients
print("Computing gradients.")
gradx = map(lambda x: cv2.Sobel(x, cv2.CV_64F, 1, 0, ksize=KERNEL_SIZE), images)
grady = map(lambda y: cv2.Sobel(y, cv2.CV_64F, 0, 1, ksize=KERNEL_SIZE), images)
以下是上述代码的示例输出:
[array([[[ 0., 0., 0.],
[ 114., 52., 350.],
[ 78., 118., 420.],
...,
[ -56., -192., -262.],
[ -86., -162., -150.],
[ 0., 0., 0.]],
[[ 0., 0., 0.],
[ 107., 45., 299.],
[ 54., 78., 328.],
...,
[ -50., -177., -257.],
[ -72., -148., -138.],
[ 0., 0., 0.]],
[[ 0., 0., 0.],
[ 81., 12., 149.],
[ 30., 9., 121.],
...,
[ -37., -152., -252.],
[ -43., -128., -117.],
[ 0., 0., 0.]],
现在,我试图计算gradx和grady的中位数
# Compute median of grads
print("Computing median gradients.")
Wm_x = np.median(np.array(gradx), axis=0)
Wm_y = np.median(np.array(grady), axis=0)
但我每次都会收到错误:
IndexError Traceback (most recent call last)
in()29#计算梯度中位数30打印(“计算中位数梯度” . )---> 31 Wm_x = np.median(np.array(gradx),轴= 0)32 Wm_y = np.median(np . array(grady),axis = 0)33
/Users/ksrivasta/anaconda3/lib/python3.6/site-packages/numpy/lib/function_base.py in median(a,axis,out,overwrite_input,keepdims)3942“”“3943 r,k = _ureduce(a, func = _median,axis = axis,out = out, - > 3944 overwrite_input = overwrite_input)3945 if keepdims:3946 return r.reshape(k)
_Users/ksrivasta/anaconda3/lib/python3.6/site-packages/numpy/lib/function_base.py in _ureduce(a,func,** kwargs)3834 axis = operator.index(axis)3835 if axis> = nd或轴<-nd: - > 3836引发IndexError(“轴%d超出界限(%d)”%(轴,a.ndim))3837 keepdim [轴] = 1 3838除TypeError:
IndexError:轴0超出范围(0)
我无法理解如何解决这个问题 . 请指教 .