How to convert an image from np.uint16 to np.uint8?

前端 未结 2 1673
慢半拍i
慢半拍i 2020-12-14 17:13

I am creating an image so:

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

After that I convert the image to BGR model:

2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-14 18:04

    You can use cv2.convertScaleAbs for this problem. See the Documentation.

    Check out the command terminal demo below :

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

    Hope it helps!!!

提交回复
热议问题