如何将float32 img转换为uint8范围在哪里(-1至-0.85)?

发布于 2025-01-18 15:25:06 字数 255 浏览 4 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

影子的影子 2025-01-25 15:25:06

首先将数组归一化为0..1。

假设gen_samples是图像矩阵:

arr_min = np.min(gen_samples)
arr_max = np.max(gen_samples)
gen_samples = (gen_samples - arr_min) / (arr_max - arr_min)

Normalize the array to 0..1 first.

Assuming gen_samples is the image matrix:

arr_min = np.min(gen_samples)
arr_max = np.max(gen_samples)
gen_samples = (gen_samples - arr_min) / (arr_max - arr_min)
终止放荡 2025-01-25 15:25:06

由于您使用scikit-image标记了问题,因此您可能正在使用弹丸。在这种情况下代码> img_as_ubyte 应该做一个问题:

from skimage.util import image_as_ubyte

img = img_as_ubyte(gen_samples[0])

此外,由于您用image> imageio-python标记了问题格式而不是在脚本中生成。在这种情况下,您通常可以使用执行解码的后端并在加载图像时进行转换。但是,这是特定于所使用的格式的,因此,对于更具体的答案,您将不得不更深入了解图像的来源。

Since you tagged the question using scikit-image you are probably using skimage. In this case img_as_ubyte should do the trick:

from skimage.util import image_as_ubyte

img = img_as_ubyte(gen_samples[0])

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文