如何将float32 img转换为uint8范围在哪里(-1至-0.85)?
我想在 Python 中将 float32
图像转换为 uint8
图像。 我尝试使用以下代码,但输出图像只有 2 和 3 这样的值,因此图像实际上是黑色的。
gen_samples[0] * 255).round().astype(np.uint8)
当我尝试显示 float32
图像时,我得到一个黑色/灰色图像,我可以在某种程度上辨认出所需的图像。
I want to convert a float32
image into uint8
image in Python.
I tried using the following code, but the output image only has values like 2 and 3 so the image is practically black.
gen_samples[0] * 255).round().astype(np.uint8)
When I try displaying the float32
image I get a blackish/greyish image where I can somewhat make out the required image.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先将数组归一化为0..1。
假设
gen_samples
是图像矩阵:Normalize the array to 0..1 first.
Assuming
gen_samples
is the image matrix:由于您使用
scikit-image
标记了问题,因此您可能正在使用弹丸。在这种情况下代码> img_as_ubyte 应该做一个问题:此外,由于您用
image> imageio-python标记了问题格式而不是在脚本中生成。在这种情况下,您通常可以使用执行解码的后端并在加载图像时进行转换。但是,这是特定于所使用的格式的,因此,对于更具体的答案,您将不得不更深入了解图像的来源。
Since you tagged the question using
scikit-image
you are probably using skimage. In this caseimg_as_ubyte
should do the trick:Further, since you tagged the question with
imageio-python
I'll assume that the image data actually comes from some (binary) image format rather than being generated in the script. In this case, you can often use the backend that does the decoding and do the conversion while the image is being loaded. This is, however, specific to the format being used, so for a more specific answer you would have to provide more insight into where your images are coming from.