2

我想在具有颜色名称或其十六进制代码的图像中获取唯一颜色的直方图。

我无法使用 QueryColorname 方法将直方图方法的输出值转换为颜色名称或十六进制代码;它总是返回黑色并且不返回十六进制代码。

这可能是由于 histogram() 方法的 (0 ... 65535) 结果范围,我无法将其转换为 (0 .. 255),这是 Querycolorname() 方法的可接受范围。

#!/usr/bin/perl
use Image::Magick;

$image=Image::Magick->new();
$image->ReadImage('Sun.jpeg'); 

my @histogram = $image->Histogram();
print "Red\tGreen\tBlue\tOpacity\tCount\tName\n";
for(my $i=0; $i<=29; $i++){ #Get 5 unique colors
   print "$histogram[$i]\t";
   if (($i+1)%5 == 0){ #Array elements of unique color
      my $name = $image->QueryColorname('rgb16($histogram[$i-4],$histogram[$i-3],$histogram[$i-    2],$histogram[$i-1])');
      print "$name\n";
   }
}

结果看起来像,

红色 绿色 蓝色 不透明度 计数 名称
0 0 0 0 16134 黑色
257 257 257 0 27 黑色
0 257 0 0 303 黑色
257 0 0 0 286 黑色
257 257 0 0 8 黑色
71 0 0 0 82 黑色

http://www.imagemagick.org/script/perl-magick.php上的方法描述

4

1 回答 1

0

首先:当您在变量周围使用单引号时,它们不会被扩展。QueryColorname看到一个可能转换为零的字符串。这就是为什么所有颜色都是“黑色”的。

第二:我在文档中没有看到rgb16,我想它不会做你想要的。相反,您必须缩小到 8 位

将两者放在一起,我为内部 if-Block 提出了这样的建议:

my $colVec = "rgb(";
$colVec .= int($histogram[$i-4]/65535*256) . ",";
$colVec .= int($histogram[$i-3]/65535*256) . ",";
$colVec .= int($histogram[$i-2]/65535*256) . ",";
$colVec .= $histogram[$i-1] . ")";
print $image->QueryColorname($colVec) . "\n";
于 2015-03-21T14:26:54.130 回答