gpt4 book ai didi

c# - 根据YUV420P格式的格式化缓冲区

转载 作者:太空宇宙 更新时间:2023-11-03 17:53:42 31 4
gpt4 key购买 nike

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

我目前正在尝试的是


  int宽度= 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是mat的12位,并且类似地,U平面具有 width/2 x height/2 x 1.5字节,而V平面具有类似的字节。只要我的格式正确,我现在就不在乎YUV420P和YVU420P,只需要交换U和V平面即可。

最佳答案

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子采样中的放置方式:



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

关于c# - 根据YUV420P格式的格式化缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17450526/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com