2012-07-12 19 views
3

我怎样才能在我的图像中找到最黑暗的像素,这在图像中是最大的?所以找到最黑暗的像素,我可以看到比其他像素更多。 在这里,我创建我的图像噪声,并用白色为它着色,但如何找到最黑的像素?我试图找到在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); 

    ?> 

回答

0

您需要使用与RGB不同的颜色模型 - 在这种情况下,HSL很有用,因为它包含“亮度”组件。基于这个组件,我们可以判断某种颜色是否比其他颜色更亮或更暗。有RGB转换为HSL的几种方法,我用在Wikipedia article(见HSL“双hexcone”模式)所描述的:

<?php 

// Returns whether c2 is darker than c1 
function isDarker($c1, $c2) { 
    // Find L (lightness) component in a HSL model 
    $max1 = max($c1); 
    $min1 = min($c1); 
    $l1 = ($max1 + $min1)/2; 
    $max2 = max($c2); 
    $min2 = min($c2); 
    $l2 = ($max2 + $min2)/2; 

    // Compare colors based on L component (lower means darker) 
    return $l2 < $l1; 
} 

// Assuming colors are stored in a 2-dimensional $rgb array 
$rowCount = count($rgb); 
$colCount = count($rgb[0]); 
$darkest = $rgb[0][0]; 
for ($i = 0; $i < $rowCount; $i++) { 
    for ($j = 0; $j < $colCount; $j++) { 
    if (isDarker($darkest, $rgb[$i][$j])) { 
     $darkest = $rgb[$i][$j]; 
     // Optionally you can save the indices $i and $j 
    } 
    } 
} 
4

你想要做的是设置一个变量,将存储最黑暗的颜色。

然后,如果该像素比最暗的颜色(存储在该最暗的颜色变量中)颜色暗,则可以遍历每个像素测试。

如果像素较暗,则将其设置为等于最暗的颜色。

这样,最黑的颜色只会/总是被更深的颜色覆盖(如果有的话)。

$pixels = array(/*put colors that correspond to pixels here*/) 
$darkest = $pixels[0]; 
for($i=0; $i<$count($pixels); $i++){ 
    if($pixels[$i] is darker than $darkest){ //you will have to figure out how to do that part 
     $darkest = $pixels[$i]; 
    } 
} 
+0

但如何algoritmize呢? – byCoder 2012-07-12 14:46:23

+0

以及如何检查我是否比我更加黑暗+ 1 – byCoder 2012-07-12 14:46:42

+0

你的意思是你如何在代码中编写?如果你的意思是说,这个论坛的重点,但我会给你一个总体想法 – maxhud 2012-07-12 14:48:50