1

我有一个图像大小,640 x480我想使用 yv12 作为输出格式。图像正在由一些 API 处理,我想获得 YV12 格式的输出。

我现在正在尝试的是

整数宽度 = 640;
整数高度 = 480;

byte[] byteArray = new byte[Convert.ToInt32((width * height + (2 * ((width * height) / 4))) * 1.5) ];

captureDevice.GetPreviewBufferYCbCr(byteArray)");

            int YLength = Convert.ToInt32(width * height * 1.5);

            // UVlength multipled by 2 because it is for both u and v
            int UVLength = Convert.ToInt32(2 * (width / 2) * (weight / 2)) * 1.5);

            var inputYBuffer = byteArray.AsBuffer(0, YLength);
            var inputUVBuffer = byteArray.AsBuffer(YLength, UVLength);


            var inputBuffers = new IBuffer[] { inputYBuffer, inputUVBuffer };
            var inputScanlines = new uint[] { (uint)YLength, (uint)UVLength  };
            /////Creating input buffer that supports Nv12 format

                Bitmap inputBtm = new Bitmap(
                outputBufferSize,
                ColorMode.Yvu420Sp,
                inputScanlines, // 1.5 bytes per pixel in  Yuv420Sp mode
                inputBuffers);



            //As v length is same as u length in output buffer so v length can be used in both place.
            int vLength = UVLength / 2;
            var outputYBuffer = byteArray.AsBuffer(0, YLength);
            var outputVBuffer = byteArray.AsBuffer(YLength, vLength);
            var outputUBuffer = byteArray.AsBuffer(YLength + vLength, vLength);

            var outputBuffers = new IBuffer[] { outputYBuffer, outputVBuffer, outputUBuffer };
            //
            var outputScanlines = new uint[] { (uint)YLength, (uint)vLength, (uint)vLength };


            Bitmap outputBtm = new Bitmap(
                outputBufferSize,
                ColorMode.Yuv420P,
                outputScanlines, 
                outputBuffers);**strong text**

所以我想问的是我是否根据 YUV420P 格式正确创建了输出缓冲区。有一个 API,我在其中传递 NV12 中的输入缓冲区,我假设它会给我 YV12(YUV420P)格式的输出缓冲区。所以我创建了 Y 平面,它有width x height x 1.5字节。1.5 因为 YV12 是一个 12 位的 mat 并且类似地 U 平面具有width/2 x height/2 x 1.5字节并且类似地 V 平面。我现在不关心 YUV420P 和 YVU420P,只要我的格式正确,我只需要交换 U 和 V 平面。

4

1 回答 1

11

YUV 4:2:0 平面看起来像这样:

----------------------
|     Y      | Cb|Cr |
----------------------

在哪里:

Y = width x height pixels (bytes)
Cb = Y / 4 pixels (bytes)
Cr = Y / 4 pixels (bytes)

Total num pixels (bytes) = width * height * 3 / 2

这是像素在 4:2:0 子采样中的放置方式:

420

如您所见,每个色度值在 4 个亮度像素之间共享。

于 2013-07-09T21:01:50.167 回答