Help understanding Maltab code.
조회 수: 4 (최근 30일)
이전 댓글 표시
Hi, I need some help understaing some matlab code:
The DJEPG function takes an input of 8x8 matrix and outputs a 63x1 array, I understand that it goes through the matrix in a specific zigzag way to extract elements. I need to understand how it does that and how could I reverse this process? It'd be very helpful if you could comment on some of the code and how would reversing the process work?
Edit: I have included the JPEG function that computes the the inputs for the DJPEG function that I'm trying to implement, added the entire code instead of the small section.
Here is the JPEG function that computes the inputs for my function:
% Compute the quantised coefficients for a given 8x8 jpeg region
% tile - 8x8 greyscale region (0..255)
% Q - quality factor 1..100 (eg. 80).
% dc_coeff - quantised dc coefficient of DCT
% ac_coeff - quantised ac coefficients (63) of DCT in zig-zag order
function [dc_coeff,ac_coeff] = jpeg_8x8(tile,Q)
% quantisation table used to quantise DCT coefs
Qtable = [ 16 11 10 16 24 40 51 61 ; ...
12 12 14 19 26 58 60 55 ; ...
14 13 16 24 40 57 69 56 ; ...
14 17 22 29 51 87 80 62 ; ...
18 22 37 56 68 109 103 77 ; ...
24 35 55 64 81 104 113 92 ; ...
49 64 78 87 103 121 120 101 ; ...
72 92 95 98 112 100 103 99 ];
% centre image data about greylevel 128
tile=double(tile)-128;
% Q scale factor used in quantisation step
if (Q<=50)
qt_scale = 50/Q;
else
qt_scale = 2-Q/50;
end
% get raw DCT coeffs of tile
Y = dct(tile);
% quantise coefficients
Yq = round(Y./(Qtable*qt_scale));
% get DC coefficient (top left entry in DCT)
dc_coeff = Yq(1,1);
% zig zag along rows
ll = 1; mm = 2; ac_count = 1; direction = 1;
ac_coeff=zeros(1,63);
for kk = 3:16
if (direction)
for ll = max(1,kk-8):min(kk-1,8)
ac_coeff(ac_count) = Yq(min(8,ll),kk-min(8,ll));
ac_count = ac_count+1;
end
else
for ll = max(1,kk-8):min(kk-1,8)
ac_coeff(ac_count) = Yq(kk-min(8,ll),min(8,ll));
ac_count = ac_count+1;
end
end
direction = 1-direction;
end
Now my task is to reverse the order of these steps to acquire the original image by implenting the a dJPEG function.
Here is the DJPEG function that I'm trying to implement:
% Compute the quantised coefficients for a given 8x8 jpeg region
% tile - 8x8 greyscale region (0..255)
% Q - quality factor 1..100 (eg. 80).
% dc_coeff - quantised dc coefficient of DCT
% ac_coeff - quantised ac coefficients (63) of DCT in zig-zag order
function tile = djpeg_8x8(dc_coeff,Out,Q)
% quantisation table used to quantise DCT coefs
Qtable = [ 16 11 10 16 24 40 51 61 ; ...
12 12 14 19 26 58 60 55 ; ...
14 13 16 24 40 57 69 56 ; ...
14 17 22 29 51 87 80 62 ; ...
18 22 37 56 68 109 103 77 ; ...
24 35 55 64 81 104 113 92 ; ...
49 64 78 87 103 121 120 101 ; ...
72 92 95 98 112 100 103 99 ];
% init quantised 8x8 coef array
Zq=zeros(8,8);
% 1. copy DC back in
% 2. order zig-zag access and copy AC back
% 3. Q scale factor used in quantisation step
% 4. estimate original Z coefficients using Zq etc
% 5. inverse dct (assign to variable 'tile')
% centre image data about greylevel 128
tile=uint8(tile+128);
Thank you for the help!
댓글 수: 4
Adam Danz
2019년 5월 2일
The way I reverse-engineer a script is to load some data and step through the code, line by line, in dubug mode. I look at what each line produces; sometimes I plot it out. Sometimes I can just read the code and have a good idea of what's going on at some level. For example, I understand every step of your code syntactically but I don't know how it's stepping through the diagram you shared because I don't have the values for the variables (even if I had the values, I wouldn't be able to devote much time into it).
Without having any other information other than your speculation that this code steps through the diagram you shared, my guess is that "direction" decides which direction to step next. But something's puzzling to me. 'direction' starts as a 1 and then decreases to 0, then to -1 etc because of this line:
direction = 1-direction
Yet the for-loops are conditiond on 'direction' being true which is the case any time 'direction' does not equal 0. Maybe there's another section of the code that resets this variable. This, along with other poor coding habits like vague variable names makes it difficult to follow what's going on.
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!