直接上代码

#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) }


參考:https://msdn.microsoft.com/en-us/library/aa917087.aspx

YUV12(420) (from)to RGB24的更多相关文章

  1. linux之x86裁剪移植---ffmpeg的H264解码显示(420、422)

    在虚拟机上yuv420可以正常显示 ,而945(D525)模块上却无法显示 ,后来验证了directdraw的yuv420也无法显示 ,由此怀疑显卡不支持 ,后把420转换为422显示. 420显示如 ...

  2. etlpy: 并行爬虫和数据清洗工具(开源)

    etlpy是python编写的网页数据抓取和清洗工具,核心文件etl.py不超过500行,具备如下特点 爬虫和清洗逻辑基于xml定义,不需手工编写 基于python生成器,流式处理,对内存无要求 内置 ...

  3. Kafka 0.9+Zookeeper3.4.6集群搭建、配置,新Client API的使用要点,高可用性测试,以及各种坑 (转载)

    Kafka 0.9版本对java client的api做出了较大调整,本文主要总结了Kafka 0.9在集群搭建.高可用性.新API方面的相关过程和细节,以及本人在安装调试过程中踩出的各种坑. 关于K ...

  4. 【position也可以很复杂】当弹出层遇上了鼠标定位(下)

    前言 接着昨天的内容写,为了保证内容连续性,这里还是把昨天的内容拷了过来. 请用现代浏览器测试 引出问题 有图有真相,我们来看一个智联招聘里面经常出现的图层: 他这个是没有什么问题的,我们来简单看看其 ...

  5. (转)Rest介绍

    参考文献:Rest简介 REST是一种组织Web服务的架构,其只在架构方面提出了一系列约束. 关于Restful的无状态 所以在stackoverflow中,我们常常会看到有人问:我现在使用了这样一种 ...

  6. OCJP(1Z0-851) 模拟题分析(一)11

    Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有 ...

  7. 《利用python进行数据分析》读书笔记--第十一章 金融和经济数据应用(一)

    自2005年开始,python在金融行业中的应用越来越多,这主要得益于越来越成熟的函数库(NumPy和pandas)以及大量经验丰富的程序员.许多机构发现python不仅非常适合成为交互式的分析环境, ...

  8. Python_sklearn机器学习库学习笔记(三)logistic regression(逻辑回归)

    # 逻辑回归 ## 逻辑回归处理二元分类 %matplotlib inline import matplotlib.pyplot as plt #显示中文 from matplotlib.font_m ...

  9. 建模算法(八)——插值

    插值:求过已知有限个数据点的近似函数 拟合:已知有限个数据点,求近似函数,不要求过已知数据点,只要求在某种意义下在这些点的误差最小 (一)插值方法 一.拉格朗日多项式插值 1.插值多项式 就是做出一个 ...

随机推荐

  1. 2017年上海金马五校程序设计竞赛:Problem A : STEED Cards (STL全排列函数)

    Description Corn does not participate the STEED contest, but he is interested in the word "STEE ...

  2. TensorFlow_曲线拟合

    # coding:utf-8 import tensorflow as tf import numpy as np import matplotlib.pyplot as plt import os ...

  3. kuangbin 带你飞 概率期望

    正推不行就逆推! 经典问题:生日悖论 换成其互斥事件:m个人, 每个人生日都不相同的概率 ≤ 0.5 时最小人数. 这就是邮票收集问题的变形:每个邮票至少出现一次的概率 小于等于 0.5 邮票收集问题 ...

  4. 轻量级批量管理工具pssh

    pssh工具 pssh工具是个轻量级的批量管理工具,相比同类型的开源工具 Ansible,Saltstack,他比较轻量级,需要对管理的主机做秘钥认证 Ansible是可以做秘钥认证,也可以通过配置文 ...

  5. mysql之any,some all(zz)

    转载自:http://blog.csdn.net/netcy/article/details/8464503 ALL和ANY操作符的常见用法是结合一个相对比较操作符对一个数据列子查询的结果进行测试.它 ...

  6. 关于ros stage与navigation仿真总结5月16号

    主要总结内容 在costmap里是怎么判断机器人和障碍物碰撞了 stage_ros包输入输出,stage是怎么回事 rviz 中footprint和stage中position怎么联系到一起 voxe ...

  7. Jquery实现全选和取消全选的方法

    <input type="checkbox" id="all" />全选<br /> <input type="chec ...

  8. 【转】python argparse用法总结

    转自:https://www.jianshu.com/p/fef2d215b91d 1. argparse介绍 是python的一个命令行解析包,非常编写可读性非常好的程序 2. 基本用法 prog. ...

  9. django CXRF介绍

    CSRF(Cross-site request forgery)跨站请求伪造,是攻击者利用用户的身份操作用户帐户的一种攻击方式.和XSS攻击一样,存在巨大的危害性. 一.攻击方法 1.低级的CXRF攻 ...

  10. 训练指南 UVALive - 3523 (双联通分量 + 二分图染色)

    layout: post title: 训练指南 UVALive - 3523 (双联通分量 + 二分图染色) author: "luowentaoaa" catalog: tru ...