2011-02-08 35 views
5

Ruby'de "entropy" ile imagemagick, tercihen mini_magic nasıl bulunur? Bunu daha büyük bir projenin bir parçası olarak görüyorum, Bir görüntüde "ilginçlik" bulmak için.Bir görüntüyü Ruby ya da imagemagick ile entropi edin ya da hesaplayın

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 << 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 << log2(pixel) 
    endif 
endfor 

#Calculate the total of the enhanced pixel-values and invert(?) that. 
entropy = entroy_list.sum * -1 

Bu formül entropy = -sum(p.*log2(p)) da uyarlanabilir:

Aşağıdaki sözde kodu verir iyi example in Python/Django bulundu.

Sorularım: Django/Python kodunu doğru mu söyledim? Ruby'nin mini_magick'inde histogramı nasıl alabilirim?

En önemli soru: bu algoritma ilk etapta herhangi bir iyi midir? Görüntülerin (kısımlarındaki) "entropi" veya "değişen piksel miktarı" veya "gradyan derinliği" ni bulmak için daha iyi bir öneri önerir misiniz?

Düzenle: a.o. Aşağıda cevabını tarafından sağlanan kaynaklar, ben çalışma kod ile geldi:

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

image_data bir RMagick::Image olduğu yere. Bu, örneğin, örn. Ile görüntülerin akıllı dilimlenmesini ve kırpılmasını sağlayan smartcropper gem numaralı belgede kullanılmaktadır. Ataç.

cevap

İlgili konular