【问题标题】:PHP - How to get the coordinates of the point after rotation?PHP - 如何在旋转后获取点的坐标?
【发布时间】:2021-06-04 07:35:31
【问题描述】:

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

$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:

rightEyeY = 446

rightEyeX = 625

leftEyeY = 433

leftEyeX = 733

TO:

rightEyeY = 432

rightEyeX = 640

leftEyeY = 432

leftEyeX = 749

【问题讨论】:

  • 中间点将被$rightEyeY - $leftEyeY;中的两个除以
  • How can I get the coordinates of each eye after rotation? - 你为什么不旋转每只眼睛?
  • 正如stackoverflow.com/questions/8742237/coordinate-rotation-in-php 中所指出的,您正在计算$x$y,同时在计算中使用它们。
  • @NigelRen 他是先减法再加法。但我正在这样做。我只是计算并将其放入变量中。即使我改变了变量名,也不会影响结果。
  • 在第一个计算 - $x = $x * $cosa - $y * $sina;,你改变了$x的值,这肯定会影响到下一个结果$y = $x * $sina + $y * $cosa;,它依赖于原来的x坐标。跨度>

标签: php rotation coordinates imagick


【解决方案1】:

我尝试了一些东西,但得到了其他坐标。它看起来很适合我。诀窍是旋转平移到中心。我认为差异来自 -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 度。

二维旋转矩阵和平移代码

$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

【讨论】:

  • 好像旋转后,它的宽高变大了。因为旋转不会在背面下方,但会扩展宽度和高度。是否可以在旋转期间防止图像中的这种扩展?
  • 如前所述,它取决于旋转中心。想象一下每只眼睛的投影圆圈,它看起来对我来说是正确的。您没有提供原始图片尺寸,所以我假设为 800x800。
  • 不同图像的尺寸不同。但这个是 1500x1125。在 Photoshop 中,当我旋转图像时,它隐藏在背面。但在 Imagick 中,它使工作空间更大。我找不到任何设置中心点的方法。
  • Photoshop 始终围绕中心旋转并进行剪裁,因此图像不会增长。使用imagick时需要自己剪辑。寻找chopcrop
  • 拥有完整的其他维度当然会导致不同的值,因为旋转中心不同。我很确定,通过我所做的测试,我的答案是正确的。旋转后图像大小必须相同以匹配正确的坐标,否则您需要添加新的偏移量。 (newSizeX-oldSizeX)/2 和 Y 相同。
猜你喜欢
  • 1970-01-01
  • 2010-10-09
  • 1970-01-01
  • 2013-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-04
  • 1970-01-01
相关资源
最近更新 更多