如何为在[0,1]和[0,255]范围内归一化的图像添加图像保存功能?

huangapple go评论54阅读模式
英文:

How to have Image-Saving Function for Images Normalized with [0,1] and [0,255]?

问题

我有一组图像,其中一些在[0,1]范围内进行了归一化,而另一些在[0,255]范围内进行了归一化。我不知道输入图像是以[0,1]形式还是另一种形式进行归一化。这就是为什么我试图编写一个支持两种情况的函数。

换句话说,我想创建一个函数,可以同时支持这两种情况,我可以将图像提供给函数,然后将其保存在相应的范围内。有谁知道如何做到这一点?

英文:

I have a collection of images, with some being normalized within the range of [0,1], and others within the range of [0,255]. I don't know about when input images is normalized in [0,1] form and when the other form. That's why I'm trying to write a function that support both.

In other words, I would like to create a function that can support both cases, where I can provide an image to the function and have it saved accordingly. Does anyone know how to do so?

答案1

得分: 1

我不确定这是否回答了你的问题,但这是我考虑要做的事情。你可以检查图像的范围并将它们转换为标准形式。这是我的意思:

min_val = np.min(image)
max_val = np.max(image)

if min_val >= 0 and max_val <= 1:
        # 图像已标准化在[0, 1]范围内
        image = (image * 255).astype(np.uint8)
    elif min_val >= 0 and max_val <= 255:
        # 图像已在[0, 255]范围内
        image = image.astype(np.uint8)
    else:
        # 错误

希望对你有所帮助。

英文:

I am not sure if this answers your question but it is what I thought of doing. You can check the range of image and convert them to a standard form. This is what I mean:

min_val = np.min(image)
max_val = np.max(image)

if min_val &gt;= 0 and max_val &lt;= 1:
        # Image normalized in [0, 1] range
        image = (image * 255).astype(np.uint8)
    elif min_val &gt;= 0 and max_val &lt;= 255:
        # Image already in [0, 255] range
        image = image.astype(np.uint8)
    else:
        # Error 

Hope this helps.

huangapple
  • 本文由 发表于 2023年7月3日 19:04:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76604142.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定