【问题标题】:Rotating image issue旋转图像问题
【发布时间】:2016-05-23 05:12:38
【问题描述】:

这是我编写的一些代码,用于将图像顺时针旋转 90 度。当图像的高度与其宽度匹配时,这可以正常工作。但是,如果不是这种情况,则图像在图像的其余部分应该在的末端有一个黑色边缘。任何帮助表示赞赏

public void rotate(){
    int y = this.image.length;
    int x = this.image[0].length;
    int[][] rotate = new int[x][y];
    UI.println(y);
    UI.println(x);
    for(int row = 0; row<x/2; row++){
        for(int col = 0; col < ((y+1)/2); col++){
            int temp = this.image[row][col];
            rotate[row][col] = this.image[y-1-col][row];
            rotate[y-1-col][row] = this.image[y-1-row][y-1-col];
            rotate[y-1-row][y-1-col] = this.image[col][y-1-row];
            rotate[col][y-1-row] = temp;
        }
    }
    int[][] image = new int[x][y]; 
    this.image = rotate;

【问题讨论】:

标签: java arrays rotation 2d


【解决方案1】:

旋转的变换矩阵是

| c   -s  0 |
| s   c   0 |
| 0   0   1 |

c = cos theta 和 s = sin theta
翻译的变换矩阵是

| 1   0   xr |
| 0   1   yr |
| 0   0   1  |

您需要将对象的翻译中心翻译为原点
然后旋转
然后翻译回来

|1  0  xr|   |c  -s  0|   |1  0  -xr|   |x|
|0  1  yr| x |s  c   0| x |0  1  -yr| x |y|
|0  0  1 |   |0  0   1|   |0  0    1|   |1|

解决后你会得到

|x*c-y*s-xr*c+yr*s+xr|
|x*s+y*c-xr*s-yr*c+yr|
|          1         |

所以新点 (x2, y2) 是这样的

x2 = x*c-y*s-xr*c+yr*s+xr
y2 = x*s+y*c-xr*s-yr*c+yr

把 c = 0 (因为 cos90 = 0)
和 s = 1(因为 sin90 = 1

x2 = -y+yr+xr
y2 = x-xr+yr

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多