如何修复我的氡转换?

时间:2013-10-30 16:55:16

标签: php math transformation php-gd

我正在PHP中this article之后构建一个氡转换。

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

输入

预期结果

实际结果

...

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


现在是代码的时间。

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

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()内,我得到了一些out of bounds(因为显然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']))))
);

但显然,似乎还有一些问题......

1 个答案:

答案 0 :(得分:1)

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