图像强度归一化Intensity normalization

本文介绍了如何使用OpenCV和NumPy进行图像归一化处理,包括将图像从0到255的范围转换到0到1之间,以及线性归一化的方法。通过示例代码展示了如何读取图像,进行数据类型转换,并应用归一化算法。

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

#因为opencv读入的图片矩阵数值是0到255,有时我们需要对其进行归一化为0~1
'''
import cv2
img3 = cv2.imread('me.png')
img3 = img3.astype("float") / 255.0  #注意需要先转化数据类型为float
cv2.imshow("Image",img3)
cv2.waitKey()
print(img3.dtype)
print(img3)
'''
import numpy as np
from PIL import Image

def normalize(arr):
    """
    Linear normalization
    http://en.wikipedia.org/wiki/Normalization_%28image_processing%29
    """
    arr = arr.astype('float')
    # Do not touch the alpha channel
    for i in range(3):
        minval = arr[...,i].min()
        maxval = arr[...,i].max()
        if minval != maxval:
            arr[...,i] -= minval
            arr[...,i] *= (255.0/(maxval-minval))
    return arr

def demo_normalize():
    img = Image.open('happy1.png').convert('RGBA')
    arr = np.array(img)
    new_img = Image.fromarray(normalize(arr).astype('uint8'),'RGBA')
    new_img.save('./normalized.png')
demo_normalize()	

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值