在YUV格式图像上画点/画线/画矩形

在YUV格式图像上画点/画线/画矩形

1.YUY2格式描述

V4L2_PIX_FMT_YUYV格式也就是YUYV格式在Window环境中被称为YUY2,这种格式中每四个字节表示一个两个像素,其中包含两个Y,一个Cb(U)一个Cr(V)。也就是说,在这种格式中,两个像素的Y共用一组CbCr。其数据格式中Y与UV交错排布存储,其4x4图像数据存储如下,其中 Y’00、Y’01 共用Cb00 和Cr00 ,可以发现其数据量大小=宽x高x2:

start + 0:  Y’00  Cb00  Y’01  Cr00  Y’02  Cb01  Y’03 Cr01
start + 8:  Y’10  Cb10  Y’11  Cr10  Y’12  Cb11  Y’13 Cr11
start + 16: Y’20  Cb20  Y’21  Cr20  Y’22  Cb21  Y’23 Cr21
start + 24: Y’30  Cb30  Y’31  Cr30  Y’32  Cb31  Y’33 Cr31

2.NV12格式描述

与V4L2_PIX_FMT_YUYV不同,V4L2_PIX_FMT_NV12格式也就是NV12格式中每四个Y共用一组CbCr,其4x4图像数据存储如下所示,其中Y’00、Y’01、 Y’10、 Y’11共用Cb00 Cr00。除此之外图像的数据存储格式也不一样,NV12采用平面存储格式,其数据格式中先将Y分量单独存放在一平面中,然后再把UV数据分量交错排布存储在另一个平面,可以发现其数据量大小=宽x高 + 宽 x 高 / 2:

start + 0:  Y’00  Y’01  Y’02  Y’03
start + 4:  Y’10  Y’11  Y’12  Y’13
start + 8:  Y’20  Y’21  Y’22  Y’23
start + 12: Y’30  Y’31  Y’32  Y’33
start + 16: Cb00  Cr00  Cb01  Cr01
start + 20: Cb10  Cr10  Cb11  Cr11

3.程序实现:

实现思路:先画点,再画线,最后组成矩形


typedef struct{
	int left;
    int top;
    int right;
    int bottom;
}RECT;
typedef struct
{ 
    char Y; 
    char U; 
    char V;
}stYuvColor;

static stYuvColor s_color_table[2] = 
{ 
    {0x51, 0x5A, 0xFA}, 
    // red 
    {0x28, 0xff, 0x00}, 
    // blue 
};
static int YUY_Draw_Piont(unsigned char * YuvBuf, int ImgW, int ImgH, int fmt,int PntX, int PntY, int Color)
{
    int ret = -1;
	
    if(YuvBuf == NULL)
		return ret;

	switch(fmt)
	{
        case V4L2_PIX_FMT_YUYV:
        {
            int Yoffset = PntY * ImgW * 2 + (PntX * 2); 
			int Uoffset,Voffset;
			/*Y U Y V Y U Y V
	    pexl  0   1   2   3
        data  0 1 2 3 4 5 6 7
			 *Y U Y V Y U Y V */
			 
			if(PntX % 2 == 0)
			{
				Uoffset =  PntY * ImgW * 2 + (PntX * 2 + 1);
				Voffset =  PntY * ImgW * 2 + (PntX * 2 + 3);
			}
			else
			{
				Uoffset =  PntY * ImgW * 2 + (PntX * 2 - 1);
				Voffset =  PntY * ImgW * 2 + (PntX * 2 + 1);
			}
			
            YuvBuf[Yoffset] = s_color_table[Color].Y;
			YuvBuf[Uoffset] = s_color_table[Color].U;
			YuvBuf[Voffset] = s_color_table[Color].V;
		}
		    ret = 0;
		break;
		
		case V4L2_PIX_FMT_NV12:
		{
            int Yoffset = PntY * ImgW + PntX;
			int Uoffset,Voffset;
			
		    Uoffset =  ImgH * ImgW + (PntY / 2) * ImgW + PntX / 2 * 2 ;
		    Voffset =  Uoffset + 1;
			
            YuvBuf[Yoffset] = s_color_table[Color].Y;
			YuvBuf[Uoffset] = s_color_table[Color].U;
			YuvBuf[Voffset] = s_color_table[Color].V;
		}
		    ret = 0;
		break;

		default:
		    ret = -2;
		break;
	}
    return ret;
}

static int YUY_Draw_Line(unsigned char * YuvBuf, int ImgW, int ImgH, int fmt,int SPntX, int SPntY,int EPntX, int EPntY, int Color,int lineWidth)
{
    int ret = -1;
    if(YuvBuf == NULL)
		return ret;

	if(lineWidth == 0)
		lineWidth = 1;

    SPntX = (SPntX + lineWidth >= ImgW) ? (ImgW - lineWidth) : (SPntX < 0 ? 0 : SPntX);
    EPntX = (EPntX + lineWidth >= ImgW) ? (ImgW - lineWidth) : (EPntX < 0 ? 0 : EPntX); 
    SPntY = (SPntY + lineWidth >= ImgH) ? (ImgH - lineWidth) : (SPntY < 0 ? 0 : SPntY); 
    EPntY = (EPntY + lineWidth >= ImgH) ? (ImgH - lineWidth) : (EPntY < 0 ? 0 : EPntY);  

    int dx = (SPntX > EPntX) ? (SPntX - EPntX) : (EPntX - SPntX); 
    int dy = (SPntY > EPntY) ? (SPntY - EPntY) : (EPntY - SPntY);  
    int xstep = (SPntX < EPntX) ? 1 : -1;
	int ystep = (SPntY < EPntY) ? 1 : -1; 
    int nstep = 0, eps = 0; 
	int pointX = SPntX;
	int	pointY = SPntY;
	
     if(dx > dy)
    {  
    	while(nstep <= dx) 
    	{	
    		ret = YUY_Draw_Piont(YuvBuf, ImgW, ImgH, fmt, pointX, pointY, Color);
			if(ret < 0)
				break;
			
    		eps += dy;	 
    		if( (eps << 1) >= dx ) 
    		{	 
    			pointY += ystep;
    			eps -= dx;	 
    		}	
    		pointX += xstep;	 
    		nstep++;  
    	} 
    }
    else 
    {  
    	while(nstep <= dy)
    	{
    		ret = YUY_Draw_Piont(YuvBuf, ImgW, ImgH, fmt, pointX, pointY, Color); 
			if(ret < 0)
				break;
			
    		eps += dx;	 
    		if( (eps << 1) >= dy ) 
    		{	 
    			pointX += xstep;	  
    			eps -= dy;	 
    		}	
    			pointY += ystep;	 
    			nstep++;
    	} 
    }
    return ret;
}


static int YUV_Draw_Rect(unsigned char *YuvBuf, int ImgW, int ImgH, int fmt,RECT* Rect, int Color,int lineWidth)
{
    int ret = -1;
	if(YuvBuf == NULL)
		return ret;

	if(Rect->bottom == Rect->top || Rect->left == Rect->right)
		return ret;

	YUY_Draw_Line(YuvBuf, ImgW, ImgH,fmt, Rect->left, Rect->top , Rect->right, Rect->top, Color,lineWidth);
	YUY_Draw_Line(YuvBuf, ImgW, ImgH,fmt, Rect->right, Rect->top, Rect->right, Rect->bottom, Color,lineWidth);
	YUY_Draw_Line(YuvBuf, ImgW, ImgH,fmt, Rect->left, Rect->top , Rect->left,  Rect->bottom, Color,lineWidth);
	YUY_Draw_Line(YuvBuf, ImgW, ImgH,fmt, Rect->left,  Rect->bottom, Rect->right, Rect->bottom, Color,lineWidth);
	
    return 0;
}

注:线宽暂未实现,后续有时间再处理

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的YUV格式视频编码的C++代码示例,使用了OpenCV库进行图像读取、处理和编码: ``` #include <iostream> #include <opencv2/opencv.hpp> #include <opencv2/videoio.hpp> #include <opencv2/imgcodecs.hpp> #include <opencv2/imgproc.hpp> #include <opencv2/highgui.hpp> #include <opencv2/video.hpp> #include <vector> #include <fstream> using namespace std; using namespace cv; int main(int argc, char** argv) { // 读取视频文件 VideoCapture cap("input.mp4"); // 获取视频帧率和大小 int frame_width = (int)cap.get(CAP_PROP_FRAME_WIDTH); int frame_height = (int)cap.get(CAP_PROP_FRAME_HEIGHT); int fps = (int)cap.get(CAP_PROP_FPS); // 创建编码器对象 VideoWriter video("output.mp4", VideoWriter::fourcc('H', '2', '6', '4'), fps, Size(frame_width, frame_height), true); // 循环读取视频帧并编码 Mat frame; while (cap.read(frame)) { // 转换为YUV格式 Mat yuv; cvtColor(frame, yuv, COLOR_BGR2YUV); // 分离YUV通道 vector<Mat> planes; split(yuv, planes); // 写入编码器 video.write(planes[0]); video.write(planes[1]); video.write(planes[2]); } // 释放资源 cap.release(); video.release(); return 0; } ``` 在上面的示例中,我们首先使用`VideoCapture`对象读取视频文件,然后获取视频帧率和大小。接下来,我们创建一个`VideoWriter`对象,用于将编码后的视频帧写入文件。在循环中,我们逐帧读取视频并将其转换为YUV格式,然后将YUV通道分离并写入编码器。最后,我们释放资源并退出程序。 请注意,这仅是一个简单的示例,并且可能需要根据实际情况进行修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值