Perl 如何轻松提取图像的颜色百分比?

Perl 如何轻松提取图像的颜色百分比?,perl,colors,imagemagick,extraction,Perl,Colors,Imagemagick,Extraction,我需要提取图像的颜色信息,以便以后能够根据颜色的百分比搜索图像。我想用这个。 有什么想法吗?ImageMagick有一个直方图方法,可以返回图像中的颜色列表以及它们出现的频率。它的文档记录很差,界面也很糟糕,但我在过去使用过它,因此我提供了一些有用的代码: my @hist_data = $image->Histogram; my @hist_entries; # Histogram returns data as a single list, but the list is actual

我需要提取图像的颜色信息,以便以后能够根据颜色的百分比搜索图像。我想用这个。
有什么想法吗?

ImageMagick有一个
直方图方法,可以返回图像中的颜色列表以及它们出现的频率。它的文档记录很差,界面也很糟糕,但我在过去使用过它,因此我提供了一些有用的代码:

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;
my@hist\u data=$image->Histogram;
我的@hist_条目;
#直方图以单个列表的形式返回数据,但该列表实际上是一组数据
#5个要素。将其转换为有用的哈希列表。
while(@hist_data){
my($r,$g,$b,$a,$count)=拼接@hist_数据,0,5;
推送@hist_条目{
r=>$r,
g=>g美元,
b=>b美元,
alpha=>$a,
count=>count美元,
};
}
#按降序排列颜色
@hist_entries=sort{$b->{count}$a->{count}@hist_entries;

然而,取决于你要做的事情,直方图并不像它对全彩图像那样有用,因为同一颜色会有很多细微不同的色调,而计数在直方图中被分割。一个有用的预处理步骤是在图像的克隆上调用
$image->Segment(colorspace=>'rgb')
,它会找到颜色相似的区域,并用其平均颜色替换整个区域。然后,当您调用
直方图时
您将看到较少颜色的较大计数,以及更具代表性的数据。

图像信息@squiguy:此模块不提取颜色值