将图像旋转90度的算法? (无额外记忆) [英] Algorithm to rotate an image 90 degrees in place? (No extra memory)

查看:696
本文介绍了将图像旋转90度的算法? (无额外记忆)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在嵌入式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];
}

我希望移动数据数组,然后交换 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.

推荐答案

这可能有助于:就地矩阵转置

(你也可以必须在转置后做一些镜像,如rlbond提到)。

(You might also have to do some mirroring after the transposition, as rlbond mentions).

这篇关于将图像旋转90度的算法? (无额外记忆)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆