gpt4 book ai didi

php - 如何修复我的氡转化?

转载 作者:可可西里 更新时间:2023-11-01 13:30:35 24 4
gpt4 key购买 nike

我在 this article 之后构建氡转化在 PHP 中。

但我的输出不是预期的结果。

输入:

预期结果:

实际结果:

...

我故意使用 RGB 而不是灰度,因为我想使用这种方法进行图像指纹识别。最后, channel 的数量应该不是很重要,对吧?


现在是编写代码的时候了。

主要功能:
这是主要功能,做了很多实际工作:

function RadonTransform($filename)
{
$i = imagecreatefromjpeg($filename);
$size = getimagesize($filename);
$center = new Vector2($size[0] / 2, $size[1] / 2);

$d = min(array($size[0], $size[1]));
$u2 = round(M_PI * ($d / 2.0));

$r = imagecreatetruecolor($u2, $d);

for ($z = 0; $z < $u2; $z++)
{
$w2 = M_PI * ($z / $u2);
$w4 = M_PI / 2.0;
$c1 = new Vector2(cos($w2), sin($w2)); $c1->Multiply($d / 2.0)->Add($center);
$c2 = new Vector2(cos($w2 + M_PI), sin($w2 + M_PI)); $c2->Multiply($d / 2.0)->Add($center);
$c3 = new Vector2(cos($w2 + $w4), sin($w2 + $w4)); $c3->Multiply($d / 2.0)->Add($center);
$c4 = new Vector2(cos($w2 + 3 * $w4), sin($w2 + 4 * $w4)); $c4->Multiply($d / 2.0)->Add($center);
$c = Vector2::sSubstract($c2, $c1)->Divide(2);
$m = Vector2::sSubstract($c4, $c3);
for ($x = 0; $x < $d; $x++)
{
$p1 = Vector2::sAdd($c3, Vector2::sMultiply($m, ($x / $d)))->Substract($c);
$p2 = Vector2::sAdd($c3, Vector2::sMultiply($m, ($x / $d)))->Add($c);

$color = imageGetLine($i, round($p1->x), round($p1->y), round($p2->x), round($p2->y));
imagesetpixel($r, $z + 1, $x + 1, imagecolorallocate($r, array_sum($color['r']), array_sum($color['g']), array_sum($color['b'])));
}
}

return $r;
}

补充功能:
这是 imageGetLine(),用于从输入图像中获取直线(但可能是对角线)样本。

function imageGetLine($i, $sx, $sy, $tx, $ty)
{
$r = array(
'r' => array(),
'g' => array(),
'b' => array()
);

if (abs($tx - $sx) > abs($ty - $sy))
{
if ($sx > $tx)
{
$tmp = $sx;
$sx = $tx;
$tx = $tmp;
}

for ($x = $sx; $x < $tx; $x++)
{
$y = $sy + ($x - $sx) / ($tx - $sx) * ($ty - $sy);

$color = imageGetColorAt($i, $x, $y);
$r['r'][] = $color['r'];
$r['g'][] = $color['g'];
$r['b'][] = $color['b'];
}
}
else
{
if ($sy > $ty)
{
$tmp = $sy;
$sy = $ty;
$ty = $tmp;
}

for ($y = $sy; $y < $ty; $y++)
{
$x = $sx + ($y - $sy) / ($ty - $sy) * ($tx - $sx);

$color = imageGetColorAt($i, $x, $y);
if ($color === false)
continue;
$r['r'][] = $color['r'];
$r['g'][] = $color['g'];
$r['b'][] = $color['b'];
}
}
return $r;
}

imageGetColorAt() 除了检索给定位置的像素颜色外什么都不做:

function imageGetColorAt($i, $x, $y)
{
// @todo nearest resampling instead of rounding
$color = @imagecolorat($i, round($x), round($y));
if ($color === false)
return false;
return array(
'r' => ($color >> 16) & 0xFF,
'g' => ($color >> 8) & 0xFF,
'b' => $color & 0xFF
);
}

Vector2 类可以在这里查看:https://github.com/cobrafast/prophp/blob/master/Math/vector2.class.php


我面临的一个问题是,在 imageGetColorAt() 中,我得到了一些 越界(因为显然 GD 从 0 开始计数n-1) 错误,我只是用 @ 静音并让它返回 false 跳过,因为我不知道如何修复所有导致给定 imageGetLine() 坐标的奇特数学。
这会是我大麻烦的原因吗?

我的努力哪里出了问题?我错过了什么吗?


编辑 2013-11-05

在自己摆弄了一段时间之后,我现在很接近了:

我添加的是将我的颜色值剪裁为 {0..255} 并将线样本总和除以样本数量(这样我就可以得到线的平均值):

...
for ($x = 0; $x < $d; $x++)
{
$p1 = Vector2::sAdd($c3, Vector2::sMultiply($m, ($x / $d)))->Substract($c);
$p2 = Vector2::sAdd($c3, Vector2::sMultiply($m, ($x / $d)))->Add($c);

$color = imageGetLine($i, round($p1->x), round($p1->y), round($p2->x), round($p2->y));
$color = normalizeColor(array(
'r' => array_sum($color['r']) / count($color['r']),
'g' => array_sum($color['g']) / count($color['g']),
'b' => array_sum($color['b']) / count($color['b'])
));
imagesetpixel($r, $z + 1, $x + 1, imagecolorallocate($r, $color['r'], $color['g'], $color['b']));
}
...

如前所述,normalizeColor 只做:

return array(
'r' => min(array(255, max(array(0, $color['r'])))),
'g' => min(array(255, max(array(0, $color['g'])))),
'b' => min(array(255, max(array(0, $color['b']))))
);

但是很明显,好像还是哪里不对……

最佳答案

for ($z = 3*$u2/4; $z < $u2*7/4; $z++) {
...
$c4 = ... sin($w2 + 3 * $w4) ... // it was sin($w2 + 4 * $w4)
...
for ($x = 0; $x < $d; $x++) {
imagesetpixel($r, $z - 3*$u2/4, $x, ...);
}
}

Actual result

关于php - 如何修复我的氡转化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19689118/

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