YUV420图像旋转90算法的优化
在做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;
}
编译执行,捕获视频显示很流畅 :)
YUV420图像旋转90算法的优化的更多相关文章
- 不占用额外内存空间能否做到 将图像旋转90度 N × N矩阵表示的图像,其中每个像素的大小为4字节
给定一幅由N × N矩阵表示的图像,其中每个像素的大小为4字节,编写一种方法,将图像旋转90度. 不占用额外内存空间能否做到? 示例 1: 给定 matrix = [ [1,2,3], [4,5,6] ...
- EasyPusher手机直播编码推送之图像旋转90度后画面重复的问题
本文转自EasyDarwin开源团队开发Holo的博客:http://blog.csdn.net/holo_easydarwin 最初在做EasyPusher手机直播的时候遇到过一个问题:手机竖屏推送 ...
- EasyPusher手机直播图像旋转90度后画面重复的问题
本文转自:http://blog.csdn.net/holo_easydarwin/article/details/51147379 最初在做EasyPusher手机直播的时候遇到过一个问题:手机竖屏 ...
- 通过transpose和flip实现图像旋转90/180/270度
在fbc_cv库中,提供了对图像进行任意角度旋转的函数rotate,其实内部也是调用了仿射变换函数warpAffine.如果图像仅是进行90度倍数的旋转,是没有必要用warpAffine函数的.这里通 ...
- [google面试CTCI] 1-6.图像旋转问题
[字符串与数组] Q:Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, wr ...
- python-Day4-迭代器-yield异步处理--装饰器--斐波那契--递归--二分算法--二维数组旋转90度--正则表达式
本节大纲 迭代器&生成器 装饰器 基本装饰器 多参数装饰器 递归 算法基础:二分查找.二维数组转换 正则表达式 常用模块学习 作业:计算器开发 实现加减乘除及拓号优先级解析 用户输入 1 - ...
- 每日算法37:Rotate Image (图像旋转)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- 基于均值坐标(Mean-Value Coordinates)的图像融合算法的优化实现
目录 1. 概述 2. 实现 2.1. 原理 2.2. 核心代码 2.3. 第二种优化 3. 结果 1. 概述 我在之前的文章<基于均值坐标(Mean-Value Coordinates)的图像 ...
- PyOpenCV图像逆时针旋转90度
warpAffine方法效果很搓,留下大片黑色区域. 使用flip和transpose可以实现逆时针旋转90度.先flip或先transpose均可. #coding:utf-8 import cv2 ...
随机推荐
- 转:javascript获取上一访问页面
原文链接:移动端返回上一页,刚需!document.referrer 详解 全文如下: 返回上一页,在PC端我们可以使用:history.go(-1)或者history.back(),可以正常返回第一 ...
- CSS属性display的浅略探讨
display 的属性值有:none|inline|block|inline-block|list-item|run-in|table|inline-table|table-row-group|tab ...
- <Android 基础(二十八)> Fragment (1)
简介 Fragment,碎片,常用的内容,但是一直没有系统的学习下它的使用方法,花几天抽空看看随便记录一下. 生命周期 来自官网的图片一目了然. 自测试结果: 基本使用 1.自定义一个Fragment ...
- 从源码上分析ListView的addHeaderView和setAdapter的调用顺序
ListView想要添加headerview的话,就要通过addHeaderView这个方法,然后想要为ListView设置数据的话,就要调用setAdapter方法了.但是,在调用addHeader ...
- docker的网络基础配置
一.端口映射实现访问容器 当容器中运行一些网络应用,要让外部访问这些应用时,可以通过-P或-p参数来指定端口映射.当使用-P标记时,Docker会随机映射一个49000~49900的端口至容器内部开放 ...
- MYSQL 5.7 sqlmode 行为
最近碰到了sql_mode 的一些问题,故进行了研究,根据实际情况研究其行为. sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ER ...
- Vue2学习笔记:v-model指令
1.v-model指令 <!DOCTYPE html> <html> <head> <title></title> <script s ...
- 【Kettle】4、SQL SERVER到SQL SERVER数据转换抽取实例
1.系统版本信息 System:Windows旗舰版 Service Pack1 Kettle版本:6.1.0.1-196 JDK版本:1.8.0_72 2.连接数据库 本次实例连接数据库时使用全局变 ...
- [SQLServer] 内存占用查看
SELECT (physical_memory_in_use_kb/1024) AS Memory_usedby_Sqlserver_MB, (locked_page_allocations_kb/1 ...
- POI导出excel,本地测试没问题,linux测试无法导出
java.lang.RuntimeException: java.io.IOException: No such file or directory at org.apache.poi. ...