微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP - 如何在旋转后获取点的坐标?

如何解决PHP - 如何在旋转后获取点的坐标?

我希望图像中的眼睛是水平的

$rightEyeY = 446;
$rightEyeX = 625;
$leftEyeY = 433;
$leftEyeX = 733;

// Get middle point of two eyes
$y = $rightEyeY - $leftEyeY;
$x = $rightEyeX - $leftEyeX;

$angle = rad2deg(atan2($y,$x)) - 180; // -6.8 degrees

$manager = new ImageManager(['driver' => 'imagick']);
$image = $manager->make('image.jpg')->rotate($angle);
$a = $angle * pi() / 180.0;
$cosa = cos($a);
$sina = sin($a);
$x = $x * $cosa - $y * $sina; // This one calculates x of the middle point not each eye.
$y = $x * $sina + $y * $cosa; // This one calculates y of the middle point not each eye.

旋转后如何获取每只眼睛的坐标?

我想要那些变量在顶部

FROM:

右眼 = 446

rightEyeX = 625

leftEyeY = 433

leftEyeX = 733

TO:

右眼 = 432

rightEyeX = 640

leftEyeY = 432

leftEyeX = 749

解决方法

我尝试了一些东西,但得到了其他坐标。它看起来很适合我。诀窍是旋转平移到中心。我认为差异来自 -6.83 的角度是错误的(OP 代码中眼睛的距离)。

如果你不平移,旋转将在坐标系的原点 (0,0) 处完成,然后是图像空间的左上角,但你想要中心。

$angle = deg2rad(-6.83);
list($leftX,$leftY)  = $rotateEye($leftEyeX,$leftEyeY,$angle);
list($rightX,$rightY) = $rotateEye($rightEyeX,$rightEyeY,$angle);

然后给我

L: (734.56131177907,734.56131177907)
R: (628.87375746869,418.91568508316)

但图像看起来像那样,左蓝色,右红色。下面一对是原点,上面一对旋转了-6.83度。

enter image description here

二维旋转矩阵和平移代码

$rotateEye = function ($x,$y,$angle) use ($centerX,$centerY): array {
    $tx = $x - $centerX;
    $ty = $y - $centerY;
    $rx = cos($angle) * $tx - sin($angle) * $ty;
    $ry = sin($angle) * $tx + cos($angle) * $ty;
    return [$rx + $centerX,$ry + $centerY];
};

这里是完整代码的pastebin

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。