Skip to content

Commit dbaef1d

Browse files
committed
init commit
0 parents  commit dbaef1d

16 files changed

+1274
-0
lines changed

CalMSE.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
% Calculate the MSE
2+
function MSE = CalMSE(orig_image, reco_image)
3+
4+
clear dist_image;
5+
dist_image = orig_image - reco_image;
6+
sum_image = dist_image.*dist_image;
7+
sumdist = sum(sum(sum(sum(sum_image))));
8+
[rownum, colnum, dimension, imagenum] = size(orig_image);
9+
MSE = sumdist/(rownum*colnum*dimension*imagenum);
10+
11+
12+

Compratio.m

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
% Calculate the compression ratio
2+
% between the original images and the compressed images;
3+
function comp_ratio = Compratio(orig_image, comp_image)
4+
5+
% Calculate how many bits should be used to represented the original images
6+
% and store it in the variable B0
7+
clear tempmatr1;
8+
tempmatr1 = ceil(log2(orig_image+1));
9+
clear sizevector1;
10+
sizevector1 = size(orig_image);
11+
[rownum, colnum] = size(sizevector1);
12+
while colnum >1
13+
clear tempmatr2;
14+
tempmatr2 = sum(tempmatr1);
15+
clear tempmatr1;
16+
tempmatr1 = tempmatr2;
17+
colnum = colnum -1;
18+
end
19+
B0 = sum(tempmatr1);
20+
21+
% Calculate how many bits should be used to represented the compressed images
22+
% and store it in the variable B1
23+
clear tempvec1;
24+
tempvec1 = find(comp_image<0);
25+
clear tempmatr1;
26+
if sum(tempvec1) == 0
27+
tempmatr1 = ceil(log2(comp_image+1));
28+
else
29+
tempmatr1 = ceil(log2(abs(comp_image)+1))+1;
30+
end
31+
clear sizevector1;
32+
sizevector1 = size(comp_image);
33+
[rownum, colnum] = size(sizevector1);
34+
while colnum >1
35+
clear tempmatr2;
36+
tempmatr2 = sum(tempmatr1);
37+
clear tempmatr1;
38+
tempmatr1 = tempmatr2;
39+
colnum = colnum -1;
40+
end
41+
B1 = sum(tempmatr1);
42+
comp_ratio = B0/B1;

Compress.m

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
% Image compression funtion
2+
function [comp_image_Y,comp_image_U,comp_image_V] = Compress(orig_image)
3+
4+
RGB=orig_image;
5+
%下面是对RGB三个分量进行分离
6+
R=RGB(:,:,1);
7+
G=RGB(:,:,2);
8+
B=RGB(:,:,3);
9+
10+
%RGB->YUV
11+
Y=0.299*double(R)+0.587*double(G)+0.114*double(B);
12+
[xm, xn] = size(Y);
13+
U=-0.169*double(R)-0.3316*double(G)+0.5*double(B);
14+
V=0.5*double(R)-0.4186*double(G)-0.0813*double(B);
15+
16+
%产生一个8*8的DCT变换举证
17+
T=dctmtx(8);
18+
%进行DCT变换 BY BU BV是double类型
19+
BY=blkproc(Y,[8 8],'P1*x*P2',T,T');
20+
BU=blkproc(U,[8 8],'P1*x*P2',T,T');
21+
BV=blkproc(V,[8 8],'P1*x*P2',T,T');
22+
%低频分量量化表
23+
a=[
24+
16 11 10 16 24 40 51 61;
25+
12 12 14 19 26 58 60 55;
26+
14 13 16 24 40 57 69 55;
27+
14 17 22 29 51 87 80 62;
28+
18 22 37 56 68 109 103 77;
29+
24 35 55 64 81 104 113 92;
30+
49 64 78 87 103 121 120 101;
31+
72 92 95 98 112 100 103 99;
32+
33+
];
34+
%高频分量量化表
35+
b=[17 18 24 47 99 99 99 99;
36+
18 21 26 66 99 99 99 99;
37+
24 26 56 99 99 99 99 99;
38+
47 66 99 99 99 99 99 99;
39+
99 99 99 99 99 99 99 99;
40+
99 99 99 99 99 99 99 99;
41+
99 99 99 99 99 99 99 99;
42+
99 99 99 99 99 99 99 99;];
43+
44+
%使用量化表对三个分量进行量化
45+
BY2=blkproc(BY,[8 8],'round(x./P1)',a);
46+
BU2=blkproc(BU,[8 8],'round(x./P1)',b);
47+
BV2=blkproc(BV,[8 8],'round(x./P1)',b);
48+
49+
%调用压缩函数
50+
comp_image_Y=img2jpg(BY2,1);
51+
comp_image_U=img2jpg(BU2,2);
52+
comp_image_V=img2jpg(BV2,3);

Decompress.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
function reco_image = Decompress(orig_image_Y,orig_image_U,orig_image_V)
3+
%解压缩
4+
YI=jpg2img(orig_image_Y);
5+
UI=jpg2img(orig_image_U);
6+
VI=jpg2img(orig_image_V);
7+
8+
%YUV转为RGB
9+
RI=YI-0.001*UI+1.402*VI;
10+
GI=YI-0.344*UI-0.714*VI;
11+
BI=YI+1.772*UI+0.001*VI;
12+
13+
%经过DCT变换和量化后的YUV图像
14+
RGBI=cat(3,RI,GI,BI);
15+
RGBI=uint8(RGBI);
16+
reco_image = RGBI;

Image1.bmp

297 KB
Binary file not shown.

Image2.bmp

297 KB
Binary file not shown.

Image3.bmp

297 KB
Binary file not shown.

Image4.bmp

297 KB
Binary file not shown.

Projec1.jpg

39.6 KB
Loading

Projec2.jpg

41.3 KB
Loading

0 commit comments

Comments
 (0)