📄 bp-compress.m
字号:
%3月19日
clc;
clear;
close all;
%I=imread('lena1.jpg');
rgb=imread('mandrill.jpg');
rgb=im2double(rgb);
r=rgb(:,:,1);%取值在[0,1]之间
g=rgb(:,:,2);
b=rgb(:,:,3);
%I=imresize(I,[128,128]);
P=[];
for i=1:64
for j=1:64
I2=r((i-1)*4+1:i*4,(j-1)*4+1:j*4);
i3=reshape(I2,16,1);
II=double(i3);
P_1=II; %P_1=II/255;%对于值在0-255之间的灰度图像需要归一化
P=[P,P_1];
end
end
Trainset=P(:,1:200);%选择训练样本的大小
Targetset=Trainset;
%T=P;%训练样本集
net=newff(minmax(P),[8,16],{'tansig','logsig'},'trainlm');%隐层数目为8
net.trainParam.goal=0.001;
net.trainParam.epochs=500;
%训练神经网络,并计算训练时间
tic
net=train(net,Trainset,Targetset);%net=train(net,P,T);
toc
%压缩
Y_chonggou=sim(net,P);
Ychonggou_ceshi=[];
%重建
for k=1:4096
Ychonggou_ceshi1=reshape(Y_chonggou(:,k),4,4);
Ychonggou_ceshi=[Ychonggou_ceshi,Ychonggou_ceshi1];
end
YYchonggou_ceshi=[];
for k=1:64
YYchonggou_ceshi1=Ychonggou_ceshi(:,(k-1)*256+1:k*256);
YYchonggou_ceshi=[YYchonggou_ceshi;YYchonggou_ceshi1];
end
%转换为8bite图像
Ychonggou_ce=uint8(YYchonggou_ceshi*255);
%显示图像
subplot(1,2,1),imshow(r)
title('原始图像');
subplot(1,2,2),imshow(Ychonggou_ce)
title('S=8');
%绿色分量
G=[];
for i=1:64
for j=1:64
I2=g((i-1)*4+1:i*4,(j-1)*4+1:j*4);
i3=reshape(I2,16,1);
II=double(i3);
G_1=II; %P_1=II/255;%对于值在0-255之间的灰度图像需要归一化
G=[G,G_1];
end
end
Y_chonggou_g=sim(net,G);%压缩
Ychonggou_ceshi_g=[];
%重建
for k=1:4096
Ychonggou_ceshi1_g=reshape(Y_chonggou_g(:,k),4,4);
Ychonggou_ceshi_g=[Ychonggou_ceshi_g,Ychonggou_ceshi1_g];
end
YYchonggou_ceshi_g=[];
for k=1:64
YYchonggou_ceshi1_g=Ychonggou_ceshi_g(:,(k-1)*256+1:k*256);
YYchonggou_ceshi_g=[YYchonggou_ceshi_g;YYchonggou_ceshi1_g];
end
%转换为8bite图像
Ychonggou_ce_g=uint8(YYchonggou_ceshi_g*255);
%蓝色分量
B=[];
for i=1:64
for j=1:64
I2=b((i-1)*4+1:i*4,(j-1)*4+1:j*4);
i3=reshape(I2,16,1);
II=double(i3);
B_1=II; %P_1=II/255;%对于值在0-255之间的灰度图像需要归一化
B=[B,B_1];
end
end
Y_chonggou_b=sim(net,B);
Ychonggou_ceshi_b=[];
%重建b
for k=1:4096
Ychonggou_ceshi1_b=reshape(Y_chonggou_b(:,k),4,4);
Ychonggou_ceshi_b=[Ychonggou_ceshi_b,Ychonggou_ceshi1_b];
end
YYchonggou_ceshi_b=[];
for k=1:64
YYchonggou_ceshi1_b=Ychonggou_ceshi_b(:,(k-1)*256+1:k*256);
YYchonggou_ceshi_b=[YYchonggou_ceshi_b;YYchonggou_ceshi1_b];
end
%转换为8bite图像
Ychonggou_ce_b=uint8(YYchonggou_ceshi_b*255);
%合成RGB图像
compress_rgb=cat(3,double(Ychonggou_ce)/255,double(Ychonggou_ce_g)/255,double(Ychonggou_ce_b)/255);
compress_rgb=max(min(compress_rgb,1),0);
figure;
subplot(1,2,1);imshow(rgb);title('原彩色图像');
subplot(1,2,2);imshow(compress_rgb);title('压缩后彩色图像');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -