Rubyでimagemagick、好ましくはmini_magicを使って "エントロピー"を見つける方法は?私は大きなプロジェクトの一環としてこれを必要とします。画像に「面白さ」があり、それを切り抜くにはです。Rubyとimagemagickで画像のエントロピーを取得または計算する
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
これは式entropy = -sum(p.*log2(p))
に変換します:
私の質問:Django/Pythonコードを正しく解釈しましたか?どうすればルビのmini_magickでヒストグラムを取得できますか?
最も重要な質問:このアルゴリズムは最初に良いですか?あなたは画像の(部分)の "エントロピー"または "変化するピクセルの量"または "グラジエントの深さ"を見つけるのに良い方をお勧めしますか?
:a.o.を使用します。 IMAGE_DATAがRMagick::Image
ある
# 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
:以下の答えが提供するリソースは、私は、作業コードを思い付きました。
これは、smartcropper gemで使用されています。これは、スマートスライスや画像の切り抜きを可能にします。ペーパークリップ。
本当にありがとう。私のMatlabのスキルは少し錆びますが、これをRubyに変換して(そしてFreeMatで再生して)うまく管理しています。 – berkes