0

在这里我尝试检查图像,但它有一些另一种颜色的“椭圆”,我尝试用我的颜色平滑它们。但是我的代码只搜索特定的颜色,怎么说他例如5%的差异是不好的呢?如何在我的像素附近找到所有相同的像素并将它们着色为另一种颜色?

<?php
function LoadJpeg($imgname)
{
    $count = 0;
    /* Attempt to open */
    $im = @imagecreatefrompng($imgname);
    $imagedata = getimagesize($imgname);


    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($i=0; $i<$imagedata[0]-5; $i++){
        for($j=0; $j<$imagedata[1]-5; $j++){
            if (($rgb[$i][$j] == $rgb[$i+3][$j]) || ($rgb[$i][$j] == $rgb[$i][$j+3]))
            {
                #echo "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAa";
                $count = $count + 1;
                //echo "<br> <br>";
                //echo $count;
                //$red = imagecolorallocate($im, 255, 255, 255);
                imagesetpixel($im, $i, $j, 13437229);
            }


        }
    }
    return $im;
}



header('Content-Type: image/jpeg');

$img = LoadJpeg('1.png');


// echo "Image width  is: " . $imagedata[0];
// echo "Image height is: " . $imagedata[1];

imagejpeg($img,null, 100);

?>

主要问题是strong ==,但是我的if case 必须捕捉到这种颜色的一些差异。

4

1 回答 1

1

What about calculating distance between each color component? If one of the components differ by more than 5 percents, return false:

// Returns RGB components of the color represented by an integer
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.05) return false;
  }
  return true;
}

// ...
if (isSimilar($rgb[$i][$j], $rgb[$i][$j + 3]) or isSimilar($rgb[$i][$j], $rgb[$i + 3][$j])) {
  // ...
}

The code may require additional testing and tweaking, but I think you've got an idea.

于 2012-07-07T17:04:35.777 回答