Compression video using Dct
7 views (last 30 days)
Show older comments
Hii
i need code for compression video using Dct, i want to read all frames frm video then apply Dct on each single frame please..
8 Comments
Answers (1)
Walter Roberson
on 5 Jan 2020
numFrames=v.NumberofFrames;
At that point in the code, v is not defined.
v=VideoWriter('S5.mp4');
The default "profile" for VideoWriter is AVI. If you do not tell VideoWriter specifically to create an MP4 then it will not create an MP4.
ifx = idct2(fx);
INV_img(i:i+N-1,j:j+N-1);
What is the purpose of those two lines?
open(v);
writeVideo(v,CINV_img);
close(v);
Do not open / write / close after changing each sub-block: open once near the beginning, and close once after all of the frames have been written, and only write completed frames.
DF = zeros(r,c);
%...
CINV_img = DF;
Your CINV_img array will be double precision. That is going to be a problem when you go to writeVideo: when you tell writeVideo to write data that is double precision, the data must be in the range 0 to 1. Your data will not be in the range 0 to 1 because you are starting with frames read from video, and those frames are going to be uint8 that is range 0 to 255. You will need to figure out a way to either convert your CINV_img range to 0 to 1, or convert it to not be floating point.
1 Comment
Walter Roberson
on 6 Jan 2020
There is an important factor that you need to consider.
You are, in a sense, doing data compression during your processing, in the sense that if you were to store just the parts of fx that you are not deliberately zeroing out, then you could take just those coefficients and reconstruct an approximation of the image from them -- by padding out each (depth x depth) sub-block to be N x N, doing idct2 on that, and storing the result in the appropriate place in the output array. You could store just the not-zeroed coefficients and process those later, and those would be the compressed data.
However, when you idct2 the adjusted fx, and create your CINV_img, that is a full array of data that will mostly not be zero; the restricted information content of the partly-zeroed fx will be "smeared out" by the idct2. The CINV_img that you build will have less information in it than the original image had, but the CINV_img array is full sized and not compressed.
You then take that full-sized CINV_img and pass it on to VideoWriter . VideoWriter is within its rights to need the full array size to write out the frame. As far a VideoWriter is concerned, what you are passing it is not compressed. Whether VideoWriter produces a smaller file than the original or not is not dependent on you having passed less data to VideoWriter: you are passing the full sized data to VideoWriter. If a reduced size of file is produced, it is only because VideoWriter is doing compression on the data.
You are doing dct compression, but you then decode the compressed version and give the uncompressed version to VideoWriter. VideoWriter is not necessarily going to do DCT compression on what you pass it. Indeed, with the MPEG-4 profile, it would do H.264 compression on it, rather than DCT compression.
As I do not have your sample video, I used the standard sample video rhinos.avi . I compressed with your algorithm, and I also sent through just grayscale frames in their original form, without any dct work. The output MPEG-4 for your version was 494115 bytes, and the output for not doing anything was 508937 bytes, which is only about 3% larger.
See Also
Categories
Find more on Audio and Video Data in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!