tensorflow对图像分通道进行标准化

本文介绍了一种使用TensorFlow对图像的RGB通道进行独立标准化的方法,通过将图像分离为单独的通道并应用tf.image.per_image_standardization,实现了每个通道的均值和标准差计算,最后展示了与整体通道标准化的效果对比。
部署运行你感兴趣的模型镜像
tensorflow自带的tf.image.per_image_standardization会在整个图像通道上计算均值标准差,进而标准化。这里实现分别在r g b通道上单独标准化。
import tensorflow as tf
import numpy as np
import cv2

origin = cv2.resize(cv2.imread('/home/shuai/Desktop/tool/lena.jpg'), (400, 400))

a = tf.constant(origin, dtype=tf.float32)

# separate channel
a = tf.unstack(a, axis=2)
res = []
for i in a:
    res.append(tf.squeeze(tf.image.per_image_standardization(tf.expand_dims(i, axis=2)), axis=2))
a = tf.stack(res, axis=2)

# all channel
b = tf.image.per_image_standardization(origin)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    a_, b_ = sess.run([a, b])

    # numpy 分通道标准化图像
    image = origin
    mean1, mean2, mean3 = np.mean(image[:, :, 0]), np.mean(image[:, :, 1]), np.mean(image[:, :, 2])
    std1, std2, std3 = np.std(image[:, :, 0]), np.std(image[:, :, 1]), np.std(image[:, :, 2])
    i1 = (image[:, :, 0] - mean1) / std1
    i2 = (image[:, :, 1] - mean2) / std2
    i3 = (image[:, :, 2] - mean3) / std3
    image = np.stack([i1, i2, i3], axis=2)

    cv2.imshow('tf each channel', a_)
    cv2.imshow('tf all channel', b_)
    cv2.imshow('numpy', image)
    cv2.imshow('origin', origin)

    cv2.waitKey(-1)
    cv2.destroyAllWindows()

demo

在这里插入图片描述

您可能感兴趣的与本文相关的镜像

TensorFlow-v2.15

TensorFlow-v2.15

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值