【问题标题】:How to convert an image from np.uint16 to np.uint8?如何将图像从 np.uint16 转换为 np.uint8?
【发布时间】:2012-07-05 10:54:13
【问题描述】:

我正在创建一个图像:

image = np.empty(shape=(height, width, 1), dtype = np.uint16)

之后我将图像转换为 BGR 模型:

image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)

我想现在将图像转换为dtype = np.uint8,以便将该图像与cv2.threshold() 函数一起使用。我的意思是,我想将图像转换为CV_8UC1

【问题讨论】:

    标签: python opencv numpy


    【解决方案1】:

    您可以使用cv2.convertScaleAbs 解决此问题。见Documentation.

    查看下面的命令终端演示:

    >>> img = np.empty((100,100,1),dtype = np.uint16)
    >>> image = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
    
    >>> cvuint8 = cv2.convertScaleAbs(image)
    
    >>> cvuint8.dtype
    dtype('uint8')
    

    希望对你有帮助!!!

    【讨论】:

      【解决方案2】:

      我建议你使用这个:

      outputImg8U = cv2.convertScaleAbs(inputImg16U, alpha=(255.0/65535.0))
      

      这将输出一个 uint8 图像并相对于 0-65535 之间的先前值分配 0-255 之间的值

      exemple :
      pixel with value == 65535 will output with value 255 
      pixel with value == 1300 will output with value 5 etc...
      

      【讨论】:

        猜你喜欢
        • 2021-01-26
        • 2011-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-04
        • 2016-05-29
        • 2011-08-15
        相关资源
        最近更新 更多