将图像原地旋转 90 度的算法?(没有额外的内存) [英] Algorithm to rotate an image 90 degrees in place? (No extra memory)
问题描述
在嵌入式 C 应用程序中,我有一个大图像,我想将其旋转 90 度.目前我使用众所周知的简单算法来做到这一点.但是,该算法要求我制作另一个图像副本.我想避免为副本分配内存,我宁愿就地旋转它.由于图像不是方形的,这很棘手.有人知道合适的算法吗?
In an embedded C app, I have a large image that I'd like to rotate by 90 degrees. Currently I use the well-known simple algorithm to do this. However, this algorithm requires me to make another copy of the image. I'd like to avoid allocating memory for a copy, I'd rather rotate it in-place. Since the image isn't square, this is tricky. Does anyone know of a suitable algorithm?
编辑添加澄清,因为人们在问:
Edited to add clarification, because people are asking:
我以通常的格式存储图像:
I store an image in the usual format:
// Images are 16 bpp
struct Image {
int width;
int height;
uint16_t * data;
};
uint16_t getPixel(Image *img, int x, int y)
{
return img->data[y * img->width + x];
}
我希望移动 data
数组的内容,然后交换 width
和 height
成员变量.所以如果我从一个 9x20 像素的图像开始,然后旋转它,我最终会得到一个 20x9 像素的图像.这改变了图像的步幅,使算法复杂化了很多.
I'm hoping to move the contents of the data
array around, then swap over the width
and height
member variables. So if I start with a 9x20 pixel image, then rotate it, I'll end up with a 20x9 pixel image. This changes the stride of the image, which complicates the algorithm a lot.
推荐答案
这可能会有所帮助:In-位矩阵转置.
(如 rlbond 所述,您可能还需要在移调后进行一些镜像).
(You might also have to do some mirroring after the transposition, as rlbond mentions).
这篇关于将图像原地旋转 90 度的算法?(没有额外的内存)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!