您的位置:首页 > 其它

YUV420图像旋转90算法的优化

2014-04-25 14:37 316 查看
在做android摄像头捕获时,发现从android摄像头出来的原始视是逆时针旋转了90度的,所以需要把它顺时针旋转90。android视频支持的是NV21格式,它是一种YUV420的格式。当然,始果你用的是android sdk的话,其中image就提供这个能力。但是我是在ndk下开发,没有找到相应的功能(如果你知道请告诉我)。

我本想用开源的图像处理库(opencv)做旋转,但是opencv只能处理bmp的图像。这样的话,需要先把NV21转换成BMP32。然后再做旋转。所以要操作两次,效率肯定低。最后也没找到好的方法(如果你知道一次用opencv做YUV420格式的旋转,请你告诉我)。所以只有自己写一个。

先从网上搜索了一个:

void CTool::YUV420spRotate90(uchar *des, uchar *src,int width,int height)
{
int wh = width * height;
//旋转Y
int k = 0;
for(int i = 0; i < width; i++) {
for(int j = 0; j < height; j++)
{
des[k] = src[width * j + i];
k++;
}
}

for(int i = 0; i < width; i += 2) {
for(int j = 0; j < height / 2; j++)
{
des[k] = src[wh+ width * j + i];
des[k+1] = src[wh + width * j + i + 1];
k+=2;
}
}
}


在android上运行了一下,发现视频非常卡。所以只有自己优化了。这段代码里有乘法,优化的重点就是去掉乘法用加法代替:

void CTool::YUV420spRotate90(uchar *dst, const uchar *src, int srcWidth, int srcHeight)
{
static int nWidth = 0, nHeight = 0;
static int wh = 0;
static int uvHeight = 0;
if(srcWidth != nWidth || srcHeight != nHeight)
{
nWidth = srcWidth;
nHeight = srcHeight;
wh = srcWidth * srcHeight;
uvHeight = srcHeight >> 1;//uvHeight = height / 2
}

//旋转Y
int k = 0;
for(int i = 0; i < srcWidth; i++) {
int nPos = 0;
for(int j = 0; j < srcHeight; j++) {
dst[k] = src[nPos + i];
k++;
nPos += srcWidth;
}
}

for(int i = 0; i < srcWidth; i+=2){
int nPos = wh;
for(int j = 0; j < uvHeight; j++) {
dst[k] = src[nPos + i];
dst[k + 1] = src[nPos + i + 1];
k += 2;
nPos += srcWidth;
}
}
return;
}


逆时针旋转90度:

void CTool::YUV420spRotateNegative90(uchar *dst, const uchar *src, int srcWidth, int height)
{
static int nWidth = 0, nHeight = 0;
static int wh = 0;
static int uvHeight = 0;
if(srcWidth != nWidth || height != nHeight)
{
nWidth = srcWidth;
nHeight = height;
wh = srcWidth * height;
uvHeight = height >> 1;//uvHeight = height / 2
}

//旋转Y
int k = 0;
for(int i = 0; i < srcWidth; i++){
int nPos = srcWidth - 1;
for(int j = 0; j < height; j++)
{
dst[k] = src[nPos - i];
k++;
nPos += srcWidth;
}
}

for(int i = 0; i < srcWidth; i+=2){
int nPos = wh + srcWidth - 1;
for(int j = 0; j < uvHeight; j++) {
dst[k] = src[nPos - i - 1];
dst[k + 1] = src[nPos - i];
k += 2;
nPos += srcWidth;
}
}

return;
}


编译运行,捕获视频显示非常流畅 :)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: