Getting started with video collection and processing (displaying YUV data)

Source: Internet
Author: User
Tags fread scale image
Original Works are allowed to be reprinted. During reprinting, please mark the article in hyperlink form
Original source, author information, and this statement. Otherwise, legal liability will be held. Http://ticktick.blog.51cto.com/823160/568928

In this article "Getting Started video collection and processing (learning to analyze YUV data)", we have learned how to analyze the acquired YUV original code stream, the next step is how to preview and display the collected code streams. Only after the code streams are displayed can you intuitively see whether the collected data is faulty.

Based on my experience, there are three methods to display the original YUV code stream:

(1) use MATLAB to write related programs for display. (In this article, I will provide related display programs)

(2) Use MFC + Direct Draw. (It will be provided after it is sorted out later)

(3) Use the third tool (yuvviewerplus.exe), good software. (Provided in the attachment)

Because Matlab is used for image display, the program code is easy to understand, intuitive, easy to maintain and modify, and can be easily modified according to different image sizes and YUV code stream storage methods, this article focuses on the discussion.

For the collected images, we generally need to pay attention to whether the Y component is correct. The gray scale image can be seen by directly displaying the Y component data. According to the displayed gray scale image, we can quickly determine whether the image's contour is correct. Based on the content in the previous section, you can extract the Y component of a frame in the code stream and use the following program to display it in MATLAB.

Test Conditions: [image size]: 720x576; [file content]: Only Y components of one frame of image;

 
 
  1. Function yuvtest (filename)
  2.  
  3. % Open image file
  4. FID = fopen (filename, 'R ');
  5. If FID =-1
  6. Error ('the file can not open ');
  7. End
  8.  
  9. Line = 576; % Image Height
  10. Colom = 720; % Image Width
  11.  
  12. Im = zeros (line, Colom );
  13. For I1 = 1: Line
  14. Im (I1, :) = fread (FID, Colom); % read data to the matrix
  15. End
  16.  
  17. Im = Im./255; % Normalization
  18. Figure, imshow (IM); % display image
  19. Fclose (FID );
  20. End

Of course, the above program can only display gray images. If you want to display color images, you must first convert the YUV code stream to the RGB data stream. The conversion formula is as follows:

 
 
  1. // Conversion formula (Floating Point Mode)
  2. R = Y + 1.4075 * (V-128)
  3. G = Y-0.3455 * (U-128)-0.7169 * (V-128)
  4. B = Y + 1.779 * (U-128)

For how to obtain the YUV value of each pixel, refer to the article at the beginning of this article, whether in yuv444, yuv422, or yuv420 format, after YUV is extracted and converted to RGB data according to the corresponding method, the file size should be:
Image Height * Image Width * 3. Because different YUV code streams are converted into RGB data extraction methods, I will not provide a unified conversion program here. You can convert them by yourself based on the formula and YUV extraction method.

When writing a file, consider the following storage format:

 
 
  1. R R R R R  
  2. R R R R R  
  3. R R R R R  
  4.  
  5. G G G G G  
  6. G G G G G  
  7. G G G G G  
  8.  
  9. B B B B B  
  10. B B B B B  
  11. B B B B B 

After saving the converted RGB file, you can use the MATLAB program provided below to display the color image.

Test Conditions: [image size]: 720x576; [file content]: image data consists of three parts: R, G, and B;

 
 
  1. Function showrgb (inputfile)
  2.  
  3. % Initialization
  4. Width = 720; % Image Width
  5. Height = 576; % Image Height
  6.  
  7. % Open File
  8. FID = fopen (inputfile );
  9.  
  10. % Read data
  11. If FID ~ =-1
  12. IMG = uint8 (zeros (height, width, 3 ));
  13. Img_t = uint8 (zeros (height, width ));
  14. For I1 = 1:3
  15. For I2 = 1: Height
  16. Img_t (I2, :) = fread (FID, width );
  17. End
  18. IMG (:,:, i1) = img_t;
  19. End
  20. Figure, imshow (IMG );
  21. Fclose (FID );
  22.  
  23. End

Very easy to use, you can directly select the YUV code stream format for display. However, I suggest you write related MATLAB programs or MFC programs to analyze your own code streams. In this way, you can modify the code according to the actual situation without being limited to other software.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.