如何在旋转之前获取旋转坐标的位置?

huangapple go评论75阅读模式
英文:

How to get positions of rotated coordinate before rotation?

问题

以下是翻译好的部分:

为了旋转图像,并且旋转后的图像上出现了一些空洞或者缺失的像素。为了得到应该填充缺失像素的颜色,我需要获得在未旋转图像中对应缺失像素的位置。

为了计算旋转后的像素坐标,我执行以下操作:

double rotationAsRadiant = Math.toRadians( 360 - rotationInDegrees );
double cos = Math.cos( rotationAsRadiant );
double sin = Math.sin( rotationAsRadiant );
int xAfterRotation = (int)( x * cos + y * sin );
int yAfterRotation = (int)( -x * sin + y * cos );

如何获得用于计算 xAfterRotation 和 yAfterRotation 的 x 和 y 值呢?

英文:

I am trying to rotate an image and the rotated image has some holes or missing pixels. In order to get the color that should fill the missing pixel, I need to get the position of where the missing pixel would be in the not-rotated image.

To calculate the pixel coordinate after rotation I do this

double rotationAsRadiant = Math.toRadians( 360 - rotationInDegrees );
double cos = Math.cos( rotationAsRadiant );
double sin = Math.sin( rotationAsRadiant );
int xAfterRotation = (int)( x * cos + y * sin );
int yAfterRotation = (int)( -x * sin + y * cos );

How can I get the x and y used in calculating xAfterRotation and yAfterRotation?

答案1

得分: 1

将公式反转,如下所示:

    double rotationAsRadiant = Math.toRadians( rotationInDegrees );
    double cos = Math.cos( rotationAsRadiant );
    double sin = Math.sin( rotationAsRadiant );
    for (int xAfter = 0; xAfter < width; xAfter++) {
        for (int yAfter = 0; yAfter < height; yAfter++) {
            int xBefore = (int)( xAfter * cos + yAfter * sin );
            int yBefore = (int)( -xAfter * sin + yAfter * cos );
            // 使用来自 xBefore/yBefore 的原始像素绘制像素 xAfter/yAfter
            ...
        }
    }

这样,您肯定会填充结果图像的所有像素,原始像素最接近准确位置。不会有空洞。

您最初的方法是基于问题“给定的源像素在目标图像中的位置在哪里?”。不能保证结果会覆盖所有像素(您已经看到了空洞)。

我的方法是基于问题“对于给定的目标像素,我从哪里找到它的源像素?”。

英文:

Invert the formula like in:

double rotationAsRadiant = Math.toRadians( rotationInDegrees );
double cos = Math.cos( rotationAsRadiant );
double sin = Math.sin( rotationAsRadiant );
for (int xAfter = 0; xAfter &lt; width; xAfter++) {
    for (int yAfter = 0; yAfter &lt; height; yAfter++) {
        int xBefore = (int)( xAfter * cos + yAfter * sin );
        int yBefore = (int)( -xAfter * sin + yAfter * cos );
        // paint pixel xAfter/yAfter using original from xBefore/yBefore
        ...
    }
}

This way, you will surely fill all pixels of the resulting image, with the original pixel closest to the exact position. There will be no holes.

Your original approach was driven by the question "Where does a given source pixel go in the destination image?". There's no guarantee that the results will cover all pixels (you've seen holes).

My approach goes along the question "For a given destination pixel, where do I find its source?".

huangapple
  • 本文由 发表于 2020年9月25日 18:34:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64062417.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定