目前我有一张图片,我从中检索背景颜色。我正在使背景颜色透明,然后尝试将其与原始图像进行比较,以确定由所述背景颜色组成的图像的百分比。这是我试图使用的代码:
$image = new Imagick($file);
// Make bg transparent for comparison
$tpImage = $image->clone();
$tpImage->setFormat('png');
$tpImage->setImagePage(0, 0, 0, 0);
$swatch_pixel = $tpImage->getImagePixelColor(1, 1);
$tpImage->paintTransparentImage($swatch_pixel, 0, 65535 * 0.1);
$image->SetOption('fuzz', '2%');
$result = $image->compareImages($tpImage, Imagick::METRIC_ABSOLUTEERRORMETRIC);
echo $result[0];
目前这总是返回0 ...我不确定我是否需要使用不同的比较方法或者什么,或者如果我做错了什么,我花了很多时间在这上面和大部分时间我发现的资源已经很老了。
答案 0 :(得分:1)
更新了答案
实际上,现在你明确地指出了它,当唯一的区别是透明度时,ABSOLUTE_ERROR指标将不起作用。
要回答您的问题,您可能只想使用以下方法分离Alpha通道:
separateImageChannel(Imagick::CHANNEL_ALPHA);
然后得到它的统计平均值,它将告诉你像素的百分比是白色的 - 即透明的。
我将在下面留下原始答案,因为它是一种有用的技术,适用于任何其他不完全透明的图像差异。
原始答案
您可以在比较后获取图像属性,并在其中找到名为“失真”的元素。所以,如果我从这张图片开始:
然后向右滚动10个像素:
<?php
$image1 = new Imagick("image.png");
$image2 = new Imagick("image.png");
$result = $image1->compareImages($image2,Imagick::METRIC_MEANABSOLUTEERROR);
$p1=$image1->getImageProperties();
print_r($p1);
$image1->rollImage(10,0);
$result = $image1->compareImages($image2,Imagick::METRIC_MEANABSOLUTEERROR);
$p1=$image1->getImageProperties();
print_r($p1);
?>
<强>输出强>
Array
(
[date:create] => 2016-06-02T14:15:01+01:00
[date:modify] => 2016-06-02T14:15:01+01:00
[distortion] => 0 <--- Here's the little devil
[png:bKGD] => chunk was found (see Background color, above)
[png:cHRM] => chunk was found (see Chromaticity, above)
[png:gAMA] => gamma=0.45454544 (See Gamma, above)
[png:IHDR.bit-depth-orig] => 4
[png:IHDR.bit_depth] => 4
[png:IHDR.color-type-orig] => 3
[png:IHDR.color_type] => 3 (Indexed)
[png:IHDR.interlace_method] => 0 (Not interlaced)
[png:IHDR.width,height] => 200, 200
[png:PLTE.number_colors] => 5
[png:sRGB] => intent=0 (Perceptual Intent)
[png:text] => 2 tEXt/zTXt/iTXt chunks were found
[png:tIME] => 2016-06-02T14:15:01Z
)
Array
(
[date:create] => 2016-06-02T14:15:01+01:00
[date:modify] => 2016-06-02T14:15:01+01:00
[distortion] => 0.0833333 <--- Here's the little devil
[png:bKGD] => chunk was found (see Background color, above)
[png:cHRM] => chunk was found (see Chromaticity, above)
[png:gAMA] => gamma=0.45454544 (See Gamma, above)
[png:IHDR.bit-depth-orig] => 4
[png:IHDR.bit_depth] => 4
[png:IHDR.color-type-orig] => 3
[png:IHDR.color_type] => 3 (Indexed)
[png:IHDR.interlace_method] => 0 (Not interlaced)
[png:IHDR.width,height] => 200, 200
[png:PLTE.number_colors] => 5
[png:sRGB] => intent=0 (Perceptual Intent)
[png:text] => 2 tEXt/zTXt/iTXt chunks were found
[png:tIME] => 2016-06-02T14:15:01Z
)