如何将图像从np.uint16转换为np.uint8? [英] How to convert an image from np.uint16 to np.uint8?

查看:285
本文介绍了如何将图像从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')

希望有帮助!

这篇关于如何将图像从np.uint16转换为np.uint8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆