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

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

在这里插入图片描述

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值