我怎样才能在我的图像中找到最黑暗的像素,这在图像中是最大的?所以找到最黑暗的像素,我可以看到比其他像素更多。 在这里,我创建我的图像噪声,并用白色为它着色,但如何找到最黑的像素?我试图找到在RGB数组元素女巫发生是最大的,但所以我发现白色像素。这里是我的代码的一部分:如何使用php查找最暗的像素?
<?php
function components($color) {
return array(($color >> 16) & 0xFF, ($color >> 8) & 0xFF, $color & 0xFF);
}
// Performs "similarity test" of 2 colors
function isSimilar($color1, $color2) {
$c1 = components($color1);
$c2 = components($color2);
for ($i = 0; $i < 3; $i++) {
$k = ($c1[$i] > $c2[$i]) ? ($c1[$i] - $c2[$i])/$c2[$i] : ($c2[$i] - $c1[$i])/$c1[$i];
if ($k > 0.35) return false;
}
return true;
}
function LoadJpeg($imgname)
{
$count = 0;
/* Attempt to open */
$im = @imagecreatefrompng($imgname);
$imagedata = getimagesize($imgname);
$n = $imagedata[0];
$m = $imagedata[1];
for($i=0; $i<$imagedata[0]; $i++){
for($j=0; $j<$imagedata[1]; $j++){
$rgb[$i][$j] = imagecolorat($im, $i, $j);
//echo $rgb[$i][$j];
//echo "<br>";
}
}
/* for ($k = 0; $k < $n; $k++)
{
for ($l = 0; $l < $m; $l++)
{
for ($i = 0; $i < $n; $i++)
{
for ($j = 0; $j < $m; $j++)
{
if (($i+1 == $n) && ($j+1 == $m))
{
continue;
}
else
{
if ($j+1 == $m and $rgb[$i][$j] > $rgb[$i+1][0])
{
$t = $rgb[$i][$j];
$rgb[$i][$j] = $rgb[$i+1][0];
$rgb[$i+1][0] = $t;
}
else
{
if ($rgb[$i][$j] > $rgb[$i][$j+1])
{
$t = $rgb[$i][$j];
$rgb[$i][$j] = $rgb[$i][$j+1];
$rgb[$i][$j+1] = $t;
}
}
}
}
}
}
}*/
for($i=0; $i<$imagedata[0]-3; $i++){
for($j=0; $j<$imagedata[1]-3; $j++){
if (isSimilar($rgb[$i][$j], $rgb[$i][$j + 3]) or isSimilar($rgb[$i][$j], $rgb[$i + 3][$j]))
{
#echo "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAa";
$count = $count + 1;
//echo "<br> <br>";
//echo $count;
//$red = imagecolorallocate($im, 255, 255, 255);
imagesetpixel($im, $i, $j, 16777215);
cropCentered($im,20,20);
}
}
}
return $im;
}
function cropCentered($img, $w, $h)
{
$cx = $img->getWidth()/2;
$cy = $img->getHeight()/2;
$x = $cx - $w/2;
$y = $cy - $h/2;
if ($x < 0) $x = 0;
if ($y < 0) $y = 0;
return $img->crop(0, 0, $w, $h);
}
header('Content-Type: image/jpeg');
$img = LoadJpeg('1.png');
imagejpeg($img,null, 100);
?>
但如何algoritmize呢? – byCoder 2012-07-12 14:46:23
以及如何检查我是否比我更加黑暗+ 1 – byCoder 2012-07-12 14:46:42
你的意思是你如何在代码中编写?如果你的意思是说,这个论坛的重点,但我会给你一个总体想法 – maxhud 2012-07-12 14:48:50