gpt4 book ai didi

php - 如何使用 php 查找最暗的像素?

转载 作者:可可西里 更新时间:2023-11-01 01:05:42 27 4
gpt4 key购买 nike

如何在我的图像中找到最暗的像素,即图像中出现次数最多的像素?所以要找到比其他像素更能看到的最暗像素。在这里,我建立在我的图像噪声上,并用白色着色,但如何找到最暗的像素?我试图在 rgb 数组元素中找到出现次数最多的女巫,但我发现白色像素为。这是我的部分代码:

<?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);

?>

最佳答案

您要做的是设置一个变量来存储最暗的颜色。

然后循环遍历每个像素,测试该像素是否比最暗的颜色(存储在最暗的颜色变量中)更暗。

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

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

$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];
}
}

关于php - 如何使用 php 查找最暗的像素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11453916/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com