YUV12(420) (from)to RGB24
直接上代码
#include <opencv2/opencv.hpp>
#include <stdio.h> #define min(a,b) ((a<b)?a:b)
#define max(a,b) ((a>b)?a:b)
/************************************************************************/
/* YUV12 to RGB24 (4) Basic Optimizations 3.1 */
/************************************************************************/
int yuv420_to_argb888(const unsigned char *y, const unsigned char *u,const unsigned char *v ,int width ,int height ,unsigned char *rgb)
{
static const int Precision = 32768 ;
static const int Coefficient_y = (int) (1.164*Precision + 0.5);
static const int Coefficient_rv = (int) (1.596*Precision + 0.5);
static const int Coefficient_gu = (int) (0.391*Precision + 0.5);
static const int Coefficient_gv = (int) (0.813*Precision + 0.5);
static const int Coefficient_bu = (int) (2.018*Precision + 0.5);
static int CoefficientY[256];
static int CoefficientRV[256];
static int CoefficientGU[256];
static int CoefficientGV[256];
static int CoefficientBU[256];
static int _CoefficientsR[1024];
//static int _CoefficientsG[1024];
//static int _CoefficientsB[1024];
static int flag = 1 ; if (flag)
{ for(int i =0;i<256;i++)
{
CoefficientY[i] = Coefficient_y *(i - 16) + (Precision/2);
CoefficientGV[i] = -Coefficient_gv *(i - 128);
CoefficientBU[i] = Coefficient_bu *(i - 128);
CoefficientGU[i] = -Coefficient_gu *(i - 128);
CoefficientRV[i] = Coefficient_rv *(i - 128);
} for(int j=0;j<1024;j++)
{
_CoefficientsR[j] = min((max(j-320,0)),255) ;
//_CoefficientsG[j] = min((max(j-320,0)),255) ;
//_CoefficientsB[j] = min((max(j-320,0)),255) ;
} flag = 0;
}
CoefficientY[0] = -593888;
CoefficientY[1] = -555746; //修复bug!! CoefficientY[1]在第二次进入此函数的时候意外被改动为非常大的数,理论值应该为-555746
int *CoefficientsR = &_CoefficientsR[320];
// int *CoefficientsG = &_CoefficientsG[320];
// int *CoefficientsB = &_CoefficientsB[320]; for ( int h=0;h<height;h++)
{ for (int w=0;w<width;w++)
{
int k = h*width + w;
int index = k*3;
int i = (h/2)*(width/2)+(w/2);
int Y = y[k];
int U = u[i];
int V = v[i]; //3.3 Optimizations Removing Conditional Tests
int r = CoefficientY[Y] + CoefficientRV[V];
int g = CoefficientY[Y] + CoefficientGU[U]+ CoefficientGV[V];
int b = CoefficientY[Y] + CoefficientBU[U];
rgb[index] = CoefficientsR[r/Precision];
rgb[index+1] = CoefficientsR[g/Precision];
rgb[index+2] = CoefficientsR[b/Precision]; }
}
return 0;
}
//Compare 3 images histograms together,
// the first is divided in half along y to test its other half
// Call is:
// ch7HistCmp modelImage0 testImage1 testImage2 badImage3
// Note that the model image is split in half. Top half(0) makes model. It's then tested
// against its lower half(0), testImages 1 and 2 in different lighting and different object 3
//
int main( int argc, char** argv ) { IplImage* src[5], *tmp;
int i;
if((src[0] = cvLoadImage(argv[1], 1)) == 0){ //We're going to split this one in half
printf("Error on reading image 1, %s\n",argv[1]);
return(-1);
}
//Parse the first image into two image halves divided halfway on y
printf("Getting size [[%d] [%d]] format is [%s]\n",src[0]->width,src[0]->height,src[0]->channelSeq);
CvSize size = cvGetSize(src[0]);
printf("Get size %d %d\n",size.width,size.height);
int width = size.width;
int height = size.height;
int halfheight = height >> 1;
//RGB888 to YUV420
unsigned char * rgb = (unsigned char*)src[0]->imageData;
unsigned char * yuv = (unsigned char*)malloc(width*height + width*height/2);
unsigned char * y = yuv;
unsigned char * u = &yuv[width*height];
unsigned char * v = &yuv[width*height+width*height/4]; int k = 0;
for(int i = 0;i<height ;i++)
{
for(int j =0;j<width;j++)
{
int index = i*width+j;
unsigned char R = rgb[width*i*3+j*3];
unsigned char G = rgb[width*i*3+j*3+1];
unsigned char B = rgb[width*i*3+j*3+2];
y[index] = ( ( 66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
y[index] = min(max(y[index],0),255); if( (j%2 ==0)&&(i%2 == 0) )
{
k++;
u[k] = ( ( -38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
v[k] = ( ( 112 * R - 94 * G - 18 * B + 128) >> 8) + 128;
u[k] = min(max(u[k],0),255);
v[k] = min(max(v[k],0),255);
}
}
} src[1] = cvCreateImage(cvSize(width,height), 8, 3);
yuv420_to_argb888((const unsigned char*)y,(const unsigned char*)u,(const unsigned char*)v,width,height,(unsigned char *)src[1]->imageData); //DISPLAY
cvNamedWindow( "Source0", 1 );
cvShowImage( "Source0", src[0] );//原图RGB
cvNamedWindow( "Source1", 1 );
cvShowImage( "Source1", src[1] );//经过RGB88->YUV420->RGB888后的图像 cvWaitKey(0);
free(yuv) }
YUV12(420) (from)to RGB24的更多相关文章
- linux之x86裁剪移植---ffmpeg的H264解码显示(420、422)
在虚拟机上yuv420可以正常显示 ,而945(D525)模块上却无法显示 ,后来验证了directdraw的yuv420也无法显示 ,由此怀疑显卡不支持 ,后把420转换为422显示. 420显示如 ...
- etlpy: 并行爬虫和数据清洗工具(开源)
etlpy是python编写的网页数据抓取和清洗工具,核心文件etl.py不超过500行,具备如下特点 爬虫和清洗逻辑基于xml定义,不需手工编写 基于python生成器,流式处理,对内存无要求 内置 ...
- Kafka 0.9+Zookeeper3.4.6集群搭建、配置,新Client API的使用要点,高可用性测试,以及各种坑 (转载)
Kafka 0.9版本对java client的api做出了较大调整,本文主要总结了Kafka 0.9在集群搭建.高可用性.新API方面的相关过程和细节,以及本人在安装调试过程中踩出的各种坑. 关于K ...
- 【position也可以很复杂】当弹出层遇上了鼠标定位(下)
前言 接着昨天的内容写,为了保证内容连续性,这里还是把昨天的内容拷了过来. 请用现代浏览器测试 引出问题 有图有真相,我们来看一个智联招聘里面经常出现的图层: 他这个是没有什么问题的,我们来简单看看其 ...
- (转)Rest介绍
参考文献:Rest简介 REST是一种组织Web服务的架构,其只在架构方面提出了一系列约束. 关于Restful的无状态 所以在stackoverflow中,我们常常会看到有人问:我现在使用了这样一种 ...
- OCJP(1Z0-851) 模拟题分析(一)11
Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有 ...
- 《利用python进行数据分析》读书笔记--第十一章 金融和经济数据应用(一)
自2005年开始,python在金融行业中的应用越来越多,这主要得益于越来越成熟的函数库(NumPy和pandas)以及大量经验丰富的程序员.许多机构发现python不仅非常适合成为交互式的分析环境, ...
- Python_sklearn机器学习库学习笔记(三)logistic regression(逻辑回归)
# 逻辑回归 ## 逻辑回归处理二元分类 %matplotlib inline import matplotlib.pyplot as plt #显示中文 from matplotlib.font_m ...
- 建模算法(八)——插值
插值:求过已知有限个数据点的近似函数 拟合:已知有限个数据点,求近似函数,不要求过已知数据点,只要求在某种意义下在这些点的误差最小 (一)插值方法 一.拉格朗日多项式插值 1.插值多项式 就是做出一个 ...
随机推荐
- 我在一个前端项目中用js整理的一些通用方法,其中使用到的思想,主要就是约定了。
把名称和后台来的json数据约定起来,可以达到的效果就是可以将东西统一化,减少差异,提升模块等的通用性,此后就可以实现具体不同模块内容可以自动或拷贝赋值的方式 2016.7.18 refactor s ...
- Python 本地线程
1. 本地线程,保证即使是多个线程,自己的值也是互相隔离. 2.普通对象演示 import threading import time class A(): pass a=A() def func(n ...
- IPsec传输模式下ESP报文的装包和拆包过程
原创文章,拒绝转载 装包过程 总体流程图 过程描述 在原IP报文中找到TCP报文部分,在其后添加相应的ESP trailer信息. ESP trailer 包含三部分:Padding,Pad leng ...
- Swift中的类型属性(静态变量)
http://blog.haohtml.com/archives/15098 Swift中的类型属性(静态变量) Posted on 2014/06/13 类型属性语法 在 C 或 Objective ...
- Django组件之contenttype的应用
contenttypes 是Django内置的一个应用,可以追踪项目中所有app和model的对应关系,并记录在ContentType表中. 每当我们创建了新的model并执行数据库迁移后,Conte ...
- hdu 1364(差分约束)
King Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12056 Accepted: 4397 Description ...
- Mysql优化之——启用查询缓存
启用MySQL查询缓存可以极大地减低数据库服务器的CPU使用率,实际使用情况是:开启前CPU使用率120%左右,开启后降到了10%. 查看查询缓存情况: mysql> show variable ...
- codeforces 671C
题意定义f(l,r)为去掉[l,r]部分后剩下的数任意两个数的最大公约数的最大值 现在求f(l,r)的和 由于每个数ai最大只有200000,因此我们穷举因子x,记录以其为因子的a[i]的i值并按i升 ...
- STL模板整理 priority_queue
priority_queue 优先队列是队列的一种,不过它可以按照自定义的一种方式(数据的优先级)来对队列中的数据进行动态的排序,每次的push和pop操作,队列都会动态的调整,以达到我们预期的方式来 ...
- CF 1005B Delete from the Left 【模拟数组操作/正难则反】
You are given two strings s and t. In a single move, you can choose any of two strings and delete th ...