简体   繁体   English

如何标准化图像中的像素值并保存

[英]How to normalize pixel values in an image and save it

I am trying to normalize my data to prepare it as input for this model.我正在尝试规范化我的数据以准备将其作为model 的输入。 I tried following this guide, but have been having issues.我尝试按照指南进行操作,但一直遇到问题。

First off, my min and max values aren't starting off as 0 and 255, and the final results are not normalized between 0 and 1.首先,我的最小值和最大值不是从 0 到 255 开始的,最终的结果也不是在 0 到 1 之间标准化。

Here is my function这是我的 function

def process_image(image_path):
    image = Image.open(image_path)
    new_image = image.resize((224,224))
    
    pixels = asarray(new_image)

    # confirm pixel range is 0-255
    print('Data Type: %s' % pixels.dtype)
    print('Min: %.3f, Max: %.3f' % (pixels.min(), pixels.max()))

    # convert from integers to floats
    pixels = pixels.astype('float32')

    # normalize to the range 0-1
    pixels /= 255.0

    # confirm the normalization
    print('Min: %.3f, Max: %.3f' % (pixels.min(), pixels.max()))
    new_image.save("result.jpg")
    
    return new_image

and here is my resulting output这是我得到的 output

Data Type: uint8
Min: 8.000, Max: 216.000
Min: 0.031, Max: 0.847

Any ideas why?任何想法为什么? And also, how can I save the image with these normalized results.而且,如何使用这些标准化结果保存图像。 As the code is written now, the pixels aren't being changed because I am only creating a copy of the pixels from new_image.由于现在编写代码,像素没有被更改,因为我只是从 new_image 创建像素的副本。

Thanks for any help that can be provided.感谢您提供的任何帮助。

UPDATED CODE更新代码

Thanks to @relh for the help!感谢@relh 的帮助! I implemented his code and it runs great now我实现了他的代码,现在运行良好

The code编码

def process_image(image_path):
    min = sys.maxsize
    max = -sys.maxsize


    image = Image.open(image_path)
    new_image = image.resize((224,224))    
    np_image = asarray(image)
    if min > np_image.min():
        min = np_image.min()
    if max < np_image.max():
        max = np_image.max()    

    np_image = np_image.astype('float32')
    print("BEGINNING PIXEL VALUES", np_image)
    print('Data Type: %s' % np_image.dtype)
    print('Min: %.3f, Max: %.3f' % (np_image.min(), np_image.max()))
    np_image -= min
    np_image /= (max - min)

    print('Min: %.3f, Max: %.3f' % (np_image.min(), np_image.max()))
    print(np_image)

Output snippet Output 片段

Min: 2.000, Max: 223.000
Min: 0.000, Max: 1.000

[[0.22171946 0.4162896  0.37104073]
  [0.23076923 0.42533937 0.3846154 ]
  [0.18099548 0.37556562 0.33484164]
  ...
  [0.12217195 0.51583713 0.47511312]
  [0.15837105 0.54751134 0.50678736]
  [0.16742082 0.5565611  0.51583713]]]

What you can do is calculate the real minimum and maximum values for your dataset, before doing your own minmax normalization.您可以做的是在进行自己的 minmax 归一化之前计算数据集的实际最小值和最大值。

Here's what that might look like:这可能是这样的:

import sys
from PIL import Image
import numpy as np

image_paths = ['image_path1.jpg', 'image_path2.jpg', 'image_path3.jpg']
min = sys.maxsize
max = -sys.maxsize

for image_path in image_paths:
    image = Image.open(image_path)
    np_image = np.asarray(image)
    if min > np_image.min()
        min = np_image.min()
    if max < np_image.max()
        max = np_image.max()

This will give you the variables min and max and you can now use them to normalize between 0 and 1 with this instead of the /= 255 you had!这将为您提供变量 min 和 max,您现在可以使用它们在 0 和 1 之间进行标准化,而不是 /= 255!

    ...
    pixels -= min
    pixels /= (max - min)
    ...

Let me know if that helps!让我知道这是否有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM