最近做视频采集,使用ov511摄像头。由于采用2.6.24内核,其驱动程序不能将图片格式转换为rgb565的,所以需要在应用程序上做修改。
网上流行的一个程序都是直接使用rgb565格式直接显示在fb设备上,为了使用该程序,需要将yuv420p格式的图像转换为rgb565的,下面是我的源代码,测试通过的
static void cvt_420p_to_rgb565(int width, int height, const unsigned char *src, unsigned short *dst) { int line, col, linewidth; int y, u, v, yy, vr, ug, vg, ub; int r, g, b; const unsigned char *py, *pu, *pv;
linewidth = width >> 1; py = src; pu = py + (width * height); pv = pu + (width * height) / 4;
y = *py++; yy = y << 8; u = *pu - 128; ug = 88 * u; ub = 454 * u; v = *pv - 128; vg = 183 * v; vr = 359 * v;
for (line = 0; line < height; line++) { for (col = 0; col < width; col++) { r = (yy + vr) >> 8; g = (yy - ug - vg) >> 8; b = (yy + ub ) >> 8;
if (r < 0) r = 0; if (r > 255) r = 255; if (g < 0) g = 0; if (g > 255) g = 255; if (b < 0) b = 0; if (b > 255) b = 255; *dst++ = (((unsigned short)r>>3)<<11) | (((unsigned short)g>>2)<<5) | (((unsigned short)b>>3)<<0); y = *py++; yy = y << 8; if (col & 1) { pu++; pv++;
u = *pu - 128; ug = 88 * u; ub = 454 * u; v = *pv - 128; vg = 183 * v; vr = 359 * v; } } if ((line & 1) == 0) { pu -= linewidth; pv -= linewidth; } } }
|
各个参数的意思想必大家都明白吧,就不多说了。
YUV和RGB的转换公式如下,供大家参考:
Y = 0.299R + 0.587G + 0.114B
U = -0.147R - 0.289G + 0.436B
V = 0.615R - 0.515G - 0.100B
R = Y + 1.14V
G = Y - 0.39U - 0.58V
B = Y + 2.03U
阅读(5600) | 评论(1) | 转发(0) |