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

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

时间: 2024-08-02 19:04:11

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

YUV12(420) 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 */

关于码间串扰(转载)

根据上节对码间串扰的讨论,我们可将无码间串扰对基带传输系统冲激响应h(t)的要求概括如下: ( 1 )基带信号经过传输后在抽样点上无码间串扰,也即瞬时抽样值应满足                                 ( 4-18 ) ( 2 ) 尾部衰减要快. 式( 4-18 )所给出的无码间串扰条件是针对第 个码元在 时刻进行抽样判决得来的. 是一个时延常数,为了分析简便起见,假设 ,这样无码间串扰的条件变为                                    

《你不知道的JavaScript》整理(五)——值与原生函数

一.值 1)数字 JavaScript只有一种数值类型:number(数字),包括"整数"和带小数的十进制数. //数字的语法 var a = 5E10; // 50000000000 a.toExponential(); // "5e+10" var b = a * a; // 2.5e+21 var c = 1 / a; // 2e-11 var d = 0.42; var e = .42; //数字前面的0可以省略 var f = 42.; //小数点后小数部

Backbone笔记(续)

Backbone Bockbone 总览 Backbone 与 MVC 模式:解决某一类问题的通用方案 - 套路 MVC:一种架构模式,解耦代码,分离关注点 M(Model) - 数据模型 V(View) - 表现视图 C(Controller) - 控制器 Backbone 与 SPA 传统web应用与 SPA 的区别: 状态概念代替了页面概念 http://www.example.com/page1 http://www.example.com/page2 http://www.exampl

小白日记3:kali渗透测试之被动信息收集(二)-dig、whios、dnsenum、fierce

一.DIG linux下查询域名解析有两种选择,nslookup或者dig.Dig(Domain Information Groper)是一个在类Unix命令行模式下查询DNS包括NS记录,A记录,MX记录等相关信息的工具. <span style="font-size:18px;">[email protected]:~# dig -h Usage: dig [@global-server] [domain] [q-type] [q-class] {q-opt} {glo

基于汉字字频特征实现99.99%准确率的新闻文本分类器(四)

基于汉字字频特征实现99.99%准确率的新闻文本分类器(一) 基于汉字字频特征实现99.99%准确率的新闻文本分类器(二) 基于汉字字频特征实现99.99%准确率的新闻文本分类器(三) 基于汉字字频特征实现99.99%准确率的新闻文本分类器(四) 回顾 上一节中,使用五层神经网络,对抽取出的汉字字频特征向量进行分类,得到了超过99%的准确率,在高准确率的前提下,没有陷入局部最优解,对一些在训练中,被指定了上千次不是军事类的新闻,只要内容是军事类的,就能被正确的找出,同样,混在军事类新闻里的[慈善

ZooKeeper-3.3.4集群安装配置(转载)

ZooKeeper是一个分布式开源框架,提供了协调分布式应用的基本服务,它向外部应用暴露一组通用服务——分布式同步(Distributed Synchronization).命名服务(Naming Service).集群维护(Group Maintenance)等,简化分布式应用协调及其管理的难度,提供高性能的分布式服务.ZooKeeper本身可以以Standalone模式安装运行,不过它的长处在于通过分布式ZooKeeper集群(一个Leader,多个Follower),基于一定的策略来保证Z

决策树系列(四)——C4.5

预备知识:决策树.ID3 如上一篇文章所述,ID3方法主要有几个缺点:一是采用信息增益进行数据分裂,准确性不如信息增益率:二是不能对连续数据进行处理,只能通过连续数据离散化进行处理:三是没有采用剪枝的策略,决策树的结构可能会过于复杂,可能会出现过拟合的情况. C4.5在ID3的基础上对上述三个方面进行了相应的改进: a)  C4.5对节点进行分裂时采用信息增益率作为分裂的依据: b)  能够对连续数据进行处理: c)  C4.5采用剪枝的策略,对完全生长的决策树进行剪枝处理,一定程度上降低过拟合

FFmpeg的HEVC解码器源代码简单分析:解析器(Parser)部分

上篇文章概述了FFmpeg中HEVC(H.265)解码器的结构:从这篇文章开始,具体研究HEVC解码器的源代码.本文分析HEVC解码器中解析器(Parser)部分的源代码.这部分的代码用于分割HEVC的NALU,并且解析SPS.PPS.SEI等信息.解析HEVC码流(对应AVCodecParser结构体中的函数)和解码HEVC码流(对应AVCodec结构体中的函数)的时候都会调用该部分的代码完成相应的功能. 函数调用关系图 FFmpeg HEVC解析器(Parser)部分在整个HEVC解码器中的