2012-08-28 91 views

回答

7

ImageMagick有一個Histogram方法,該方法返回圖像中的顏色列表以及它們出現的頻率。它記錄不完整,有一個糟糕的界面,但我已經在過去使用過,所以我有一些有用的代碼提供:

my @hist_data = $image->Histogram; 
my @hist_entries; 
# Histogram returns data as a single list, but the list is actually groups of 
# 5 elements. Turn it into a list of useful hashes. 
while (@hist_data) { 
    my ($r, $g, $b, $a, $count) = splice @hist_data, 0, 5; 
    push @hist_entries, { 
     r => $r, 
     g => $g, 
     b => $b, 
     alpha => $a, 
     count => $count, 
    }; 
} 
# Sort the colors in decreasing order 
@hist_entries = sort { $b->{count} <=> $a->{count} } @hist_entries; 

但是,這取決於你想做什麼,直方圖ISN」 t和全色圖像一樣有用,因爲同一種顏色會有很多稍微不同的陰影,並且在直方圖中它們會被分割。一個有用的預處理步驟是在圖像的克隆上調用$image->Segment(colorspace => 'rgb'),該克隆找到具有相似顏色的區域並用其平均顏色替換整個區域。然後當您撥打Histogram時,您會看到較少顏色的較大計數,以及更具代表性的數據。

+0

+1感謝這個有幫助的代碼片段+描述 – Thariama