I would use tf.unsorted_segment_sum , where the "segment identifiers" are calculated from the color values, and the amount you sum is equal to the tf.ones vector. Note that tf.unsorted_segment_sum is probably best understood as a βtotal amount." It implements dest[segment] += thing_to_sum - the operation necessary for the histogram.
In slightly pseudo-code (this means I did not run this):
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)
You can do this in one go instead of separating the values ββof r, g and b with a section if you want to skillfully build your βonesβ to look like β100100 ...β for red, β010010β, for green, etc. but I suspect it will be slower and harder to read. I would just do the split you suggested above.
source share