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.插值多项式 就是做出一个 ...
随机推荐
- 2017年上海金马五校程序设计竞赛:Problem C : Count the Number (模拟)
Description Given n numbers, your task is to insert '+' or '-' in front of each number to construct ...
- 标签 JLable 类
标签JLable上可以添加图像,当鼠标停留在标签上时,可以显示一段提示文字. package first; import javax.swing.*; import java.awt.*; impor ...
- wiki1285
2013-09-21 16:50 裸 //By BLADEVIL var n :longint; i :longint; x, y :longint; t, tot :longint; key, s, ...
- mysql六:索引原理与慢查询优化
一 介绍 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语句 ...
- 新版谷歌浏览器设置flash插件不提示步骤
1.先去chrome实验室界面chrome://flags/#enable-ephemeral-flash-permission选择取消Disabled.取消该实验室选项. 2.然后去chrome:/ ...
- 利用Java编写简单的WebService实例-转载
使用Axis编写WebService比较简单,就我的理解,WebService的实现代码和编写Java代码其实没有什么区别,主要是将哪些Java类发布为WebService.下面是一个从编写测试例子到 ...
- jQuery获取标签中的元素
获取双标签之间的内容 在JavaScript中,获取双标签之间的内容是这样的: <!DOCTYPE html> <html lang="en"> <h ...
- python ajax post 数据
简单的html <div> <input type="submit" id="tes" value="tes"> & ...
- Laravel5.5新特性
1.新的报错页面 报错更加美观,并标记显示出错误的代码 2.包的自动配置 在conposer.json文件中加入包中的配置,下载后就会自动配置到app.php 文件中,使用更方便 在之前的 Larav ...
- 安卓长按交互onCreateContextMenu的简单 用法
1.可在activity和fragment中使用. 2.使用方法 (1)注册 registerForContextMenu(btn);//btn是要实现交互的控件 (2)重写onCreateConte ...