如何将图像从np.uint16转换为np.uint8?

如何将图像从np.uint16转换为np.uint8?

问题描述:

我正在创建图像,所以:

I am creating an image so:

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

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

After that I convert the image to BGR model:

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

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

I'd like to convert the image now in a dtype = np.uint8 in order to use that image with cv2.threshold() function. I meant, I would like to convert the image to CV_8UC1.

您可以使用cv2.convertScaleAbs来解决此问题.请参见文档.

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')

希望有帮助!