图像存储压缩
记录数字媒体技术课程,第一次使用MATLAB。
二值图像的无损压缩存储
- 二值图像的读取
- 采用游程编码思路对图像进行压缩,输出编码文件
- 编码文件解码,恢复二值图像
clear
I = imread('image1.bmp');
X=I(:);%列优先顺序转换为一维数组
L=length(X);
X(L+1)=1;
z=1;
pn=1;
j=1;
for i=1:1:L
if X(i)==X(i+1)
pn=pn+1;
else
data(2*j)=X(i);
data(2*j-1)=pn;
j=j+1;
pn=1;
end
end
fid=fopen('exp1Output.txt','wt');
fprintf(fid,'%d ',data);
fclose(fid);
for i=1:1:L
Y(i)=X(i);
end
I2=reshape(Y,8,16);
imshow(I2);
图像的三通道提取
- 24位位图读取
- 分离图像 RGB 三通道,分别输出三通道对应的灰度图像
a=imread('image2.bmp');
channel_1=a;
channel_2=a;
channel_3=a;
c1=a(:,:,1);
c2=a(:,:,2);
c3=a(:,:,3);
imshow(c1);
显示图像
subplot(2,2,1);
imshow(c1,[]);
% % title('R通道');
% % subplot(2,2,2);
% % imshow(c2,[]);
% % title('G通道');
% % subplot(2,2,3);
imshow(c3,[]);
title('B通道');
subplot(2,2,4);
imshow(a,[]);
title('原图');
- 图像三通道分别赋值为零
a=imread('image2.bmp');
channel_1=a;
channel_2=a;
channel_3=a;
channel_1(:,:,1)=0;
channel_2(:,:,2)=0;
channel_3(:,:,3)=0;
% 显示图像
subplot(2,2,1);
imshow(channel_1,[]);
title('缺少R通道');
subplot(2,2,2);
imshow(channel_2,[]);
title('缺少G通道');
subplot(2,2,3);
imshow(channel_3,[]);
title('缺少B通道');
subplot(2,2,4);
imshow(a,[]);
title('原图');
图像的采样和量化
- 读取 8 位灰度图像
- 对图像进行采样(2:1、4:1),并输出
I1 = imread('lena.bmp');
subplot(131);
imshow(I1)
title('原始图像'); %输出该图像
I2=I1(1:2:end,1:2:end); %行列方向分别从第一位开始采样,
%每隔一位采样一个点,产生一个新的矩阵
subplot(132);
imshow(I2)
title('采样图像(1:2)');
I3=I1(1:4:end,1:4:end);
subplot(133)
imshow(I3)
title('采样图像(1:4)');
- 对图像进行量化,模拟生成 4 位、2 位、1 位灰度图像并输出‘’
figure(2);
image = imread('lena.bmp');
subplot(141);
imshow(image)
title('original')
f16=histeq(image,16);%将图像的灰度级数改为16
subplot(142);
imshow(f16)
title('16*16')
f4=histeq(image,4);%将图像的灰度级数改为4
subplot(143);
imshow(f4)
title('4*4')
f2=histeq(image,2);%将图像的灰度级数改为2
subplot(144);
imshow(f2)
title('2*2')
图像有损压缩
- 读取灰度图像
- 对图像进行傅里叶变换,输出频域图像,观察高频与低频特性
I=imread('lena.bmp');
I=im2double(I);
F=fft2(I);
F=fftshift(F);
F=abs(F);
T=log(F+1);
figure;
imshow(T,[]);
- 设计高通滤波器,对图像进行高通滤波,进行反傅里叶变换,输出高通滤波后图像
clc;
clear;
close all;
I=imread('lena.bmp'); %自己设置路径
subplot(1,3,1);
imshow(I);
title('原图');
saveas(gcf, '原图','jpg')
I=imnoise(I,'salt & pepper',0.1);
subplot(1,3,2)
imshow(I)
title('加入椒盐噪声');
f=double(I); % chage into double as MATLAB doesn’t suppor calculation
g=fft2(f); % fourier transform
g=fftshift(g); % zero-frequency area centralized
[M,N]=size(g);
dl0=50; %cutoff frequency
m=fix(M/2); n=fix(N/2);
for i=1:M
for j=1:N
d=sqrt((i-m)^2+(j-n)^2);
if(d<=dl0)
h=1;
else h=0;
end
result(i,j)=h*g(i,j);
end
end
result=ifftshift(result);
Gl1=ifft2(result);
Gl2=uint8(real(Gl1));
saveas(gcf, '低通滤波图','jpg')
subplot(1,3,3);
imshow(Gl2) ;
title('低通滤波图');
% dh0=30;
% for i=1:M
% for j=1:N
% d=sqrt((i-m)^2+(j-n)^2);
% if(d>=dh0)
% h=1;
% else h=0;
% end
% result(i,j)=h*g(i,j);
% end
% end
% result=ifftshift(result); %傅里叶反变换
% Gh1=ifft2(result);
% Gh2=uint8(real(Gh1)); %取实数部分
% subplot(1,2,2);
% imshow(Gh2) ;
% title('高通滤波图');
- 设计低通滤波器,对图像添加噪声,对图像进行低通滤波
clc;
clear;
close all;
I=imread('lena.bmp'); %自己设置路径
I=imnoise(I,'salt & pepper',0.1);
f=double(I); % chage into double as MATLAB doesn’t suppor calculation
g=fft2(f); % fourier transform
g=fftshift(g); % zero-frequency area centralized
[M,N]=size(g);
dl0=50; %cutoff frequency
m=fix(M/2); n=fix(N/2);
for i=1:M
for j=1:N
d=sqrt((i-m)^2+(j-n)^2);
if(d<=dl0)
h=1;
else h=0;
end
result(i,j)=h*g(i,j);
end
end
result=ifftshift(result);
Gl1=ifft2(result);
Gl2=uint8(real(Gl1));
imshow(Gl2) ;
saveas(gcf, '低通滤波图1','bmp')
- 将原图与滤波后图像分别从 bmp 格式另存为 jpg 格式
saveas(gcf, '低通滤波图','jpg')