【问题标题】:What am I doing wrong when rotating the picture by 90 degrees将图片旋转 90 度时我做错了什么
【发布时间】:2013-11-05 17:29:48
【问题描述】:

我完全被这个问题难住了。所以这是我将数组旋转 90 度的逻辑:

例子:

1 2 3 4 5.....   ^
2 6 8 7 4.....   | 
6 4 9 8 0.....   | .....THEN.....  ------>
8 3 0 5 9.....   | 

所以如果我想将它旋转 90 度,我想要的是从最底部的行向上读取数组,然后在右侧添加一个宽度。所以数组将是

8 6 2 1
3 4 6 2 
0 9 8 3
5 8 7 4
9 0 4 5

现在我将这个逻辑应用到我的像素数组并将图片旋转 90 度

这是我的 Java 方法代码:

public void rotate90(){

    Pixel[][] rotated = new Pixel[Iwidth][Iheight];

    int Ch = Iheight-1, Cw = 0;

    while (Cw < Iwidth){
        while (Ch > -1){
            rotated[Cw][Iheight - Ch - 1] = new Pixel(Image[Ch][Cw].getRed(), Image[Ch][Cw].getGreen(), Image[Ch][Cw].getBlue());
            Ch--;
        }
        Cw++;
        Ch = Iheight-1;
    }

    Image = rotated;


    int temp = Iheight;
    Iheight = Iwidth;
    Iwidth = temp;
}

当我调用该方法时,它使图片乱码,但图片的宽度和高度是切换/交换的。

但这是我的时候,如果我尝试调用该方法 TWICE,那么图片将正确旋转 180 度

如果我尝试调用方法四次,图片将保持原样。

谁能阐明我错过了什么或做错了什么?


我找到了问题,我无法回答我自己的问题,所以这是我的代码的问题:

而不是放

imageWriter.println(Iwidth + " " + Iheight);

我有一个

imageWriter.println(Iheight + " " + Iwidth);

在我的作家方法中,我很傻 -____-

感谢@Marcelo 和@rolfl 提供的帮助。发现问题了。

我的代码一直都是正确的,我有一个愚蠢的 1 行代码把我搞砸了

【问题讨论】:

  • 我看不出有什么明显的错误。图片是怎么乱码的?你确定问题不在于显示代码?
  • 我也是这么想的。但是当我尝试调用 rotate90 两次时,它会产生正确的 180 度旋转。同样调用4次,生成相同的原图
  • 我不能肯定,但是有一个模式,我不能用语言来描述它。这是图片的示例。 OriginalGarbled Image
  • 当我尝试调用 rotate90() 两次时,它会产生 a correct 180 degree rotation

标签: java arrays image ppm


【解决方案1】:

嗯,Marcelo 打败了我,但我的程序实际上运行了......

首先,要么实现clone() 方法,要么为Pixel 创建一个新的构造函数,它将现有像素“复制”。

这种旋转作为位置的函数更容易处理......我在这里使用字符串值而不是像素来工作

public static void main(String[] args) {
    final int width = 5, height = 3;
    String[][] values = new String[height][width];
    for (int c = 0; c < width; c++) {
        for (int r = 0; r < height; r++) {
            values[r][c] = String.format("%3d", r * width + c);
        }
    }

    for (String[] row : values) {
        System.out.println(Arrays.toString(row));
    }

    System.out.println();

    int nheight = width;
    int nwidth = height;

    String[][] rotate = new String[nheight][nwidth];
    for (int c = 0; c < nwidth; c++) {
        for (int r = 0; r < nheight; r++) {
            rotate[r][c] = values[height - 1 - c][r];
        }
    }

    for (String[] row : rotate) {
        System.out.println(Arrays.toString(row));
    }
}

它产生输出

[  0,   1,   2,   3,   4]
[  5,   6,   7,   8,   9]
[ 10,  11,  12,  13,  14]

[ 10,   5,   0]
[ 11,   6,   1]
[ 12,   7,   2]
[ 13,   8,   3]
[ 14,   9,   4]

【讨论】:

    【解决方案2】:

    这应该可以满足您的需要:

    public void rotate90() {
        Pixel[][] rotated = new Pixel[iWidth][iHeight];
    
        for (int x = 0; x < iWidth; x++) {
            for (int y = 0; y < iHeight; y++) {
                rotated[x][y] = image[iWidth- y - 1][x];
            }
        }
    
        image = rotated;
    
        int temp = iHeight;
        iHeight = iWidth;
        iWidth = temp;
    }
    

    我还建议您查看java Naming Conventions。这对提高代码的可读性有很大帮助。

    【讨论】:

    • 对于 Pixel[][] 的行旋转 = new Pixel[iHeight][iWidth];实际上,我故意交换了 iHeight 和 iWidth 的位置,因为原始 Image 数组是 Image[iHeight][iWidth]。所以当我尝试你的代码时,这将导致 arrayoutofboundsexception
    • @user1862452 我在做假设,因为对此尚不清楚,我将其更改为您交换的高度和宽度。试一试。
    • 您确定使用“rotated[x][y] = image[iWidth- y - 1][x];”?我将 iWidth 更改为 iHeight,它停止给出 -1 错误。不幸的是,它仍然输出相同的乱码图像。 ://
    • 用我的代码试试。如果您有用于显示图像的代码,我们甚至可以查看它是否有问题。
    • 是的,我试过用你的方法,我的输出和我的一样,除了 iWidth 我用 iHeight 代替了
    【解决方案3】:

    扔掉它并使用 AffineTransform。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-02
      • 1970-01-01
      • 2011-01-14
      • 1970-01-01
      • 1970-01-01
      • 2022-11-11
      相关资源
      最近更新 更多