简体   繁体   中英

How to compute the mean for each channel in an image in tensorflow

What is the proper way to compute the mean for each channel in an image in tensorflow?

Any help is much appreciated!!

Just use tf.reduce_mean() and specify the axis:

axis: The dimensions to reduce. If None (the default), reduces all dimensions.

A simple way to do it is using tf.nn.moments

def mean(x):
    mm,_=tf.nn.moments(x,axes=[0])
    return mm

where axes=[0, 1, 2]

For a image tensor of shape [W,H,3], what worked for me was a derivative of @Rubens_Z answer

def mean(x):
    mm,_=tf.nn.moments(x,axes=[0,1])
    return mm

This will return a [1x3] mean value tensor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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