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	*/
	/************************************************************************/
	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-10-09 14:16:57

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

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 */

人生的诗&middot;416节~420节

416.  雨 我在雨中疾行 焦灼的心情是燃沸的炉火 一点一滴地冷却 一点一滴地幻灭 我在雨中疾行 看到你可人的清隽的容颜 我的心啊 一世一生地沦陷 一世一生地苦痛 我在雨中疾行 远的近的行人在冷冷地躲雨 看着我如看一幕伤怀的哑剧 于是我哭了 眼泪遗落在汹涌的雨水里 417.  遗忘的名字 从很多年前 我忘记了一个不能遗忘的名字 一年一年,找寻成了我生命的全部 却再也找不回遗忘的名字 418.  清隽 哀伤的歌,忧伤的诗 慢慢低吟出你的清隽 你的眉是英挺的远黛 如画的风景里藏着威仪 你的唇是轻薄

nyoj 420 p次方求和 【快速幂】

题意... 策略:rt 代码: #include <stdio.h> #include <string.h> #define temp 10003 int ans(int n, int p){ int res = 1; n %= temp; while(p){ if(p&1) res = (n*res)%temp; n = (n*n)%temp; p /= 2; } return res; } int main(){ int t, n, p; scanf("%d&

NYOJ 420 p次方求和 (快速幂+同余定理)

题目描述: http://acm.nyist.net/JudgeOnline/problem.php?pid=420 一个很简单的问题,求1^p+2^p+3^p+--+n^p的和. 输入 第一行单独一个数字t表示测试数据组数.接下来会有t行数字,每行包括两个数字n,p, 输入保证0<n<=1000,0<=p<=1000. 输出 输出1^p+2^p+3^p+--+n^p对10003取余的结果,每个结果单独占一行. 样例输入 210 110 2 样例输出 55385 题目分析: 快速幂

8月16号=》411页-420页

15.2.4 事件冒泡 当浏览者在页面上执行某个动作时,页面上实际有多个元素可以响应该事件 ,假如单击页面的某个按钮,而该按钮又处于<div>元素之内,则实际 上用户既单击了该按钮,也单击了该<div>元素. 代码示范: //脚本代码 var gotClick = function(who) { document.getElementById("results").innerHTML += who+"被单击"; } //HTML代码 <

2月.CN域名总量净增187.7万个 涨幅环比增大420%

IDC评述网(idcps.com)03月10日报道:根据中国互联网络信息中心(CNNIC)最新公布的数据显示,截止至2016年2月,.CN域名总量达到18,688,645个,环比1月,净增1,877,908个,增速有明显增大.下面,请看IDC评述网整理的相关数据情况. (图1).CN域名增长趋势图 通过图1,IDC评述网了解到,在2016年2月份,.CN域名总量急剧增长,攀升至18,688,645个.期间共净增1,877,908个,与上月447,143个的净增量相比,涨幅约增大420%,增速大幅

P次方求和 NYOJ 420

1 #include<stdio.h>//P次方求和(420) 2 long long power(int a,int b) 3 { 4 long long t; 5 if(b==0)return 1%10003; 6 if(b==1)return a%10003; 7 t=power(a,b/2); 8 t=t*t; 9 if(b&1)return a*t%10003; 10 else return t%10003; 11 } 12 int main() 13 { 14 int x;

YUV422转RGB24

使用mplayer可以直接播放YUV原始数据文件 int DeckLinkCaptureDelegate::convert_yuv_to_rgb_pixel(int y, int u, int v) { unsigned int pixel32 = 0; unsigned char *pixel = (unsigned char *)&pixel32; int r, g, b; r = y + (1.370705 * (v-128)); g = y - (0.698001 * (v-128))

NYOJ 420 P次方求和

p次方求和 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 一个很简单的问题,求1^p+2^p+3^p+--+n^p的和. 输入 第一行单独一个数字t表示测试数据组数.接下来会有t行数字,每行包括两个数字n,p, 输入保证0<n<=1000,0<=p<=1000. 输出 输出1^p+2^p+3^p+--+n^p对10003取余的结果,每个结果单独占一行. 样例输入 2 10 1 10 2 样例输出 55 385 AC码: #include<std