MatLab 数字图像处理实验 图像压缩

实验(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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值