How can i find in my image the darkest pixel, which occurence in image is maximal? So to find darkest pixel which i can see more than other pixels.
Here i’m founding on my image noise, and color it with white color, but how to find darkest pixel? I try to find in rgb array element witch occurence is maximal, but so i found white pixel to. Here is part of my code:
<?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);
?>
You need to use a color model different than RGB – in this case HSL is useful, because it contains “Lightness” component. Based on this component we can judge whether some color is lighter or darker than another. There are several methods of converting RGB to HSL, I’ve used the one described in the Wikipedia article (see HSL “bi-hexcone” model):