C++ 将 2D 像素阵列旋转 90 度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16684856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:32:21  来源:igfitidea点击:

Rotating a 2D pixel array by 90 degrees

c++image-processingrotationpixel

提问by noob

I have an array of pixel data for an image. The image I am getting is already rotated to 270 degrees. So I am trying to rotate it again by 90 degrees to have the correct image. I've tried a transpose algorithm, by changing data[x][y]to data[y][x], but I don't think that's the correct way. Can anyone guide me what can I do to have it rotated?

我有一个图像的像素数据数组。我得到的图像已经旋转到 270 度。所以我试图将它再次旋转 90 度以获得正确的图像。我已经尝试了转置算法,通过更改data[x][y]data[y][x],但我认为这不是正确的方法。谁能指导我我该怎么做才能让它旋转?

回答by raj raj

You have old_data[rows][cols]and new_data[cols][rows], then:

你有old_data[rows][cols]new_data[cols][rows],那么:

for(int i=0; i<cols; i++) {
    for(int j=0; j<rows; j++) {
        new_data[i][j] = old_data[rows-1-j][i];
    }
}

This should rotate old_data by 90 degrees CW.

这应该将 old_data 旋转 90 度 CW。

回答by mhaghighat

If you want to do it in-place with O(1) space, you can follow this:

如果你想用 O(1) 空间就地做,你可以按照这个:

  1. Transpose the matrix by swapping data[i][j]and data[j][i]:

    for (int i = 0; i < n; i += 1) {
        for (int j = i+1; j < n; j += 1) {
            swap(data[i][j], data[j][i]);
        }
    }
    
  2. Reverse each row or columnfor +90 or -90 degrees of rotation, respectively. For example for +90 degrees of rotation:

    for (int i = 0; i < n; i += 1) {
        for (int j = 0; j < n/2; j += 1) {
            swap(data[i][j], data[i][n-1-j]);
        }
    }
    
  1. 通过交换data[i][j]和 来转置矩阵data[j][i]

    for (int i = 0; i < n; i += 1) {
        for (int j = i+1; j < n; j += 1) {
            swap(data[i][j], data[j][i]);
        }
    }
    
  2. 分别反转每行或每列+90 或 -90 度的旋转。例如对于 +90 度旋转:

    for (int i = 0; i < n; i += 1) {
        for (int j = 0; j < n/2; j += 1) {
            swap(data[i][j], data[i][n-1-j]);
        }
    }
    

回答by herohuyongtao

This can be done without using any extra space, so called In-place matrix transposition(not exact the same). Remember to do some mirroring after the transposition.

这可以在不使用任何额外空间的情况下完成,所谓的就地矩阵转置(不完全相同)。记得在换位后做一些镜像。

  1. If the image is square

    enter image description here

  2. If the image is not square

    • For non-square matrices, the algorithms are more complicated. Many of the algorithms prior to 1980 could be described as "follow-the-cycles" algorithms. That is, they loop over the cycles, moving the data from one location to the next in the cycle. In pseudocode form:

    enter image description here

  1. 如果图像是正方形

    在此处输入图片说明

  2. 如果图像不是方形的

    • 对于非方阵,算法更复杂。1980 年之前的许多算法都可以被描述为“follow-the-cycles”算法。也就是说,它们在循环中循环,将数据从循环中的一个位置移动到下一个位置。伪代码形式:

    在此处输入图片说明

回答by Akinjiola Toni

To rotate the image (2D matrix) by 90deg, you can easily do this by mapping out a pattern between the initial state and the end state after rotating it by 90deg.

要将图像(2D 矩阵)旋转 90 度,您可以通过在旋转 90 度后绘制初始状态和结束状态之间的模式来轻松完成此操作。



a[i][j] => a[m][n]
a[0][0] => a[0][2]
a[0][1] => a[1][2]
a[0][2] => a[2][2]
a[1][0] => a[0][1]
a[1][1] => a[1][1]
a[1][2] => a[2][1]
a[2][0] => a[0][0]
a[2][1] => a[1][0]
a[2][2] => a[2][0]

Now the solution is obvious. All the J's turn to M and N = (Size of matrix(2) - I).

现在解决方案是显而易见的。所有的 J 都转向 M 和 N = (矩阵的大小 (2) - I)。



const rotateImage = (a) => {
  let size = a.length;
  let results = new Array(size);
  for (let i = 0; i < size; i++) {
    results[i] = new Array(size);
  }
  for (let i = 0; i < size; i++) {
    for (let j = 0; j < size; j++) {
      results[j][(size - 1) - i] = a[i][j];
    }
  }
  return results;
}

console.log(rotateImage([
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
]));