Creating a color histogram of an image using tensor flow

Is there an easy way to calculate the histogram of the color of an image? Maybe breaking the tf.histogram_summary internal code? From what I saw, this code is not very modular and directly calls some C ++ code.

Thanks in advance.

+5
source share
3 answers

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.

+6
source

Here is what I am using now:

 # 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) 
+2
source
 tf.histogram_fixed_width 

maybe what you are looking for ...

Full documentation for

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

+1
source

Source: https://habr.com/ru/post/1237558/


All Articles