我如何使用PHP和/或imagemagick来检测图像是空白的(只有一种、任意的颜色,或者,对于gif,是随机的任意颜色的帧)?
我想这就是我要尝试的:http://www.php.net/manual/en/function.imagecolorat.php#97957
发布于 2014-08-06 20:59:58
你可以使用imagecolorat检查PHP内部的图像(这可能很慢,但它是有效的):
function isPngValidButBlank($filename) {
$img = imagecreatefrompng($filename);
if(!$img)
return false;
$width = imagesx($img);
$height = imagesy($img);
if(!$width || !$height)
return false;
$firstcolor = imagecolorat($img, 0, 0);
for($i = 0; $i < $width; $i++) {
for($j = 0; $j < $height; $j++) {
$color = imagecolorat($img, $i, $j);
if($color != $firstcolor)
return false;
}
}
return true;
}发布于 2011-05-07 03:24:19
http://www.php.net/manual/en/function.imagecolorstotal.php为您提供图像中的颜色数量。嗯,在我的演示中,它似乎不起作用,对不起:(我创建的一个图像(全红,20x20像素)为PNG提供0种颜色,为GIF提供3种颜色。
好的:http://www.dynamicdrive.com/forums/showpost.php?p=161187&postcount=2看第二段代码。在这里测试:http://www.pendemo.nl/totalcolors.php
发布于 2014-08-06 23:57:02
Kevin的解决方案可以使用随机采样来加速。如果您对应与背景不同的像素百分比有一定了解(假设您处理的不是大量只有1个不同像素的图像),则可以使用泊松分布:
找到非空白像素的概率=1- e^(-n*p)
其中n是要尝试的样本数,p是预期为非空白的像素的百分比。Solve for n以获取适当数量的样本进行尝试:
N= -log(1 - x) /p
其中x是期望概率,log是自然对数。例如,如果您合理地确定图像的0.1%应该是非空白的,并且您希望有99.99%的机会找到至少一个非空白像素,
N= -log(1-.9999)/.001 =需要9210个样本。
比检查每一个像素要快得多。为了100%确定,如果采样没有找到,您可以随时返回并检查所有它们。
https://stackoverflow.com/questions/5915993
复制相似问题