首页 > 解决方案 > 为什么我的代码显示断言错误和值错误?

问题描述

我正在尝试调整图像大小。为此,我正在使用 skimage 调整大小。为此,输入将是 3d numpy 图像数组。并且输出将再次是一个较小维度的 3d numpy 数组。

downSample = []
for i in range(len(rgb_val)):
    downSample.append(resize(rgb_val[i], (rgb_val[i].shape[0] / 16, rgb_val[i].shape[1] / 16),
                       anti_aliasing=True))

在这里, rgb_val 只不过是一个列表,其中包含我的数据集中的 3d numpy 图像数组。但是在执行这个循环时,它向我显示了以下错误。我应该怎么做才能解决以下错误?

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
~/anaconda3/lib/python3.7/site-packages/skimage/_shared/utils.py in safe_as_int(val, atol)
    147     try:
--> 148         np.testing.assert_allclose(mod, 0, atol=atol)
    149     except AssertionError:

~/anaconda3/lib/python3.7/site-packages/numpy/testing/_private/utils.py in assert_allclose(actual, desired, rtol, atol, equal_nan, err_msg, verbose)
   1451     assert_array_compare(compare, actual, desired, err_msg=str(err_msg),
-> 1452                          verbose=verbose, header=header, equal_nan=equal_nan)
   1453 

~/anaconda3/lib/python3.7/site-packages/numpy/testing/_private/utils.py in assert_array_compare(comparison, x, y, err_msg, verbose, header, precision, equal_nan, equal_inf)
    788                                 names=('x', 'y'), precision=precision)
--> 789             raise AssertionError(msg)
    790     except ValueError:

AssertionError: 
Not equal to tolerance rtol=1e-07, atol=0.001

(mismatch 66.66666666666666%)
 x: array([0.25, 0.5 , 0.  ])
 y: array(0)

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-27-754225493c2e> in <module>
      1 for i in range(len(rgb_val)):
      2     downSample.append(resize(rgb_val[i], ((rgb_val[i].shape[0]) / 16, (rgb_val[i].shape[1]) / 16),
----> 3                        anti_aliasing=True))    
      4 
      5 #     downSample.append(resize(i, i.shape[0]/4, i.shape[1]/4), anti_aliasing = True)

~/anaconda3/lib/python3.7/site-packages/skimage/transform/_warps.py in resize(image, output_shape, order, mode, cval, clip, preserve_range, anti_aliasing, anti_aliasing_sigma)
    167         out = warp(image, tform, output_shape=output_shape, order=order,
    168                    mode=mode, cval=cval, clip=clip,
--> 169                    preserve_range=preserve_range)
    170 
    171     else:  # n-dimensional interpolation

~/anaconda3/lib/python3.7/site-packages/skimage/transform/_warps.py in warp(image, inverse_map, map_args, output_shape, order, mode, cval, clip, preserve_range)
    805         output_shape = input_shape
    806     else:
--> 807         output_shape = safe_as_int(output_shape)
    808 
    809     warped = None

~/anaconda3/lib/python3.7/site-packages/skimage/_shared/utils.py in safe_as_int(val, atol)
    149     except AssertionError:
    150         raise ValueError("Integer argument required but received "
--> 151                          "{0}, check inputs.".format(val))
    152 
    153     return np.round(val).astype(np.int64)

ValueError: Integer argument required but received (134.25, 186.5, 3), check inputs.

标签: pythonimageopencv

解决方案


rgb_val可能包含非int类型对象。

请尝试以下操作:

downSample = []
for i in range(len(rgb_val)):
    downSample.append(resize(int(rgb_val[i]), (int(rgb_val[i].shape[0] / 16), int(rgb_val[i].shape[1] / 16)),
                       anti_aliasing=True))

推荐阅读