# VB实现RGB888到YUV422的转换

Y =   0.257R + 0.504G + 0.098B + 16
U = -0.148R - 0.291G + 0.439B + 128
V  =  0.439R - 0.368G - 0.071B + 128

好，现在准备工作已经差不多了。需要的就是整理一下思路了。

RGB到YUV转换的代码如下：

Open strRGBfile For Binary As #1
Open strYUVfile For Binary As #100

'BMP 54个字节之后的是像素数据
lengRGB = rgbHeight * rgbWidth * 3

ReDim rgbContent(lengRGB - 1) '重定义数组，为读取文件做准备
Get #1, 55, rgbContent() '取得第一文件的内容到数组
Dim i, j As Long
Dim r, g, b As Integer
ReDim yuvContent(lengRGB - 1)

For i = 0 To rgbHeight - 1
For j = 0 To rgbWidth - 1
b = rgbContent(3 * (rgbWidth * i + j) + 0)
g = rgbContent(3 * (rgbWidth * i + j) + 1)
r = rgbContent(3 * (rgbWidth * i + j) + 2)
yuvContent(3 * (rgbWidth * (rgbHeight - 1 - i) + j)) = 0.257 * r + 0.504 * g + 0.098 * b + 16
yuvContent(3 * (rgbWidth * (rgbHeight - 1 - i) + j) + 1) = -0.148 * r - 0.291 * g + 0.439 * b + 128
yuvContent(3 * (rgbWidth * (rgbHeight - 1 - i) + j) + 2) = 0.439 * r - 0.368 * g - 0.071 * b + 128
Next j
Next i
close #1
close #100

Open strYUVfile For Binary As #100
Dim j As Long
ReDim yuv422Content((lengRGB + 1) * 4 / 6 - 2)

For j = 0 To (lengRGB / 6 - 1) 'UYVY
yuv422Content(4 * j + 0) = Temp_yuvContent(6 * j + 1) / 2 + yuvContent(6 * j + 4) / 2  'U0
yuv422Content(4 * j + 1) = Temp_yuvContent(6 * j)                                                            'Y0
yuv422Content(4 * j + 2) = Temp_yuvContent(6 * j + 2) / 2 + yuvContent(6 * j + 5) / 2  'V0
yuv422Content(4 * j + 3) = Temp_yuvContent(6 * j + 3)                                                      'Y1
Next j

Put #100, , yuv422Content() '把yuv的内容写到yuv444件中
yuvlen = (lengRGB + 1) * 4 / 6 - 1
SavetoCfunction
'关闭文件
Close #100
MsgBox "完成。", vbInformation

RGB888 ＝》YUV444

RGB888 ＝》YUV422（UYVY）

RGB888 ＝》YUV420 (YV12)

RGB888 ＝》YUV411 (YUYYVY)

YUV444          ＝》RGB888

YUV422（UYVY）  ＝》RGB888

YUV420 (YV12)   ＝》RGB888

YUV411 (YUYYVY) ＝》RGB888

#### YUV422与RGB互相转换

2016-07-26 09:21:33

#### YUV422与RGB互相转换

2016-07-26 09:21:33

#### YUV与RGB格式转换

2017-12-24 11:13:12

#### RGB与YUV之间的转换

2017-03-12 21:01:10

#### YUV格式详解及 YUV与RGB转换

2013-10-24 15:36:24

#### YUV422与RGB互相转换

2016-07-26 09:21:33

#### 飞思卡尔 I.MX6Q-ipu视频流格式转换

2016年08月29日 9KB 下载

#### ARM平台YUV转RGB888

2014-07-17 16:24:01

#### yuv 转 RGB 汇编实现源码

2009年06月04日 7KB 下载