实验(1)使用给定的图像lena做实验,采用im2bw把灰度图像转换为二值图像,试计算二值化时阈值分别取0.2,0.4,0.6的压缩比是多少?
clc
close all
clear all
I=imread('D:/lena.jpg');
BW=im2bw(I,0.6);%二值化阈值
[zipped,info]=RLEencode(BW);
unzipped=RLEdecode(zipped,info);
subplot(1,3,1);imshow(I);xlabel('(a)原图');
subplot(1,3,2);imshow(BW);xlabel('(b)二值化后的图像');
subplot(1,3,3);imshow(uint8(unzipped)*255);xlabel('对(b)经游程编码及解码后的图像');
cr=info.ratio;
fprintf('cr = %8.5f\n',cr)
whos BW unzipped zipped
function [zipped,info]=RLEencode(vector)
[m,n]=size(vector);
vector=uint8(vector(:));
L=length(vector)
c=vector(1);
e(1,1)=double(c);
e(1,2)=0;
t1=1;
for j=1:L
if(vector(j)==c)
e(t1,2)=(e(t1,2))+1;
else
c=vector(j);
t1=t1+1;
e(t1,1)=double(c);
e(t1,2)=1;
end
end
zipped=e;
info.rows=m;info.cols=n;
[m,n]=size(e);
info.ratio=(info.rows*info.cols)/(m*n);
end
function unzipped=RLEdecode(zip,info)
[m,n]=size(zip);
unzipped=[];
for i=1:m
section=repmat(uint8(zip(i,1)),1,zip(i,2));
unzipped=[unzipped section];
end
unzipped=reshape(unzipped,info.rows,info.cols);
end
实验(2)以所给lena图像为例,采用dct进行图像压缩编码,其中模板矩阵mask分别设置为
mask = [1 1 1 1 1 0 0 0;1 1 1 1 0 0 0 0;1 1 1 0 0 0 0 0;1 1 0 0 0 0 0
0;1 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0],
mask = [1 1 1 1 0 0 0 0;1 1 1 0 0 0 0 0;1 1 0 0 0 0 0 0;1 0 0 0 0 0 0
0;0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0],
mask = [1 1 1 0 0 0 0 0;1 1 0 0 0 0 0 0;1 0 0 0 0 0 0 0;0 0 0 0 0 0 0
0;0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0],
可以得到不同的压缩编码图像,根据公式
,编写程序计算原图像和上述三种模板下得到不同的压缩编码图像之间的均方误差。
clc
close all
clear all
I=imread('D:/lena.jpg');
I=im2double(I);
T=dctmtx(8);
B=blkproc(I,[8 8],'P1*x*P2',T,T'); mask = [1 1 1 0 0 0 0 0;1 1 0 0 0 0 0 0;1 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0];
B2=blkproc(B,[8 8],'P1.*x',mask);
I2=blkproc(B2,[8 8],'P1*x*P2',T',T);
e=sqrt(mse(abs(I2-I)));
fprintf('均方误差 = %8.5f',e)
当mask = [1 1 1 0 0 0 0 0;1 1 0 0 0 0 0 0;1 0 0 0 0 0 0 0;0 0 0 0 0 0 0
0;0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0]时
参考:https://blog.youkuaiyun.com/uchihalyn/article/details/104593878