FFmpeg中AVFrame到OpenCV中Mat的两种转换方法

方法一:查表法

void AVFrame2Img(AVFrame *pFrame, cv::Mat& img)
{
int frameHeight = pFrame->height;
int frameWidth = pFrame->width;
int channels = 3;
//输出图像分配内存
img = cv::Mat::zeros(frameHeight, frameWidth, CV_8UC3);
//反转图像
pFrame->data[0] += pFrame->linesize[0] * (frameHeight - 1);
pFrame->linesize[0] *= -1;
pFrame->data[1] += pFrame->linesize[1] * (frameHeight / 2 - 1);
pFrame->linesize[1] *= -1;
pFrame->data[2] += pFrame->linesize[2] * (frameHeight / 2 - 1);
pFrame->linesize[2] *= -1; //创建保存yuv数据的buffer
uchar* pDecodedBuffer = (uchar*)malloc(frameHeight*frameWidth * sizeof(uchar)*channels); //从AVFrame中获取yuv420p数据,并保存到buffer
int i, j, k;
//拷贝y分量
for (i = 0; i < frameHeight; i++)
{
memcpy(pDecodedBuffer + frameWidth*i,
pFrame->data[0] + pFrame->linesize[0] * i,
frameWidth);
}
//拷贝u分量
for (j = 0; j < frameHeight / 2; j++)
{
memcpy(pDecodedBuffer + frameWidth*i + frameWidth / 2 * j,
pFrame->data[1] + pFrame->linesize[1] * j,
frameWidth / 2);
}
//拷贝v分量
for (k = 0; k < frameHeight / 2; k++)
{
memcpy(pDecodedBuffer + frameWidth*i + frameWidth / 2 * j + frameWidth / 2 * k,
pFrame->data[2] + pFrame->linesize[2] * k,
frameWidth / 2);
} //将buffer中的yuv420p数据转换为RGB;
YUV2RGB(pDecodedBuffer, frameWidth, frameHeight, channels, img.data); //释放buffer
free(pDecodedBuffer);
}

子函数YUV2RGB( )实现yuv420p到RGB的转换

bool YUV2RGB(uchar* pYuvBuf, int nWidth, int nHeight, int channels, uchar* pRgbBuf)
{
#define PIXELSIZE nWidth * nHeight
const int Table_fv1[256] = { -180, -179, -177, -176, -174, -173, -172, -170, -169, -167, -166, -165, -163, -162, -160, -159, -158, -156, -155, -153, -152, -151, -149, -148, -146, -145, -144, -142, -141, -139, -138, -137, -135, -134, -132, -131, -130, -128, -127, -125, -124, -123, -121, -120, -118, -117, -115, -114, -113, -111, -110, -108, -107, -106, -104, -103, -101, -100, -99, -97, -96, -94, -93, -92, -90, -89, -87, -86, -85, -83, -82, -80, -79, -78, -76, -75, -73, -72, -71, -69, -68, -66, -65, -64, -62, -61, -59, -58, -57, -55, -54, -52, -51, -50, -48, -47, -45, -44, -43, -41, -40, -38, -37, -36, -34, -33, -31, -30, -29, -27, -26, -24, -23, -22, -20, -19, -17, -16, -15, -13, -12, -10, -9, -8, -6, -5, -3, -2, 0, 1, 2, 4, 5, 7, 8, 9, 11, 12, 14, 15, 16, 18, 19, 21, 22, 23, 25, 26, 28, 29, 30, 32, 33, 35, 36, 37, 39, 40, 42, 43, 44, 46, 47, 49, 50, 51, 53, 54, 56, 57, 58, 60, 61, 63, 64, 65, 67, 68, 70, 71, 72, 74, 75, 77, 78, 79, 81, 82, 84, 85, 86, 88, 89, 91, 92, 93, 95, 96, 98, 99, 100, 102, 103, 105, 106, 107, 109, 110, 112, 113, 114, 116, 117, 119, 120, 122, 123, 124, 126, 127, 129, 130, 131, 133, 134, 136, 137, 138, 140, 141, 143, 144, 145, 147, 148, 150, 151, 152, 154, 155, 157, 158, 159, 161, 162, 164, 165, 166, 168, 169, 171, 172, 173, 175, 176, 178 };
const int Table_fv2[256] = { -92, -91, -91, -90, -89, -88, -88, -87, -86, -86, -85, -84, -83, -83, -82, -81, -81, -80, -79, -78, -78, -77, -76, -76, -75, -74, -73, -73, -72, -71, -71, -70, -69, -68, -68, -67, -66, -66, -65, -64, -63, -63, -62, -61, -61, -60, -59, -58, -58, -57, -56, -56, -55, -54, -53, -53, -52, -51, -51, -50, -49, -48, -48, -47, -46, -46, -45, -44, -43, -43, -42, -41, -41, -40, -39, -38, -38, -37, -36, -36, -35, -34, -33, -33, -32, -31, -31, -30, -29, -28, -28, -27, -26, -26, -25, -24, -23, -23, -22, -21, -21, -20, -19, -18, -18, -17, -16, -16, -15, -14, -13, -13, -12, -11, -11, -10, -9, -8, -8, -7, -6, -6, -5, -4, -3, -3, -2, -1, 0, 0, 1, 2, 2, 3, 4, 5, 5, 6, 7, 7, 8, 9, 10, 10, 11, 12, 12, 13, 14, 15, 15, 16, 17, 17, 18, 19, 20, 20, 21, 22, 22, 23, 24, 25, 25, 26, 27, 27, 28, 29, 30, 30, 31, 32, 32, 33, 34, 35, 35, 36, 37, 37, 38, 39, 40, 40, 41, 42, 42, 43, 44, 45, 45, 46, 47, 47, 48, 49, 50, 50, 51, 52, 52, 53, 54, 55, 55, 56, 57, 57, 58, 59, 60, 60, 61, 62, 62, 63, 64, 65, 65, 66, 67, 67, 68, 69, 70, 70, 71, 72, 72, 73, 74, 75, 75, 76, 77, 77, 78, 79, 80, 80, 81, 82, 82, 83, 84, 85, 85, 86, 87, 87, 88, 89, 90, 90 };
const int Table_fu1[256] = { -44, -44, -44, -43, -43, -43, -42, -42, -42, -41, -41, -41, -40, -40, -40, -39, -39, -39, -38, -38, -38, -37, -37, -37, -36, -36, -36, -35, -35, -35, -34, -34, -33, -33, -33, -32, -32, -32, -31, -31, -31, -30, -30, -30, -29, -29, -29, -28, -28, -28, -27, -27, -27, -26, -26, -26, -25, -25, -25, -24, -24, -24, -23, -23, -22, -22, -22, -21, -21, -21, -20, -20, -20, -19, -19, -19, -18, -18, -18, -17, -17, -17, -16, -16, -16, -15, -15, -15, -14, -14, -14, -13, -13, -13, -12, -12, -11, -11, -11, -10, -10, -10, -9, -9, -9, -8, -8, -8, -7, -7, -7, -6, -6, -6, -5, -5, -5, -4, -4, -4, -3, -3, -3, -2, -2, -2, -1, -1, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 20, 20, 20, 21, 21, 22, 22, 22, 23, 23, 23, 24, 24, 24, 25, 25, 25, 26, 26, 26, 27, 27, 27, 28, 28, 28, 29, 29, 29, 30, 30, 30, 31, 31, 31, 32, 32, 33, 33, 33, 34, 34, 34, 35, 35, 35, 36, 36, 36, 37, 37, 37, 38, 38, 38, 39, 39, 39, 40, 40, 40, 41, 41, 41, 42, 42, 42, 43, 43 };
const int Table_fu2[256] = { -227, -226, -224, -222, -220, -219, -217, -215, -213, -212, -210, -208, -206, -204, -203, -201, -199, -197, -196, -194, -192, -190, -188, -187, -185, -183, -181, -180, -178, -176, -174, -173, -171, -169, -167, -165, -164, -162, -160, -158, -157, -155, -153, -151, -149, -148, -146, -144, -142, -141, -139, -137, -135, -134, -132, -130, -128, -126, -125, -123, -121, -119, -118, -116, -114, -112, -110, -109, -107, -105, -103, -102, -100, -98, -96, -94, -93, -91, -89, -87, -86, -84, -82, -80, -79, -77, -75, -73, -71, -70, -68, -66, -64, -63, -61, -59, -57, -55, -54, -52, -50, -48, -47, -45, -43, -41, -40, -38, -36, -34, -32, -31, -29, -27, -25, -24, -22, -20, -18, -16, -15, -13, -11, -9, -8, -6, -4, -2, 0, 1, 3, 5, 7, 8, 10, 12, 14, 15, 17, 19, 21, 23, 24, 26, 28, 30, 31, 33, 35, 37, 39, 40, 42, 44, 46, 47, 49, 51, 53, 54, 56, 58, 60, 62, 63, 65, 67, 69, 70, 72, 74, 76, 78, 79, 81, 83, 85, 86, 88, 90, 92, 93, 95, 97, 99, 101, 102, 104, 106, 108, 109, 111, 113, 115, 117, 118, 120, 122, 124, 125, 127, 129, 131, 133, 134, 136, 138, 140, 141, 143, 145, 147, 148, 150, 152, 154, 156, 157, 159, 161, 163, 164, 166, 168, 170, 172, 173, 175, 177, 179, 180, 182, 184, 186, 187, 189, 191, 193, 195, 196, 198, 200, 202, 203, 205, 207, 209, 211, 212, 214, 216, 218, 219, 221, 223, 225 };
int len = channels * nWidth * nHeight;
if (!pYuvBuf || !pRgbBuf)
return false;
const long nYLen = long(PIXELSIZE);
const int nHfWidth = (nWidth >> 1);
if (nYLen<1 || nHfWidth<1)
return false;
// Y data
unsigned char* yData = pYuvBuf;
// v data
unsigned char* vData = &yData[nYLen];
// u data
unsigned char* uData = &vData[nYLen >> 2];
if (!uData || !vData)
return false;
int rgb[3];
int i, j, m, n, x, y, pu, pv, py, rdif, invgdif, bdif;
m = -nWidth;
n = -nHfWidth;
bool addhalf = true;
for (y = 0; y<nHeight; y++) {
m += nWidth;
if (addhalf) {
n += nHfWidth;
addhalf = false;
}
else {
addhalf = true;
}
for (x = 0; x<nWidth; x++) {
i = m + x;
j = n + (x >> 1);
py = yData[i];
// search tables to get rdif invgdif and bidif
rdif = Table_fv1[vData[j]]; // fv1
invgdif = Table_fu1[uData[j]] + Table_fv2[vData[j]]; // fu1+fv2
bdif = Table_fu2[uData[j]]; // fu2
rgb[0] = py + rdif; // R
rgb[1] = py - invgdif; // G
rgb[2] = py + bdif; // B
j = nYLen - nWidth - m + x;
i = (j << 1) + j;
// copy this pixel to rgb data
for (j = 0; j<3; j++)
{
if (rgb[j] >= 0 && rgb[j] <= 255) {
pRgbBuf[i + j] = rgb[j];
}
else {
pRgbBuf[i + j] = (rgb[j] < 0) ? 0 : 255;
}
}
}
}
return true;
}

方法二逐像素转换法:

void AVFrame2Img(AVFrame *pFrame, cv::Mat& img)
{
int frameHeight = pFrame->height;
int frameWidth = pFrame->width;
int channels = ;
//输出图像分配内存
img = cv::Mat::zeros(frameHeight, frameWidth, CV_8UC3); //创建保存yuv数据的buffer
uchar* pDecodedBuffer = (uchar*)malloc(frameHeight*frameWidth * sizeof(uchar)*channels); //从AVFrame中获取yuv420p数据,并保存到buffer
int i, j, k;
//拷贝y分量
for (i = ; i < frameHeight; i++)
{
memcpy(pDecodedBuffer + frameWidth*i,
pFrame->data[] + pFrame->linesize[] * i,
frameWidth);
}
//拷贝u分量
for (j = ; j < frameHeight / ; j++)
{
memcpy(pDecodedBuffer + frameWidth*i + frameWidth / * j,
pFrame->data[] + pFrame->linesize[] * j,
frameWidth / );
}
//拷贝v分量
for (k = ; k < frameHeight / ; k++)
{
memcpy(pDecodedBuffer + frameWidth*i + frameWidth / * j + frameWidth / * k,
pFrame->data[] + pFrame->linesize[] * k,
frameWidth / );
} //将buffer中的yuv420p数据转换为RGB;
Yuv420p2Rgb32(pDecodedBuffer, img.data, frameWidth, frameHeight); //释放buffer
free(pDecodedBuffer);
}

子函数Yuv420p2Rgb32( )函数实现yuv420p到RGB32的转换

void Yuv420p2Rgb32(const uchar *yuvBuffer_in, const uchar *rgbBuffer_out, int width, int height)
{
uchar *yuvBuffer = (uchar *)yuvBuffer_in;
uchar *rgb32Buffer = (uchar *)rgbBuffer_out; int channels = ; for (int y = ; y < height; y++)
{
for (int x = ; x < width; x++)
{
int index = y * width + x; int indexY = y * width + x;
int indexU = width * height + y / * width / + x / ;
int indexV = width * height + width * height / + y / * width / + x / ; uchar Y = yuvBuffer[indexY];
uchar U = yuvBuffer[indexU];
uchar V = yuvBuffer[indexV]; int R = Y + 1.402 * (V - );
int G = Y - 0.34413 * (U - ) - 0.71414*(V - );
int B = Y + 1.772*(U - );
R = (R < ) ? : R;
G = (G < ) ? : G;
B = (B < ) ? : B;
R = (R > ) ? : R;
G = (G > ) ? : G;
B = (B > ) ? : B; rgb32Buffer[(y*width + x)*channels + ] = uchar(R);
rgb32Buffer[(y*width + x)*channels + ] = uchar(G);
rgb32Buffer[(y*width + x)*channels + ] = uchar(B);
}
}
}

AVFrame转换到Mat,yuv420p转换到RGB源代码的更多相关文章

  1. bgcolor RGB 和16进制之间的转换,16进制转RGB,源码

    <p>bgcolor RGB 和16进制之间的转换,16进制转RGB,源码例如:<br /> 输入 201,255,201 转换成 #C9FFC9</p> < ...

  2. 实现同时将一批.bmp文件转换成.mat格式

    %% 功能:实现同时对一批.bmp文件的转换成.mat格式PicFormat = {'*.bmp','Bitmap image (*.bmp)';... '*.jpg','JPEG image (*. ...

  3. C# 使用ffmpeg.exe进行音频转换完整demo-asp.net转换代码

    C# 使用ffmpeg.exe进行音频转换完整demo-asp.net转换代码 上一篇说了在winform下进行调用cmd.exe执行ffmpeg.exe进行音频转换完整demo.后来我又需要移植这个 ...

  4. C# 字符串string类型转换成DateTime类型 或者 string转换成DateTime?(字符串转换成可空日期类型)

    在c#中,string类型转换成DateTime类型是经常用到的,作为基本的知识,这里在此做个小结.一般来说可以使用多种方法进行转换,最常用的就是使用Convert.ToDateTime(string ...

  5. json 字符串转换成对象,对象转换成json字符串

    json   字符串转换成对象,对象转换成json字符串 前端: 方法一: parseJSON方法:   [注意jquery版本问题] var str = '{"name":&qu ...

  6. 利用boost做string到wstring转换,以及字符集转换 - Error - C++博客

    利用boost做string到wstring转换,以及字符集转换 - Error - C++博客 利用boost做string到wstring转换,以及字符集转换 #include <boost ...

  7. 最全面的Java字节byte操作,处理Java基本数据的转换及进制转换操作工具,流媒体及java底层开发项目常用工具类

    前言:用于处理Java基本数据的转换及进制转换操作工具 一.实现功能 1.int与byte互转 2.int与byte[]互转 3.short与byte互转 4.short与byte[]互转 5.16位 ...

  8. SQLserver 字符串转换日期,日期转换成为字符串

    sqlserver 日期与字符串之间的转换   该文章摘自:http://www.cnblogs.com/windphoenix/archive/2013/04/26/3044784.html 字符转 ...

  9. 网络图片转换到本地并转换成base64位

    /** * 网络图片转换到本地并转换成base64位 * @param $url * @return string */ public function imgzhuanhuan($url) { // ...

随机推荐

  1. Swift3中数组创建方法

    转载自:http://blog.csdn.net/bwf_erg/article/details/70858865 数组是由一组类型相同的元素构成的有序数据集合.数组中的集合元素是有 序的,而且可以重 ...

  2. AFNetWorking 对汉字部分UTF-8编码

    随笔记一下 好用的小技巧 1.将字典数据拼接成url的参数... AFQueryStringFromParameters NSString *query = AFQueryStringFromPara ...

  3. 为什么很多第三方接口,都改成了基于http,直接传递json数据的方式来代替webservice?

    这实际上是三个问题,从WebService到今天流行的RESTful API(JSON) over HTTP,经历了数次变革 1 WebService有很多协议,为什么HTTP比较流行? WebSer ...

  4. 微信小程序开发之详解生命周期方法

    生命周期是指一个小程序从创建到销毁的一系列过程 在小程序中 ,通过App()来注册一个小程序 ,通过Page()来注册一个页面 先来看一张小程序项目结构 从上图可以看出,根目录下面有包含了app.js ...

  5. Neo4j学习笔记(2)——数据索引

    和关系数据库一样,Neo4j同样可以创建索引来加快查找速度. 在关系数据库中创建索引需要索引字段和指向记录的指针,通过索引可以快速查找到表中的行. 在Neo4j中,其索引是通过属性来创建,便于快速查找 ...

  6. ssh整合开发

    ssh整合思想 ssh整合 第一步:导入ssh相关jar包 第二步:搭建struts环境   (1)创建 action  struts.xml配置文件, 配置action struts.xml约束 & ...

  7. spring中Bean后置处理器实现总结

    BeanPostProcessor接口 bean的后置处理器实现功能主要是 可以在bean初始化之前和之后做增强处理.自定义MyBeanProcessor实现BeanPostProcessor接口,重 ...

  8. 一秒搞定mysql的远程登录

    执行下面命令! mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'mypw' WITH GRANT OPTION ; ...

  9. CentOS修改系统时间

    CentOS修改系统时间 操作: 1. date –s '1987-05-02 10:10:10' 2. clock –w //将日期写入CMOS 补充: 修改Linux时间一般涉及到3个命令: 1. ...

  10. Mac操作系统下忘记MYSQL的密码

    1. 在系统偏好 中,中止MySQL服务.: 2. cd/usr/local/mysql/bin   sudo ./mysqld_safe --skip-grant-tables 3. 登录MySQL ...