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.插值多项式 就是做出一个 ...
随机推荐
- OpenWRT介绍
1. 介绍 OpenWRT是一款第三方路由器固件, 其特别在于开放性, 如它的文件系统可写, 用户可在路由器上安装需要的第三方软件.通过刷入OpenWRT, 我们可以完成如下事情 - DLNA共享 - ...
- 出现“error c4430缺少类型说明符-假定为int。注意C++不支持默认int
出现这种错误的原因,是因为函数没有写返回值.是在VC6.0的工程转为高版本(VS2010)的时候经常出现的; #include <stdio.h> main() { printf(&quo ...
- python日志模块笔记
前言 在应用中记录日志是程序开发的重要一环,也是调试的重要工具.但却很容易让人忽略.之前用flask写的一个服务就因为没有处理好日志的问题导致线上的错误难以察觉,修复错误的定位也很困难.最近恰好有时间 ...
- Android系统是一个基于BInder通信的C/S架构
Android系统基本上可以看作是一个基于Binder通信的C/S架构.他有服务器端和客户端.比如自己开发一个程序,肯定是基于Activity的而Activity就是作为客户端,他的服务器端就是Act ...
- win32 listctrl控件右键菜单的实现
HMENU Menu_list,Menu_all; POINT point; HINSTANCE hInstance;//下面代码放到BOOL WINAPI DialogProc下 case WM_C ...
- AC日记——由乃与大母神原型和偶像崇拜 洛谷 P3792
由乃与大母神原型和偶像崇拜 思路: 逆元+线段树维护和+线段树维护平方和+线段树维护最大最小值: 代码: #include <bits/stdc++.h> using namespace ...
- Codeforces #436 Div2 E
#436 Div2 E 题意 某人的房子着火了,现在有 \(n\) 件物品待抢救,每件物品有抢救需要的时间和自身的价值,以及过多长时间物品会损坏.问最多一共可以抢救价值多少的物品? 分析 看数据就知道 ...
- LCA【SP913】Qtree - Query on a tree II
Description 给定一棵n个点的树,边具有边权.要求作以下操作: DIST a b 询问点a至点b路径上的边权之和 KTH a b k 询问点a至点b有向路径上的第k个点的编号 有多组测试数据 ...
- [BZOJ1143][CTSC2008]祭祀river(Dilworth定理+二分图匹配)
题意:给你一张n个点的DAG,最大化选择的点数,是点之间两两不可达. 要从Dilworth定理说起. Dilworth定理是定义在偏序集上的,也可以从图论的角度解释.偏序集中两个元素能比较大小,则在图 ...
- python3开发进阶-Django框架学习前的小项目(一个简单的学员管理系统)
''' 自己独立写一个学员管理系统 表结构: 班级表: -id -grade_name 学生表: -id -student_name -grade 关联外键班级表 老师表: -id -teacher_ ...