织梦CMS - 轻松建站从此开始!

罗索

图像实战 - 旋转RGB、YUV图像

jackyhwei 发布于 2022-04-09 10:52 点击:次 
在开发相机程序显示相机预览数据时,有时相机的位置是固定的,那我们可能会需要用到图像的旋转进行纠正,以获取我们需要的旋转一定角度后的图像。本文介绍了一些常见的YUV、RGB数据的旋
TAG: 图像旋转  图像翻转  

在开发相机程序显示相机预览数据时,有时相机的位置是固定的,那我们可能会需要用到图像的旋转进行纠正,以获取我们需要的旋转一定角度后的图像。本文介绍了一些常见的YUV、RGB数据的旋转方法。

效果图:

 
0度
 
90度
 
180度
 
270度

一、 按像素点旋转图像

假设有以下一张图像:

Pixel1  Pixel2  Pixel3  Pixel4

Pixel5  Pixel6  Pixel7  Pixel8

其图像分辨率是width x height

  1. 在顺时针旋转90度后,其内容将会变成:

Pixel5  Pixel1

Pixel6  Pixel2

Pixel7  Pixel3

Pixel8  Pixel4

分辨率变成了height x width

也就是:

  • 原始数据第 height - 1 行第 0 列的像素点将会变成目标数据第 0 行第 0 列的像素点
  • 原始数据第 height - 1 行第 1 列的像素点将会变成目标数据第 1 行第 0 列的像素点
  • ...
  • 原始数据第 height - 1 行第 width - 1 列的像素点将会变成目标数据第 width - 1 行第 0 列的像素点
  • ...
  • 原始数据第 1 行第 width - 1 列的像素点将会变成目标数据第 width - 1 行第 height - 2 列的像素点
  • 原始数据第 1 行第 width - 2 列的像素点将会变成目标数据第 width - 2 行第 height - 2 列的像素点
  • 原始数据第 1 行第 width - 3 列的像素点将会变成目标数据第 width - 3 行第 height - 2 列的像素点
  • ...
  • 原始数据第 i 行第 j 列的像素点将会变成目标数据第 j 行第 height - 1 - i 列的像素点

 

  1. 若旋转180度,其内容将会变成:

Pixel8  Pixel7  Pixel6  Pixel5

Pixel4  Pixel3  Pixel2  Pixel1

也就是

  • 原始数据第 0 行第 0 列的像素点将会变成目标数据第 height - 1 行第 width - 1 列的像素点
  • 原始数据第 1 行第 0 列的像素点将会变成目标数据第 height - 2 行第 width - 1 列的像素点
  • ...
  • 原始数据第 height - 1 行第 0 列的像素点将会变成目标数据第 0 行第 width - 1 列的像素点
  • 原始数据第 height - 1 行第 1 列的像素点将会变成目标数据第 0 行第 width - 2 列的像素点
  • ...
  • 原始数据第 i 行第 j 列的像素点将会变成目标数据第 height - 1 - i 行第 width - 1 - j 列的像素点

 

  1. 若旋转270度,其内容将会变成:

Pixel4  Pixel8

Pixel3  Pixel7

Pixel2  Pixel6

Pixel1  Pixel5

也就是:

  • 原始数据第 0 行第 width - 1 列的像素点将会变成目标数据第 0 行第 0 列的像素点
  • 原始数据第 0 行第 width - 2 列的像素点将会变成目标数据第 1 行第 0 列的像素点
  • ...
  • 原始数据第 0 行第 0 列的像素点将会变成目标数据第 width - 1 行第 0 列的像素点
  • ...
  • 原始数据第 1 行第 width - 1 列的像素点将会变成目标数据第 0 行第 1 列的像素点
  • 原始数据第 1 行第 width - 2 列的像素点将会变成目标数据第 1 行第 1 列的像素点
  • ...
  • 原始数据第 i 行第 j 列的像素点将会变成目标数据第 width - j - 1 行第 i 列的像素点

二、 旋转 BGR24 / RGB24 数据

BGR24 / RGB24 都是以3个byte作为一个像素,因此在旋转时需要将3个byte作为一个整体进行旋转。示例代码如下。

  • 旋转90度:

 

  1. void rotateRgb24Degree90(char *rgb24, char *rotatedRgb24, int width, int height) { 
  2.     int lineDataSize = width * 3; 
  3.     int rotatedRgb24Index = 0
  4.     int finalLineStartIndex = (height - 1) * lineDataSize; 
  5.     for (int w = 0; w < lineDataSize; w += 3) { 
  6.         int bgr24StartIndex = finalLineStartIndex + w; 
  7.         int offset = 0
  8.         for (int h = 0; h < height; h++) { 
  9.             rotatedRgb24[rotatedRgb24Index++] = rgb24[bgr24StartIndex - offset]; 
  10.             rotatedRgb24[rotatedRgb24Index++] = rgb24[bgr24StartIndex - offset + 1]; 
  11.             rotatedRgb24[rotatedRgb24Index++] = rgb24[bgr24StartIndex - offset + 2]; 
  12.             offset += lineDataSize; 
  13.         } 
  14.     } 
  • 旋转180度
  1. void rotateRgb24Degree180(char *rgb24, char *rotatedRgb24, int width, int height) { 
  2.     int lineDataSize = width * 3; 
  3.     int rotatedRgb24Index = 0
  4.     int rgb24StartIndex = lineDataSize * height - 3; 
  5.     for (int h = height - 1; h >= 0; h--) { 
  6.         for (int w = lineDataSize - 3; w >= 0; w -3) { 
  7.             rotatedRgb24[rotatedRgb24Index++] = rgb24[rgb24StartIndex]; 
  8.             rotatedRgb24[rotatedRgb24Index++] = rgb24[rgb24StartIndex + 1]; 
  9.             rotatedRgb24[rotatedRgb24Index++] = rgb24[rgb24StartIndex + 2]; 
  10.             rgb24StartIndex -3
  11.         } 
  12.     } 
  • 旋转270度
    1. void rotateRgb24Degree270(char *rgb24, char *rotatedRgb24, int width, int height) { 
    2.     int lineDataSize = width * 3; 
    3.     int rotatedRgb24Index = 0
    4.     int finalColumnStartIndex = lineDataSize
    5.     for (int w = 0; w < lineDataSize; w += 3) { 
    6.         finalColumnStartIndex -3
    7.         int offset = 0
    8.         for (int h = 0; h < height; h++) { 
    9.             int originalOffset = finalColumnStartIndex + offset; 
    10.             rotatedRgb24[rotatedRgb24Index++] = rgb24[originalOffset]; 
    11.             rotatedRgb24[rotatedRgb24Index++] = rgb24[originalOffset + 1]; 
    12.             rotatedRgb24[rotatedRgb24Index++] = rgb24[originalOffset + 2]; 
    13.             offset += lineDataSize; 
    14.         } 
    15.     } 

 

三、旋转NV21、NV12数据

对于NV21或NV12数据,其排列是width * height个Y连续存储,接下来是 height / 2 行的UV数据,每一行的UV数据是 width / 2 个U和 width / 2 个V交叉存储(NV21是VU VU VU VU....,NV12是UV UV UV UV ....),Y数据的大小刚好就是像素数,可以直接进行旋转,对于U和V数据,需要考虑下标的跳跃情况(由于NV21和NV12的U和V只是刚好位置相反,因此旋转NV21的代码也同样适用于旋转NV12)。示例代码如下。

  • 旋转90
  1. void rotateYuv420spDegree90(char *yuv420sp, char *rotateYuv420sp, int width, int height) { 
  2.     int yFinalLineStartIndex = (height - 1) * width; 
  3.     int rotatedYIndex = 0
  4.     //rotate y 
  5.     for (int w = 0; w < width; w++) { 
  6.         int yStartIndex = yFinalLineStartIndex + w; 
  7.         int offset = 0
  8.         for (int h = 0; h < height; h++) { 
  9.             *(rotateYuv420sp + rotatedYIndex++) = *(yuv420sp + yStartIndex - offset); 
  10.             offset += width; 
  11.         } 
  12.     } 
  13.     //rotate uv 
  14.     int uvFinalLineStartIndex = width * height * 3 / 2 - width; 
  15.     int rotatedVIndex = width * height; 
  16.     int rotatedUIndex = width * height + 1; 
  17.     for (int w = 0; w < width; w += 2) { 
  18.         int uvStartIndex = uvFinalLineStartIndex + w; 
  19.         int offset = 0
  20.         for (int h = 0; h < height; h += 2) { 
  21.             int originalOffset = uvStartIndex - offset; 
  22.             *(rotateYuv420sp + rotatedVIndex) = *(yuv420sp + originalOffset); 
  23.             *(rotateYuv420sp + rotatedUIndex) = *(yuv420sp + originalOffset + 1); 
  24.             offset += width; 
  25.             rotatedVIndex += 2; 
  26.             rotatedUIndex += 2; 
  27.         } 
  28.     } 
  • 旋转180
    1. void rotateYuv420spDegree180(char *yuv420sp, char *rotateYuv420sp, int width, int height) { 
    2.     int yIndex = width * height - 1; 
    3.     int rotatedYIndex = 0
    4.     //rotate y 
    5.     for (int h = height - 1; h >= 0; h--) { 
    6.         for (int w = width - 1; w >= 0; w--) { 
    7.             *(rotateYuv420sp + rotatedYIndex++) = *(yuv420sp + yIndex); 
    8.             yIndex--; 
    9.         } 
    10.     } 
    11.     int uvIndex = width * height * 3 / 2 - 2; 
    12.     int rotatedVIndex = width * height; 
    13.     int rotatedUIndex = width * height + 1; 
    14.     //rotate uv 
    15.     for (int h = height - 1; h >= 0; h -2) { 
    16.         for (int w = width - 1; w >= 0; w -2) { 
    17.             *(rotateYuv420sp + rotatedVIndex) = *(yuv420sp + uvIndex); 
    18.             *(rotateYuv420sp + rotatedUIndex) = *(yuv420sp + uvIndex + 1); 
    19.             uvIndex -2
    20.             rotatedVIndex += 2; 
    21.             rotatedUIndex += 2; 
    22.         } 
    23.     } 
  • 旋转270
    1. void rotateYuv420spDegree270(char *yuv420sp, char *rotateYuv420sp, int width, int height) { 
    2.     int rotatedYIndex = 0
    3.     int yFinalColumnStartIndex = width
    4.     //rotate y 
    5.     for (int w = 0; w < width; w++) { 
    6.         int offset = 0
    7.         for (int h = 0; h < height; h++) { 
    8.             *(rotateYuv420sp + rotatedYIndex++) = *(yuv420sp + yFinalColumnStartIndex + offset); 
    9.             offset += width; 
    10.         } 
    11.         yFinalColumnStartIndex--; 
    12.     } 
    13.     //rotate uv 
    14.     int uvFinalColumnStartIndex = width * height + width; 
    15.     int rotatedVIndex = width * height; 
    16.     int rotatedUIndex = width * height + 1; 
    17.     for (int w = 0; w < width; w += 2) { 
    18.         uvFinalColumnStartIndex -2
    19.         int offset = 0
    20.         for (int h = 0; h < height; h += 2) { 
    21.             int originalOffset = uvFinalColumnStartIndex + offset; 
    22.             *(rotateYuv420sp + rotatedVIndex) = *(yuv420sp + originalOffset); 
    23.             *(rotateYuv420sp + rotatedUIndex) = *(yuv420sp + originalOffset + 1); 
    24.             offset += width; 
    25.             rotatedVIndex += 2; 
    26.             rotatedUIndex += 2; 
    27.         } 
    28.     } 

四、旋转I420、YV12数据

对于I420、YV12数据,其排列是width * height个Y连续存储,接下来是连续的U和V或连续的V和U(I420是UUUUUUUU....VVVVVVVV....,YV12是VVVVVVVV....UUUUUUUU....),Y数据的大小刚好就是像素数,可以直接进行旋转;因为U和V的宽高都只有Y的宽高的一半,所以宽高的循环数各只有Y的一半(由于I420和YV12的U和V只是刚好位置相反,因此旋转I420的代码也同样适用于旋转YV12)。示例代码如下。

  • 旋转90
  1. void rotateYuv420pDegree90(char *yuv420p, char *rotateYuv420p, int width, int height) { 
  2.     int halfWidth = width / 2; 
  3.     int yFinalLineStartIndex = (height - 1) * width; 
  4.     int rotatedYIndex = 0
  5.     //rotate y 
  6.     for (int w = 0; w < width; w++) { 
  7.         int yStartIndex = yFinalLineStartIndex + w; 
  8.         int offset = 0
  9.         for (int h = 0; h < height; h++) { 
  10.             rotateYuv420p[rotatedYIndex++] = yuv420p[yStartIndex - offset]; 
  11.             offset += width; 
  12.         } 
  13.     } 
  14.     //rotate uv 
  15.     int uFinalLineStartIndex = width * height * 5 / 4 - halfWidth; 
  16.     int vFinalLineStartIndex = width * height * 3 / 2 - halfWidth; 
  17.     int rotatedUIndex = width * height; 
  18.     int rotatedVIndex = width * height * 5 / 4; 
  19.     for (int w = 0; w < width; w += 2) { 
  20.         int uStartIndex = uFinalLineStartIndex + w / 2; 
  21.         int vStartIndex = vFinalLineStartIndex + w / 2; 
  22.         int offset = 0
  23.         for (int h = 0; h < height; h += 2) { 
  24.             rotateYuv420p[rotatedUIndex++] = yuv420p[uStartIndex - offset]; 
  25.             rotateYuv420p[rotatedVIndex++] = yuv420p[vStartIndex - offset]; 
  26.             offset += halfWidth; 
  27.         } 
  28.     } 
  • 旋转180
  1. void rotateYuv420pDegree180(char *yuv420p, char *rotateYuv420p, int width, int height) { 
  2.     int yIndex = width * height - 1; 
  3.     int rotatedYIndex = 0
  4.     //rotate y 
  5.     for (int h = height - 1; h >= 0; h--) { 
  6.         for (int w = width - 1; w >= 0; w--) { 
  7.             rotateYuv420p[rotatedYIndex++] = yuv420p[yIndex]; 
  8.             yIndex--; 
  9.         } 
  10.     } 
  11.     int uIndex = width * height * 5 / 4 - 1; 
  12.     int vIndex = width * height * 3 / 2 - 1; 
  13.     int rotatedUIndex = width * height; 
  14.     int rotatedVIndex = width * height * 5 / 4; 
  15.     //rotate uv 
  16.     for (int h = height - 1; h >= 0; h -2) { 
  17.         for (int w = width - 1; w >= 0; w -2) { 
  18.             rotateYuv420p[rotatedUIndex++] = yuv420p[uIndex--]; 
  19.             rotateYuv420p[rotatedVIndex++] = yuv420p[vIndex--]; 
  20.         } 
  21.     } 
  • 旋转270
  1. void rotateYuv420pDegree270(char *yuv420p, char *rotateYuv420p, int width, int height) { 
  2.     int halfWidth = width / 2; 
  3.     int yFinalColumnStartIndex = width
  4.     int rotatedYIndex = 0
  5.     //rotate y 
  6.     for (int w = 0; w < width; w++) { 
  7.         int offset = 0
  8.         for (int h = 0; h < height; h++) { 
  9.             rotateYuv420p[rotatedYIndex++] = yuv420p[yFinalColumnStartIndex + offset]; 
  10.             offset += width; 
  11.         } 
  12.         yFinalColumnStartIndex--; 
  13.     } 
  14.     //rotate uv 
  15.     int uFinalColumnStartIndex = width * height + halfWidth; 
  16.     int vFinalColumnStartIndex = width * height * 5 / 4 + halfWidth; 
  17.     int rotatedUIndex = width * height; 
  18.     int rotatedVIndex = width * height * 5 / 4; 
  19.     for (int w = 0; w < width; w += 2) { 
  20.         uFinalColumnStartIndex--; 
  21.         vFinalColumnStartIndex--; 
  22.         int offset = 0
  23.         for (int h = 0; h < height; h += 2) { 
  24.             rotateYuv420p[rotatedUIndex++] = 
  25.                     yuv420p[uFinalColumnStartIndex + offset]; 
  26.             rotateYuv420p[rotatedVIndex++] = 
  27.                     yuv420p[vFinalColumnStartIndex + offset]; 
  28.             offset += halfWidth; 
  29.         } 
  30.     } 

五、 旋转YUYV数据

由于YUYV的排列方式是(YUYV YUYV YUYV ....),其共用关系是每2个横向相邻的Y会使用同一组U和V,因此,在旋转180度时,YUV的共用关系可以不被打破,只是更改每4个byte中的2个Y的顺序;但是在旋转90度或270度时,由于原来横向的Y将被修改为纵向,YUV的共用关系也将被打破。示例代码如下。

  • 旋转90
  1. void rotateYuyvDegree90(char *yuyv, char *rotatedYuyv, int width, int height) { 
  2.     int lineDataSize = width * 2; 
  3.     int rotatedLineDataSize = height * 2; 
  4.     int rotatedYuyvIndex = 0
  5.     int finalLineStartIndex = (height - 2) * lineDataSize; 
  6.     for (int w = 0; w < lineDataSize; w += 4) { 
  7.         int yuyvStartIndex = finalLineStartIndex + w; 
  8.         int offset = 0
  9.         for (int h = 0; h < height; h += 2) { 
  10.             /** 
  11.              * y1 u1 y2 v2   y3 u2 y4 v2 
  12.              *                              ->    旋转后的画面脑补下 
  13.              * y5 u3 y6 v3   y7 u4 y8 v4 
  14.              */ 
  15.             int originalOffset = yuyvStartIndex - offset; 
  16.             int originalNextLineOffset = yuyvStartIndex - offset + lineDataSize; 
  17.             int targetNextLineOffset = rotatedYuyvIndex + rotatedLineDataSize; 
  18.             //y5 
  19.             rotatedYuyv[rotatedYuyvIndex] = yuyv[originalNextLineOffset]; 
  20.             //u3 
  21.             rotatedYuyv[rotatedYuyvIndex + 1] = yuyv[originalNextLineOffset + 1]; 
  22.             //y1 
  23.             rotatedYuyv[rotatedYuyvIndex + 2] = yuyv[originalOffset]; 
  24.             //v3 
  25.             rotatedYuyv[rotatedYuyvIndex + 3] = yuyv[originalNextLineOffset + 3]; 
  26.  
  27.             //y6 
  28.             rotatedYuyv[targetNextLineOffset] = yuyv[originalNextLineOffset + 2]; 
  29.             //u1 
  30.             rotatedYuyv[targetNextLineOffset + 1] = yuyv[originalOffset + 1]; 
  31.             //y2 
  32.             rotatedYuyv[targetNextLineOffset + 2] = yuyv[originalOffset + 2]; 
  33.             //v2 
  34.             rotatedYuyv[targetNextLineOffset + 3] = yuyv[originalOffset + 3]; 
  35.  
  36.             rotatedYuyvIndex += 4; 
  37.             offset += lineDataSize * 2; 
  38.         } 
  39.         rotatedYuyvIndex += rotatedLineDataSize; 
  40.     } 
  • 旋转180
  1. void rotateYuyvDegree180(char *yuyv, char *rotatedYuyv, int width, int height) { 
  2.     int lineDataSize = width * 2; 
  3.     int yuyvIndex = lineDataSize * height - 4; 
  4.     int rotatedIndex = 0
  5.     //rotate 
  6.     for (int h = height - 1; h >= 0; h--) { 
  7.         for (int w = lineDataSize - 4; w >= 0; w -4) { 
  8.             rotatedYuyv[rotatedIndex++] = yuyv[yuyvIndex + 2]; 
  9.             rotatedYuyv[rotatedIndex++] = yuyv[yuyvIndex + 1]; 
  10.             rotatedYuyv[rotatedIndex++] = yuyv[yuyvIndex]; 
  11.             rotatedYuyv[rotatedIndex++] = yuyv[yuyvIndex + 3]; 
  12.             yuyvIndex -4
  13.         } 
  14.     } 
  • 旋转270
  1. void rotateYuyvDegree270(char *yuyv, char *rotatedYuyv, int width, int height) { 
  2.     int lineDataSize = width * 2; 
  3.     int rotatedLineDataSize = height * 2; 
  4.     int rotatedYuyvIndex = 0
  5.     int finalColumnStartIndex = lineDataSize - 4; 
  6.     for (int w = 0; w < lineDataSize; w += 4) { 
  7.         int offset = 0
  8.         for (int h = 0; h < height; h += 2) { 
  9.             /** 
  10.              * y1 u1 y2 v1   y3 u2 y4 v2 
  11.              *                              ->    旋转后的画面脑补下 
  12.              * y5 u3 y6 v3   y7 u4 y8 v4 
  13.              */ 
  14.  
  15.             int originalOffset = finalColumnStartIndex + offset; 
  16.             int originalNextLineOffset = finalColumnStartIndex + offset + lineDataSize; 
  17.             int targetNextLineOffset = rotatedYuyvIndex + rotatedLineDataSize; 
  18.             //y4 
  19.             rotatedYuyv[rotatedYuyvIndex] = yuyv[originalOffset + 2]; 
  20.             //u2 
  21.             rotatedYuyv[rotatedYuyvIndex + 1] = yuyv[originalOffset + 1]; 
  22.             //y8 
  23.             rotatedYuyv[rotatedYuyvIndex + 2] = yuyv[originalNextLineOffset + 2]; 
  24.             //v2 
  25.             rotatedYuyv[rotatedYuyvIndex + 3] = yuyv[originalOffset + 3]; 
  26.  
  27.             //y3 
  28.             rotatedYuyv[targetNextLineOffset] = yuyv[finalColumnStartIndex + offset]; 
  29.             //u4 
  30.             rotatedYuyv[targetNextLineOffset + 1] = yuyv[originalNextLineOffset + 1]; 
  31.             //y7 
  32.             rotatedYuyv[targetNextLineOffset + 2] = yuyv[originalNextLineOffset]; 
  33.             //v4 
  34.             rotatedYuyv[targetNextLineOffset + 3] = yuyv[originalNextLineOffset + 3]; 
  35.  
  36.             rotatedYuyvIndex += 4; 
  37.             offset += lineDataSize * 2; 
  38.         } 
  39.         finalColumnStartIndex -4
  40.         rotatedYuyvIndex += rotatedLineDataSize; 
  41.     } 

 

 

(省油的灯_wsy)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/202204/17866.html]
本文出处:简书 作者:省油的灯_wsy 原文
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容