图像位深转换:64位->32位

43 篇文章 10 订阅
16 篇文章 1 订阅

问题描述

在深度学习的图像处理中遇到如下问题:
在这里插入图片描述

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=srcalpha+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=srcalpha将数据范围从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格式

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值