Home > Back-end >  Create YUVtoJPEG as the name of the M file, read the CIF format YUV file mobile_cif_300f. YUV 30 fra
Create YUVtoJPEG as the name of the M file, read the CIF format YUV file mobile_cif_300f. YUV 30 fra

Time:09-21

Created by YUVtoJPEG name M file, read the CIF format YUV file mobile_cif_300f. YUV 30 frames before, and one by one to JPG format each video frame to the current directory, video ZhenZhen named after corresponding JPG images, and generate a frame 2 to 30 frames on the Y channel from the previous frame residual image, in BMP format, with video ZhenZhen naming, save to the current directory!


Function [] Y, U, V=ReadMultiFrames (yuvfilename, format, init2last)
% this function is used to yuv format video more consecutive frames separately into the three dimensional array Y, U, V
% yuv video sampling format for 4:2:0
% input parameters:
% yuvfilename - video yuv file path name
% format - the video format (or format name resolution [rows, cols])
% init2last - read the video frame range [initial frame number, terminate the frame number]
% output parameters:
% Y - brightness, three dimensional array, the third dimension for the frame number, the first two dimension is the rows and columns of a single frame
% U, V, off color, 3 d array, the third dimension for the frame number, the first two dimension is the rows and columns of a single frame
% call demonstration:
% [] Y, U, V=ReadMultiFrames ('. \ videoname yuv ', 'cif, [100101]).
% [] Y, U, V=ReadMultiFrames ('. \ videoname yuv ', [288352], [100101]).
close all;
If ischar (format)
Format=lower (the format);
Switch the format
Case 'sub_qcif'
Cols=128; Rows=96;
Case 'qcif'
Cols=176; Rows=144;
Case 'cif'
Cols=352; Rows=288;
Case 'sif'
Cols=352; Rows=240;
Case '4 cif'
Cols=704; Rows=576;
Otherwise
Error (' no format! ');
End
Elseif isequal (size (the format), [1, 2]) | | isequal (size (the format), (2, 1])
Cols=format (2); Rows=format (1);
The else
The second parameter error (' input is wrong! ');
End


Point=fopen (yuvfilename, 'r');
If point==1
Error (' failed to open the file! ');
End

Order_num=init2last (1);
Frames_num=init2last (2) - order_num + 1;
K=0;
Y=zeros (rows, cols, frames_num);
U=zeros (rows/2, cols/2, frames_num);
V=U;

Offset=order_num * (cols + rows rows * * cols/2);
Status=fseek (point, offset, 'a bof');
Pro=fread (point, 1, 'uchar');
If (isempty (pro) & amp; & The feof (point)) | | status ~=0
Error (' reading position location failed! ');
End
Fseek (point, 1, 'cof package);

For 2=1: frames_num
k=k + 1;
Pro=fread (point, 1, 'uchar');
If feof (point) & amp; & Isempty (pro)
Disp (' reading frames range exceeds the yuv video to the total number of frames. ');
Y=Y (:, :, 1: k - 1);
U=U (:, :, 1: k - 1);
V=V (:, :, 1: k - 1);
break;
End
Fseek (point, 1, 'cof package);
Temp=fread (point, [cols, rows], 'uchar');
Y (:, :, ii)=temp ';
Temp=fread (point, [cols/2 rows/2], 'uchar');
U (:, :, ii)=temp ';
Temp=fread (point, [cols/2 rows/2], 'uchar');
V (:, :, ii)=temp ';
End

The fclose (point);

End



For I=1:30

[] Y, U, V=ReadMultiFrames ('. \ mobile_cif_300f yuv ', 'cif, 30 [1]).
U=imresize (U, 2);
V=imresize (V, 2);
B=2.032 * U + Y;
R=1.140 * V + Y;
G=Y U - 0.581-0.394 * * V.
B=mat2gray (B);
R=mat2gray (R);
G=mat2gray (G);
F=cat (3, R, G, B);
% imshow (f);


Imwrite (f, [num2str (I), 'JPG'], 'JPG');
end

CodePudding user response:

For I=0:29
[] Y, U, V=ReadMultiFrames ('. \ mobile_cif_300f yuv ', 'cif, [, I]);
U=imresize (U, 2);
V=imresize (V, 2);
B=2.032 * U + Y;
R=1.140 * V + Y;
G=Y U - 0.581-0.394 * * V.
B=mat2gray (B);
R=mat2gray (R);
G=mat2gray (G);
F=cat (3, R, G, B);

Imwrite (f, strcat (num2str (I + 1), 'JPG'), 'JPG');
End
STR='H: \ \ MATLAB 05 \';
For I=1:30
Img2=imread ([STR, num2str (I), 'JPG']);
End
R=f (:, :, 1);
G=f (:, :, 2);
B=f (:, :, 3);
Y=G + + 0.587 * 0.114 * 0.299 * R B;
For I=she
Img3=Y (I + 1) - Y (I);
Img3=mat2gray (img3);
Imwrite (img3, strcat (num2str (I + 1), '. BMP '), 'BMP);
End