使用张量流创建图像的颜色直方图
Create color histogram of an image using tensorflow
有没有一种计算图像颜色直方图的巧妙方法?也许通过滥用 tf.histogram_summary
的内部代码?据我所知,这段代码不是很模块化,直接调用了一些 C++ 代码。
提前致谢。
这是我现在正在使用的:
# Assumption: img is a tensor of the size [img_width, img_height, 3], normalized to the range [-1, 1].
with tf.variable_scope('color_hist_producer') as scope:
bin_size = 0.2
hist_entries = []
# Split image into single channels
img_r, img_g, img_b = tf.split(2, 3, img)
for img_chan in [img_r, img_g, img_b]:
for idx, i in enumerate(np.arange(-1, 1, bin_size)):
gt = tf.greater(img_chan, i)
leq = tf.less_equal(img_chan, i + bin_size)
# Put together with logical_and, cast to float and sum up entries -> gives count for current bin.
hist_entries.append(tf.reduce_sum(tf.cast(tf.logical_and(gt, leq), tf.float32)))
# Pack scalars together to a tensor, then normalize histogram.
hist = tf.nn.l2_normalize(tf.pack(hist_entries), 0)
我会使用 tf.unsorted_segment_sum
,其中 "segment IDs" 是根据颜色值计算得出的,您求和的结果是一个 tf.ones
向量。请注意,tf.unsorted_segment_sum
可能更好地理解为 "bucket sum"。它实现了 dest[segment] += thing_to_sum
—— 正是您需要的直方图操作。
稍微伪代码(意思是我没有运行这个):
binned_values = tf.reshape(tf.floor(img_r * (NUM_BINS-1)), [-1])
binned_values = tf.cast(binned_values, tf.int32)
ones = tf.ones_like(binned_values, dtype=tf.int32)
counts = tf.unsorted_segment_sum(ones, binned_values, NUM_BINS)
如果您想巧妙地将 "ones" 构造为看起来像红色的“100100...”,则可以一次性完成此操作,而不是通过拆分来分离 r、g 和 b 值,“010010”表示绿色等,但我怀疑它总体上会更慢,并且更难阅读。我只会按照您上面建议的方式进行拆分。
tf.histogram_fixed_width
可能就是您要找的...
有关
的完整文档
https://www.tensorflow.org/api_docs/python/tf/histogram_fixed_width
有没有一种计算图像颜色直方图的巧妙方法?也许通过滥用 tf.histogram_summary
的内部代码?据我所知,这段代码不是很模块化,直接调用了一些 C++ 代码。
提前致谢。
这是我现在正在使用的:
# Assumption: img is a tensor of the size [img_width, img_height, 3], normalized to the range [-1, 1].
with tf.variable_scope('color_hist_producer') as scope:
bin_size = 0.2
hist_entries = []
# Split image into single channels
img_r, img_g, img_b = tf.split(2, 3, img)
for img_chan in [img_r, img_g, img_b]:
for idx, i in enumerate(np.arange(-1, 1, bin_size)):
gt = tf.greater(img_chan, i)
leq = tf.less_equal(img_chan, i + bin_size)
# Put together with logical_and, cast to float and sum up entries -> gives count for current bin.
hist_entries.append(tf.reduce_sum(tf.cast(tf.logical_and(gt, leq), tf.float32)))
# Pack scalars together to a tensor, then normalize histogram.
hist = tf.nn.l2_normalize(tf.pack(hist_entries), 0)
我会使用 tf.unsorted_segment_sum
,其中 "segment IDs" 是根据颜色值计算得出的,您求和的结果是一个 tf.ones
向量。请注意,tf.unsorted_segment_sum
可能更好地理解为 "bucket sum"。它实现了 dest[segment] += thing_to_sum
—— 正是您需要的直方图操作。
稍微伪代码(意思是我没有运行这个):
binned_values = tf.reshape(tf.floor(img_r * (NUM_BINS-1)), [-1])
binned_values = tf.cast(binned_values, tf.int32)
ones = tf.ones_like(binned_values, dtype=tf.int32)
counts = tf.unsorted_segment_sum(ones, binned_values, NUM_BINS)
如果您想巧妙地将 "ones" 构造为看起来像红色的“100100...”,则可以一次性完成此操作,而不是通过拆分来分离 r、g 和 b 值,“010010”表示绿色等,但我怀疑它总体上会更慢,并且更难阅读。我只会按照您上面建议的方式进行拆分。
tf.histogram_fixed_width
可能就是您要找的...
有关
的完整文档https://www.tensorflow.org/api_docs/python/tf/histogram_fixed_width