Note that there are some explanatory texts on larger screens.

plurals
  1. POGet, or calculate the entropy of an image with Ruby and imagemagick
    primarykey
    data
    text
    <p>How to find the "entropy" with imagemagick, preferably mini_magic, in Ruby? I need this as part of a larger project, <em>finding "interestingness" in an image so to crop it</em>.</p> <p>I found a good <a href="https://stackoverflow.com/questions/1516736/django-sorl-thumbnail-crop-picture-head">example in Python/Django</a>, which gives the following pseudo-code:</p> <pre><code>image = Image.open('example.png') histogram = image.histogram() # Fetch a list of pixel counts, one for each pixel value in the source image #Normalize, or average the result. for each histogram as pixel histogram_recalc &lt;&lt; pixel / histogram.size endfor #Place the pixels on a logarithmic scale, to enhance the result. for each histogram_recalc as pixel if pixel != 0 entropy_list &lt;&lt; log2(pixel) endif endfor #Calculate the total of the enhanced pixel-values and invert(?) that. entropy = entroy_list.sum * -1 </code></pre> <p>This would translate to the formula <code>entropy = -sum(p.*log2(p))</code>. </p> <p>My questions: Did I interprete the Django/Python code correct? How can I fetch a histogram in ruby's mini_magick if at all? </p> <p>Most important question: is this algorithm any good in the first place? Would you suggest a better one to find the "entropy" or "amount of changing pixels" or "gradient depth" in (parts of) images? </p> <p><strong>Edit</strong>: Using a.o. the resources provided by the answer below, I came up with the working code:</p> <pre><code># Compute the entropy of an image slice. def entropy_slice(image_data, x, y, width, height) slice = image_data.crop(x, y, width, height) entropy = entropy(slice) end # Compute the entropy of an image, defined as -sum(p.*log2(p)). # Note: instead of log2, only available in ruby &gt; 1.9, we use # log(p)/log(2). which has the same effect. def entropy(image_slice) hist = image_slice.color_histogram hist_size = hist.values.inject{|sum,x| sum ? sum + x : x }.to_f entropy = 0 hist.values.each do |h| p = h.to_f / hist_size entropy += (p * (Math.log(p)/Math.log(2))) if p != 0 end return entropy * -1 end </code></pre> <p>Where image_data is an <code>RMagick::Image</code>. </p> <p>This is used in the <a href="http://smartcropper.berk.es/" rel="nofollow noreferrer">smartcropper gem</a>, which allows smart slicing and cropping for images with e.g. paperclip.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload