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

标签 php math transformation php-gd

我在 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/

相关文章:

c++ - 在 OpenGL 中渲染相机前的对象

algorithm - 转换列表的平均转换矩阵

php - 使用php从mysql获取所有字段值

java - mysql 警告它已在 PHP 中更新

android - 如何使用等同于特定 dp 的 rQuadTo 创建圆角路径?

cosf(M_PI_2) 不返回零

工作流中中间作业的 Hadoop SequenceFile 输入/输出

php - 带有 Auth 的 Laravel 处理程序错误上下文中的错误

php - 了解 woocommerce 主题 - 使用我自己的主题

php - 在 PHP 中实现位掩码或关系 ACL