转自:http://www.techbulo.com/1748.html
前一段时间在DM8168中进行颜色空间的转换,在网上找了些程序,自己也根据网上的改了下,由于能力问题,实在是不好意思说做了好几天才弄出来,主要是因为YUV<—>RGB有各种各样的转换公式。在多次的实验修改后,终于找到了对的公式,共享出来,以便需要的人选择。
在监控系统中大多采用YUV的颜色空间,原因不说了,网上搜YUV转RGB各种介绍。
在TI的视频英语达芬奇系列中(这里只测试了DVR、IPNC),
采用如下公式:
yCbCr<-->rgb
Y’ = 0.257*R' + 0.504*G' + 0.098*B' + 16
Cb' = -0.148*R' - 0.291*G' + 0.439*B' + 128
Cr' = 0.439*R' - 0.368*G' - 0.071*B' + 128
R' = 1.164*(Y’-16) + 1.596*(Cr'-128)
G' = 1.164*(Y’-16) - 0.813*(Cr'-128) - 0.392*(Cb'-128)
B' = 1.164*(Y’-16) + 2.017*(Cb'-128)
转换效果图如下
第一幅是原图YUV,第二幅是转为RGB后存为bmp,反转是因为BMP图像是从最下面一行开始存取的原因,最下面一副是转回YUV的图像。
代码如下,代码中YUV采用YUYVYUYV的格式,RGB为RGBRGBRGB.....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
#ifndef _RGB2YUV__
#define _RGB2YUV__
#include
using
namespace
std;
#define TUNE(r) ( r < 0 ? 0 : (r > 255 ? 255 : r) )
static
int
RGB_Y[256];
static
int
RGBR_V[256];
static
int
RGBG_U[256];
static
int
RGBG_V[256];
static
int
RGBB_U[256];
static
int
YUVY_R[256];
static
int
YUVY_G[256];
static
int
YUVY_B[256];
static
int
YUVU_R[256];
static
int
YUVU_G[256];
static
int
YUVU_B[256];
static
int
YUVV_R[256];
static
int
YUVV_G[256];
static
int
YUVV_B[256];
static
int
coff_rv[256];
static
int
coff_gu[256];
static
int
coff_gv[256];
static
int
coff_bu[256];
//直接采用公式浮点计算方式
//仅RGB2YUV采用了查表法,所以有一部分表是没有用到的
void
InitTable()
{
int
i;
for
(i = 0;i<256;i++)
{
//初始化表,放大256倍
RGB_Y[i] = 298 * (i - 16);
RGBR_V[i] = 408 * (i - 128);
RGBG_U[i] = 100 * (128- i);
RGBG_V[i]= 208*(128-i);
RGBB_U[i] =517 * (i - 128);
//y=0.257*r+0.504*g+0.098*b+16
//u = -0.148*r - 0.291*g + 0.439*b + 128
//0.439*r - 0.368*g - 0.071*b + 128
YUVY_R[i]=66*i;
YUVY_G[i]=129*i;
YUVY_B[i]=25*i;
YUVU_R[i]=-38*i;
YUVU_G[i]=-74*i;
YUVU_B[i]=112*i;
YUVV_R[i]=112*i;
YUVV_G[i]=-94*i;
YUVV_B[i]=-18*i;
/*所用公式(此公式不适用)
*pRGB = (unsigned char)(1.0*y + 8 + 1.402*(v-128)); pRGB++; // r
*pRGB = (unsigned char)(1.0*y - 0.34413*(u-128) - 0.71414*(v-128)); pRGB++; // g
*pRGB = (unsigned char)(1.0*y + 1.772*(u-128) + 0); pRGB++ ;
*/
coff_rv[i] = (8+1.402*(i-128))*256;
coff_gu[i] = -0.34413*(i-128)*256;
coff_gv[i] = -0.71414*(i-128)*256;
coff_bu[i] = 1.772*(i-128)*256;
/*应该使用如下公式:
Y’ = 0.257*R' + 0.504*G' + 0.098*B' + 16
Cb' = -0.148*R' - 0.291*G' + 0.439*B' + 128
Cr' = 0.439*R' - 0.368*G' - 0.071*B' + 128(标红的两组公式是可逆的转换)
R' = 1.164*(Y’-16) + 1.596*(Cr'-128)
G' = 1.164*(Y’-16) - 0.813*(Cr'-128) - 0.392*(Cb'-128)
B' = 1.164*(Y’-16) + 2.017*(Cb'-128)
*/
}
}
void
YUV2RGB422(unsigned
char
*pRGB, unsigned
char
*pYUV,
int
size)
{
unsigned
char
y, u, v,y1;
int
r,g,b;
unsigned
int
i=0;
unsigned
int
loop = size>>1;
while
(loop-- >0)
{
y = *pYUV; pYUV++;
u = *pYUV; pYUV++;
y1 = *pYUV;pYUV++;
v = *pYUV; pYUV++;
r = 1.164*(y-16) + 1.596*(v-128);
g = 1.164*(y-16) - 0.813*(v-128) - 0.392*(u-128);
b = 1.164*(y-16) + 2.017*(u-128);
*pRGB = TUNE(r);pRGB++;
*pRGB = TUNE(g);pRGB++;
*pRGB = TUNE(b);pRGB++;
r = 1.164*(y1-16) + 1.596*(v-128);
g = 1.164*(y1-16) - 0.813*(v-128) - 0.392*(u-128);
b = 1.164*(y1-16) + 2.017*(u-128);
*pRGB = TUNE(r);pRGB++;
*pRGB = TUNE(g);pRGB++;
*pRGB = TUNE(b);pRGB++;
}
}
//size 为图片的大小
void
RGB2YUV422(unsigned
char
*pRGB, unsigned
char
*pYUV,
int
size)
{
unsigned
char
r,g,b,u,v,u1,v1,r1,g1,b1;
//unsigned char *YUVBuff;
//unsigned char* p;
//p = YUVBuff;//
int
loop = size/2;
int
i;
for
( i=0;i {
r = *pRGB; pRGB++;
g = *pRGB; pRGB++;
b = *pRGB; pRGB++;
r1 = *pRGB; pRGB++;
g1 = *pRGB; pRGB++;
b1 = *pRGB; pRGB++;
//new method --- right
int
y = ((YUVY_R[r] + YUVY_G[g] + YUVY_B[b] + 128) >> 8) +16;
u = ((YUVU_R[r] + YUVU_G[g] + YUVU_B[b] + 128) >> 8) + 128;
v = ((YUVV_R[r] + YUVV_G[g] + YUVV_B[b] + 128) >> 8) + 128;
int
y1 = ((YUVY_R[r1] + YUVY_G[g1] + YUVY_B[b1] + 128) >> 8)+16;
u1 = ((YUVU_R[r1] + YUVU_G[g1] + YUVU_B[b1] + 128) >> 8) + 128;
v1 = ((YUVV_R[r1] + YUVV_G[g1] + YUVV_B[b1] + 128) >> 8) + 128;
*pYUV++ = TUNE(y);
*pYUV++ =(TUNE(u)+TUNE(u1))>>1;
*pYUV++ = TUNE(y1);
*pYUV++ = TUNE(v);
}
}
void
inline
Yuv2RgbPixel(unsigned
char
y,unsigned
char
u,unsigned
char
v,
unsigned
char
* rgbPixel)
{
int
r = (RGB_Y[y] + RGBR_V[v] + 128) >> 8;
int
g = ((RGB_Y[y] + RGBG_V[v] +RGBG_U[u]+ 128)>>8 );
int
b = ((RGB_Y[y] + RGBB_U[u]+128 )>>8);
*rgbPixel=TUNE(r);rgbPixel++;
*rgbPixel=TUNE(g);rgbPixel++;
*rgbPixel=TUNE(b);
}
void
YUV2RGB(unsigned
char
*pRGB, unsigned
char
*pYUV,
int
size)
//444
{
unsigned
char
y, u, v;
for
(
int
i=0;i {
y = *pYUV; pYUV++;
u = *pYUV; pYUV++;
v = *pYUV; pYUV++;
Yuv2RgbPixel(y,u,v,pRGB);
pRGB += 3;
}
}
void
inline
Rgb2YuvPiexl(unsigned
char
r,unsigned
char
g,unsigned
char
b,unsigned
char
* pYUV)
{
int
y = ((YUVY_R[r] + YUVY_G[g] + YUVY_B[b] + 128) >> 8) + 16;
int
u = ((YUVU_R[r] + YUVU_G[g] + YUVU_B[b] + 128) >> 8) + 128;
int
v = ((YUVV_R[r] + YUVV_G[g] + YUVV_B[b] + 128) >> 8) + 128;
*pYUV = TUNEY(y);pYUV++;
//*pYUV = u < 0 ? 0 : (u > 255 ? 255 : u);pYUV++;
*pYUV =TUNE(u);pYUV++;
*pYUV = TUNE(v);
}
void
RGB2YUV(unsigned
char
*pRGB, unsigned
char
*pYUV,
int
size)
{
unsigned
char
r,g,b,y,u,v;
for
(
int
i=0;i {
r = *pRGB; pRGB++;
g = *pRGB; pRGB++;
b = *pRGB; pRGB++;
Rgb2YuvPiexl(r,g,b,pYUV);
pYUV +=3;
}
}
#endif
|
下载地址
下载:
http://pan.baidu.com/s/1hq3uNha?errno=0&errmsg=Auth%20Login%20Sucess&stoken=f020d1d53f5d4c4671029afc709dd4ef3f893778ec8c168d7881fbbe234d5f6891fef8793af9eb320259264cc8feb7a917e42e24137f4844345aa63bb96608c0359d34a9e870&bduss=9c78001701eb1c877df86450e384eaad2e24aab8c7355e18ff571d7094736b9a98d093df0711036ea1da104794c74543f766e7a2f9045654f37d8eb04f193896eb48714c84cbdf71fd14707bf700757ca570d7a29a5126a6d1764150f69d4f3284436d8972efb737604d2caacff9fb483070733fed8246900a0410560325515b441b0fb63fc5e6625a88c07f8e3fc37236b45a016d503fa40883397d046b45914a76668db9b3f8c9f1d87c5bd6cc5473678e5db4fa483f5e17fe44612a7e2c228eebea9a3c3f&ssnerror=0