【发布时间】:2018-08-31 07:32:46
【问题描述】:
我想将位图转换为 YUV422 (YUYV) 格式。 我在谷歌上搜索了 YUYV 格式并尝试编写此代码。
path = "C:/Users/hogan/Desktop/red.bmp"
image = Image.open(path).convert("YCbCr") # YUV
image = np.asarray(image)
width, height, YUYV = image.shape[1], image.shape[0], 4
array = np.empty((height * width * 3), dtype="uint8")
Y, U, V = 0, 1, 2
count = 0
for i in range(len(image)):
for j in range(len(image[i])):
for k in range(len(image[i][j])):
if (count % 4 == 0):
array[count] = image[i][j][Y]
if (count % 4 == 1):
array[count] = image[i][j][U]
if (count % 4 == 2):
array[count] = image[i][j][Y]
if (count % 4 == 3):
array[count] = image[i][j][V]
count = count + 1
array.astype('uint8').tofile("C:/Users/hogan/Desktop/tmpdir/1.raw")
我阅读了这张图片,知道我的代码有误,但不知道如何改正。 例如 : YUV 中的红色 (255,0,0) 是 (76,84,255),如果我有很多像素,我不知道应该删除哪个“U”和“V”。
如果使用我的代码转换一个 480*640 (W*H),它将是 960*480。
【问题讨论】:
-
您不会删除 U 和 V,而是取两个相邻像素的 U 和 V 的平均值并存储平均值。该文件应具有以下格式:Y1, (U1 + U2)/2, Y2, (V1 + V2)/2, Y3, (U3 + U4)/2, Y4, (V3 + V4)/ 2跨度>