问题描述
在深度学习的图像处理中遇到如下问题:
TypeError: can't convert np.ndarray of type numpy.uint16. The only supported types are: float64, float32, float16, complex64, complex128, int64, int32, int16, int8, uint8, and bool.
问题原因
问题出现,是因为 改变了图像的读取参数。之前输入的图像是通过cv2.imread(img_path)
来读取的,默认读取的是彩色图像,默认情况下读入的数据类型是uint8
。因为需要把图像读取方式转换为cv2.imread(img_path, cv2.IMREAD_UNCHANGED)
后就出现该问题。
查看读取图像:
用image.dtype
输出图像数据的数据类型:无问题图像的数据类型为uint8
,有问题图像的数据类型为uint16
。
解决方法
if image.dtype == 'uint16':
image = cv2.convertScaleAbs(image, alpha=(255.0/65535.0))
print(image.dtype)
convertScaleAbs(src, dst=None, alpha=None, beta=None)
通过公式
d
s
t
=
s
r
c
∗
a
l
p
h
a
+
b
e
t
a
dst = src * alpha + beta
dst=src∗alpha+beta将src的数据范围转换为dst的数据范围。例如src为uint16
那么对应的取值范围是0~65535
,dst为uint8
那么对应的取值范围是0~255
。要把uint16
转换为uint8
,只要设置参数alpha = 255.0/65535.0
,通过公式
d
s
t
=
s
r
c
∗
a
l
p
h
a
dst = src * alpha
dst=src∗alpha将数据范围从0~65535
转换为0~255
。
def convertScaleAbs(src, dst=None, alpha=None, beta=None): # real signature unknown; restored from __doc__
"""
convertScaleAbs(src[, dst[, alpha[, beta]]]) -> dst
. @brief Scales, calculates absolute values, and converts the result to 8-bit.
缩放,计算绝对值,然后将结果转换为8位。
.
. On each element of the input array, the function convertScaleAbs
. performs three operations sequentially: scaling, taking an absolute
. value, conversion to an unsigned 8-bit type:
在输入数组的每个元素上,函数convertScaleAbs依次执行三个操作:缩放,获取绝对值,转换为无符号的8位类型:
. \f[\texttt{dst} (I)= \texttt{saturate\_cast<uchar>} (| \texttt{src} (I)* \texttt{alpha} + \texttt{beta} |)\f]
. In case of multi-channel arrays, the function processes each channel
. independently. When the output is not 8-bit, the operation can be
. emulated by calling the Mat::convertTo method (or by using matrix
. expressions) and then by calculating an absolute value of the result.
如果是多通道阵列,该函数将独立处理每个通道。
当输出不是8位时,可以通过调用Mat :: convertTo方法(或使用矩阵表达式),
然后通过计算结果的绝对值来模拟该操作。
. For example:
. @code{.cpp}
. Mat_<float> A(30,30);
. randu(A, Scalar(-100), Scalar(100));
. Mat_<float> B = A*5 + 3;
. B = abs(B);
. // Mat_<float> B = abs(A*5+3) will also do the job,
. // but it will allocate a temporary matrix
. @endcode
. @param src input array. 输入数组。
. @param dst output array. 输出数组。
. @param alpha optional scale factor. 可选比例因子。
. @param beta optional delta added to the scaled values. 可选增量添加到缩放值。
. @sa Mat::convertTo, cv::abs(const Mat&)
"""
pass
参考资料
python opencv 4.1.0 cv2.convertScaleAbs()函数 (通过线性变换将数据转换成8位[uint8])(用于Intel Realsense D435显示depth图像)
如何将图像从np.uint16转换为np.uint8?
python处理图像转化为uint8格式