Scipy生成16位的深度图

这篇博客介绍了如何使用numpy和scipy库生成一个16位深度图,并将其保存为PNG文件。通过scipy.misc.toimage函数,可以将随机生成的整数数组转换为16位图像,同时确保数据范围正确,避免值溢出。保存的PNG图像能够准确还原原始数据,并在读取时保持数据类型为int32。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

生成16位的深度图(点云投影)

With scipy.misc.image there seems to be no option to save as 16-bit, so it will always write an 8-bit PNG. But you can use scipy.misc.toimage to create a 16-bit image, just be sure to pass mode=‘I’. Also be sure to specify the array min and max to avoid scaling. Here’s how to use it to save a 16-bit png:

import numpy as np
import scipy.misc
a = np.random.uniform(0, 2**16 - 1, (500, 500)).astype('int32')
img = scipy.misc.toimage(a, high=np.max(a), low=np.min(a), mode='I')
img.save('my16bit.png')
``

```python
# check that you got the same values
b = scipy.misc.imread('my16bit.png')
b.dtype
# dtype('int32')
np.array_equal(a, b)
# True

Note that in this example I used int32 for data type. However, the data must still fit in a uint16. If you put negative values or values larger than 2^16, those will be clipped in the save to PNG. Conversely, even though sp.misc.imread reads as int32, the data will never be more than uint16.

In summary: if you want to write exactly the same numpy array to a PNG you need to make sure it is of uint8/uint16 type, and that you pass the correct high/low/mode to scipy.misc.toimage.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值