📄 jpegandwhitenoise_sys_demo.m
字号:
% JPEGdemo.m
% Prototype JPEG compression algorithm demostration
%
% copyright (c) 1997-2002 by Yu Hen Hu
%
% This algorithm only demonstrate the basic
% JPEG functionalities.
% It is not necessarily a faithful
% implementation of JPEG.
% Its output will not be binary bit streams
% either, but rather
% an integer stream of 0 and 1s
% Only gray scale picture is considered
%
% Last modification: 11/6/2002
clear all
clc
% Load data
chos=0; % default choice
%load p64int.txt;f=p64int; clear p64int;
load lena.mat
f=x;%(1+128:128+128,1+128:128+128);
imshow(mat2gray(f))
clear x
%echo on
% level shift by 128
f=f-128;
%pause
drawnow
[mf,nf]=size(f); mb=mf/8; nb=nf/8;
% size of f, # of blocks of f
% Step 1. 2D separable DCT on each 8x8
% blocks
Ff=blkproc(f,[8 8],'dct');
% apply DCT to each column of each block of f
Ff=blkproc(Ff',[8 8],'dct');
% apply DCT to each row of each block of Ff
Ff=round(Ff');
% transpose back to proper orientation
%pause
% Perceptual scaler quantization
%
Q =[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];
% this is the quantization matrix shown in figure 8.37 in the textbook
%pause
% Now perform rounding
Fq=round(blkproc(Ff,[8 8],'divq',Q));
%pause
%echo off
% DPCM of DC component, scaned row-wise
if mb*nb > 1,
fdc=reshape(Fq(1:8:mf,1:8:nf)',mb*nb,1);
fdpcm=dpcm(fdc,1);
else
fdpcm=Fq(1,1);
end
dccof=[];
for i=1:mb*nb,
dccof=[dccof jdcenc(fdpcm(i))];
end
%pause
%echo on
% Zig-Zag scanning of AC coefficients
z=[1 2 6 7 15 16 28 29
3 5 8 14 17 27 30 43
4 9 13 18 26 31 42 44
10 12 19 25 32 41 45 54
11 20 24 33 40 46 53 55
21 23 34 39 47 52 56 61
22 35 38 48 51 57 60 62
36 37 49 50 58 59 63 64];
%pause
%echo off
acseq=[];
for i=1:mb
for j=1:nb
tmp(z)=Fq(8*(i-1)+1:8*i,8*(j-1)+1:8*j);
% tmp is 1 by 64
eobi=max(find(tmp~=0)); %end of block index
% eob is labelled with 999
acseq=[acseq tmp(2:eobi) 999];
end
end
accof=jacenc(acseq);
EbN0db=2.0;
en = 10^(EbN0db/10);
sigma = 1/sqrt(2*en);
accof1=AWGN(accof,15,'measured');
accof1=round(accof1);
accof1=rem(accof1,2);
accof1=abs(accof1);
dccof1=AWGN(dccof,15,'measured');
dccof1=round(dccof1);
dccof1=rem(dccof1,2);
dccof1=abs(dccof1);
%dccof=dccof*2-zeros(1,length(dccof));
%dccof = dccof+sigma*randn(1,length(dccof));
%dccof=round(dccof);
% dccof=abs(dccof);
%dccof1= rem(dccof,2);
cha1=xor(dccof,dccof1);
cha2=xor(accof,accof1);
sum(cha1,2)
sum(cha2,2)
% Inverse JPEG i.e reconstruction of image lena
%clear,clc
% accof and dccof are from jpegdemo.m , run it first
acarr=jacdec(accof1);
dcarr=jdcdec(dccof1);
% Assumed that image size is 256 X 256, recostruction begins
load lena.mat % To find MSE, we need to have original image
subplot 121
imshow(mat2gray(x)),title(' 原始图像')
drawnow
Q =[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];
z=[1 2 6 7 15 16 28 29
3 5 8 14 17 27 30 43
4 9 13 18 26 31 42 44
10 12 19 25 32 41 45 54
11 20 24 33 40 46 53 55
21 23 34 39 47 52 56 61
22 35 38 48 51 57 60 62
36 37 49 50 58 59 63 64];
z=z(:);
mb=256/8; nb=256/8; % Number of blocks
Eob=find(acarr==999);
kk=1;ind1=1;n=1;
for ii=1:mb
for jj=1:nb
ac=acarr(ind1:Eob(n)-1);
ind1=Eob(n)+1;
n=n+1;
ri(8*(ii-1)+1:8*ii,8*(jj-1)+1:8*jj)=dezz([dcarr(kk) ac zeros(1,63-length(ac))]);
kk=kk+1;
end
end
iFq=round(blkproc(ri,[8 8],'idivq',Q));
iFf=blkproc(iFq,[8 8],'idct2');
iFf=round(iFf+128);
subplot 122
imshow(mat2gray(iFf)),title(' 还原图像')
% Calculate MSE , SNR
MSE=mean(mean((x-iFf).^2)) % Doubt about formulae
SNR=10*log10(255^2/MSE) % Doubt about formulae
%MSE = 156.6631
%SNR = 26.1811
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -