海康摄像头的视频通过H.264解码后,转换为YUV420P格式后就可以对YUV数据进行拼接,YUV的格式分为YUV444,YUV422,YUV420,格式见下图,通过此图可以看出,YUV420的压缩比较高,经常使用YUV420格式进行图像处理。
?YUV420的格式也分为以下4中,其中区别在于UV的排列方式不同
YUV420P进行拼接可分为横向拼接和纵向拼接?
下面是测试过的C#包含了横向纵向拼接代码,通过源YUV图像?,绘制到目的YUV图像的便宜位置的方法
/// <summary>
/// 绘制图像
/// </summary>
/// <param name="src"></param>
/// <param name="dst"></param>
/// <param name="nOffX"></param>
/// <param name="nOffY"></param>
public void MapImage(YuvImage src, YuvImage dst, uint nOffX, uint nOffY)
{
if (null == src || null == dst) return;
if (src.w > dst.w || src.h > dst.h) return;
if (null == src.y || null == src.u || null == src.v) return;
if (null == dst.y || null == dst.u || null == dst.v) return;
uint nOff = 0;
for (uint i = 0; i < src.h; i++)
{
nOff = dst.w * (nOffY + i) + nOffX;
Array.ConstrainedCopy(src.y, Convert.ToInt32(src.w * i), dst.y, Convert.ToInt32(nOff), Convert.ToInt32(src.w));
}
uint nUVOffX = nOffX / 2, nUVOffY = nOffY / 2;
uint nUVSrcW = src.w / 2, nUVSrcH = src.h / 2;
uint nUVDstW = dst.w / 2, nUVDstH = dst.h / 2;
for (uint j = 0; j < nUVSrcH; j++)
{
nOff = nUVDstW * (nUVOffY + j) + nUVOffX;
Array.ConstrainedCopy(src.u, Convert.ToInt32(nUVSrcW * j), dst.u, Convert.ToInt32(nOff), Convert.ToInt32(nUVSrcW));
Array.ConstrainedCopy(src.v, Convert.ToInt32(nUVSrcW * j), dst.v, Convert.ToInt32(nOff), Convert.ToInt32(nUVSrcW));
}
}
?
|