【问题标题】:Create color histogram of an image using tensorflow使用 tensorflow 创建图像的颜色直方图
【发布时间】:2015-12-07 10:04:48
【问题描述】:

有没有一种简洁的方法来计算图像的颜色直方图?也许是通过滥用tf.histogram_summary的内部代码?就我所见,这段代码不是很模块化,直接调用了一些C++代码。

提前致谢。

【问题讨论】:

  • 你想在以后的TF计算中使用直方图,还是输出直方图作为最终目标?
  • @dga 我在后面的 TF 计算中使用它。

标签: tensorflow


【解决方案1】:

我会使用tf.unsorted_segment_sum,其中“段 ID”是根据颜色值计算得出的,而您求和的是一个 tf.ones 向量。请注意,tf.unsorted_segment_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)

如果您想巧妙地将“1”构造成“100100...”代表红色,“010010”,则可以一次完成此操作,而不是通过拆分将 r、g 和 b 值分开用于绿色等,但我怀疑它总体上会更慢,更难阅读。我会按照你上面建议的拆分来做。

【讨论】:

    【解决方案2】:

    这是我现在正在使用的:

    # 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)
    

    【讨论】:

      【解决方案3】:
      tf.histogram_fixed_width 
      

      可能是您正在寻找的...

      完整的文档

      https://www.tensorflow.org/api_docs/python/tf/histogram_fixed_width

      【讨论】:

        猜你喜欢
        • 2012-08-22
        • 1970-01-01
        • 1970-01-01
        • 2012-01-02
        • 1970-01-01
        • 2012-07-21
        • 2020-05-31
        • 1970-01-01
        • 2011-02-06
        相关资源
        最近更新 更多