算法旋转图像90度的地方? (没有额外的内存) [英] Algorithm to rotate an image 90 degrees in place? (No extra memory)

查看:275
本文介绍了算法旋转图像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];
}

我希望移动数据阵列周围,然后交换在宽度和内容高度成员变量。所以,如果我开始与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天全站免登陆