DSP

【DSP开发】【图像处理】Gray与YUV之间的转换关系

2019-07-13 12:19发布

标准的V4L2 API http://v4l.videotechnology.com/dwg/v4l2.pdf 在例程/home/dvevm_1_20/demos/ImageGray中,涉及到图像采集及显示的一些概念 主要的几个文件 capture.c display.c video.c 在demo里面采集用到的格式是UYVY V4L2_PIX_FMT_UYVY ('UYVY') Name V4L2_PIX_FMT_UYVY -- Variationof V4L2_PIX_FMT_YUYV with different order of samples in memory Description In this format each four bytes is twopixels. Each four bytes is two Y's, a Cb and a Cr. Each Y goes to one of thepixels, and the Cb and Cr belong to both pixels. As you can see, the Cr and Cbcomponents have half the horizontal resolution of the Y component. Example 2-1. V4L2_PIX_FMT_UYVY4 × 4 pixel image Byte Order. Each cell is one byte. start + 0: Cb00 Y'00 Cr00 Y'01 Cb01 Y'02 Cr01 Y'03 start + 8: Cb10 Y'10 Cr10 Y'11 Cb11 Y'12 Cr11 Y'13 start + 16: Cb20 Y'20 Cr20 Y'21 Cb21 Y'22 Cr21 Y'23 start + 24: Cb30 Y'30 Cr30 Y'31 Cb31 Y'32 Cr31 Y'33 Color Sample Location.   0   1   2   3 0 Y C Y   Y C Y 1 Y C Y   Y C Y 2 Y C Y   Y C Y 3 Y C Y   Y C Y   在imagegray里面把图片变成灰度是在filecopy_dec.c这个函数里面有这样的代码 static void PictureGray(void *pInbuf,void*pOutbuf,unsigned int len) {    unsigned int i;    unsigned int * pIn = (unsigned int*)pInbuf; unsigned int *pOut= (unsigned int*)pOutbuf;  len >>= 2;    for(i=0;ihoweversince the display is expecting an image formatted in color we cannot take themout completely, but rather we can make the chrominancevalues constant, and for greyscale/B&W you want them to all be mid pointvalues of 0x80 (out of 0xFF).  The code below does just this, if yougive it a pointer to the YCbCr 4:2:2 frame buffer and the size of the buffer itwill step through and make all of the Cb and Cr values 0x80 so that the imageappears greyscale on the output. Bernie Thompson: //make the image greyscale by setting allchrominance to 0x80
void process_imagebw( void* currentFrame,  int yRows, int xPixels)
{
   
 int xx = 0;
 
     for( xx = 0; xx < (yRows * xPixels)*2; xx+=2)//just operating on the chroma
      {            *( ( (unsigned char*)currentFrame ) + xx ) = 0x80; //set all chromato midpoint 0x80
         }  
} // End process_imagebw() 对TVP5158采集到的YUV422分析,它是以UYVY格式存放的,因为U,V是正交化的由于本课题用到的图像处理基于黑白图像,所以只需对Y分量进行处理,那么第一步就是对YUV422提取出Y,如果要保留Y分量,那么需将UV置为0x80,如需置白/黑 {MOD},Y分量设为FF/00,提取出亮度信号后,即可作简单的二值化. UV是 {MOD}差分量,UV为0就会全是绿 {MOD},全为0x80的时候才能看到灰度图。