gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 20:33:03 27 4
gpt4 key购买 nike

我正在创建一个图像:

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

最佳答案

您可以使用 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')

希望对你有帮助!!!

关于python - 如何将图像从 np.uint16 转换为 np.uint8?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11337499/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com