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;
- Function yuvtest (filename)
-
- % Open image file
- FID = fopen (filename, 'R ');
- If FID =-1
- Error ('the file can not open ');
- End
-
- Line = 576; % Image Height
- Colom = 720; % Image Width
-
- Im = zeros (line, Colom );
- For I1 = 1: Line
- Im (I1, :) = fread (FID, Colom); % read data to the matrix
- End
-
- Im = Im./255; % Normalization
- Figure, imshow (IM); % display image
- Fclose (FID );
- 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:
- // Conversion formula (Floating Point Mode)
- R = Y + 1.4075 * (V-128)
- G = Y-0.3455 * (U-128)-0.7169 * (V-128)
- 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:
- R R R R R
- R R R R R
- R R R R R
-
- G G G G G
- G G G G G
- G G G G G
-
- B B B B B
- B B B B B
- 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;
- Function showrgb (inputfile)
-
- % Initialization
- Width = 720; % Image Width
- Height = 576; % Image Height
-
- % Open File
- FID = fopen (inputfile );
-
- % Read data
- If FID ~ =-1
- IMG = uint8 (zeros (height, width, 3 ));
- Img_t = uint8 (zeros (height, width ));
- For I1 = 1:3
- For I2 = 1: Height
- Img_t (I2, :) = fread (FID, width );
- End
- IMG (:,:, i1) = img_t;
- End
- Figure, imshow (IMG );
- Fclose (FID );
-
- 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.