子程序运行不了,不知道怎么调试?
img=imread('E:\yzh\hftp.jpg');%读入原图像
img_0=rgb2gray(img);
img_1=imnoise(img_0,'salt & pepper',0.02);
img_2=medfilt2(img_1);%中值滤波
subplot(2,2,1);imshow(img);title('原始图像');
subplot(2,2,2);imshow(img_0);title('灰度图像');
subplot(2,2,3);imshow(img_1);title('椒盐噪声');
figure;
imshow(img_2);title('中值滤波后图像');
hz=fspecial('log');
t=imfilter(img_2,hz);
BW=edge(t,'prewitt');
figure;
imshow(t);title('锐化');
imshow(BW);title('边缘提取');
%裁剪图像
J=imcrop(BW,[100 75 500 275]);
imwrite(J,'BW_part.jpg');
X=imread('BW_part.jpg');
imshow(X);title('剪切图片');
%图像压缩
[r,c]=size(X);%图像尺寸
A=double(X);%转换双精度
T=dctmtx(8);%离散余弦变换矩阵
B=blkproc(A,[8 8],'P1*x*P2',T,T');
%对原图像进行DCT变换
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];
B2=blkproc(B,[8 8],'P1*x',mask);
%数据压缩,丢弃右下角高频数据
M=blkproc(B2,[8 8],@lianghua);%量化
%imshow(M);
%huffman编码
H=huff06get(M)
%huffman解码
f=Huff06put(H)
N=reshape(f,r,c);
N =N+minM;
T=blkproc(N,[8 8],@ilianghua); %反量化
I=blkproc(T,[8 8],@idct2); %8*8DCT反变换
%图像重建
I=uint8(I);
imshow(X);
axis square;xlabel('原图像');
imshow(I);
axis square;xlabel('压缩图像');
function H=huff06get(M)
[m,n]=size(M);
re=m*n;
p=zeros(1,61);
for t=1:61
count=0;
for i=1:m
for j=1:n
if M(i,j)==t-1
count=count+1;
end
end
end
p(t)=count;p0=p;
end
core=cell(61,1);
sign=zeros(61);
for hh=1:60
for t=1:61
if (p(t)<re)&(p(t)>0)
re=p(t);
end
end
t=1;
while (p(t)~=re)&(t<61)
t=t+1;
end
if sign(t,1)==0
core{t}='0';
else
core{t}=['0',core{t}];
i=1;
while (sign(t,i)~=0)&(i<61)
core{sign(t,i)}=['0',core{sign(t,i)}];
i=i+1;
end
end
p(t)=0;
cou=t;
re1=m*n;
for t=1:61
if (p(t)<re1)&(p(t)>0)
re1=p(t);
end
end
t=1;
while (p(t)~=re1)&(t<61)
t=t+1;
end
if sign(t,1)==0
core{t}='1';
else
core{t}=['1',core{t}];
i=1;
while (sign(t,i)~=0)&(i<61)
core{sign(t,i)}=['1',core{sign(t,i)}];
i=i+1;
end
end
p(t)=p(t)+re;
cou1=t;
i=1;
while (sign(t,i)~=0)&(i<61)
i=i+1;
end
sign(t,i)=cou;
i=i+1;
j=1;
while (sign(cou,j)~=0)&(j<61)
sign(t,i)=sign(cou,j);
i=i+1;
j=j+1;
end
end %产生huffman码
Mc=cell(m,n);
for i=1:m
for j=1:n
if (M(i,j)<61)&(M(i,j)>0)
Mc{i,j}=core{M(i,j)+1};
else
Mc{i,j}='0';
end
end
end
imcore=char2cell[1,re];
for i=1:m
for j=1:n
imcore=[imcore,Mc{i,j}];
end
end
save picture imcore core; %保存图片码流和编码对应表
function f=Huff06put(H)
clear;
Nc=size(core);
Nic=size(imcore);
flag=0;
i=1;
j=1;
q=1;
cz=char2cell();
f=zeros(128);
for q=1:400930
if flag==0
cz=[cz,imcore(q)];
else
cz=imcore(q);
flag=0;
end
for t=1:(s-1)
if strcmp(cz,core{t})
f(j,i)=t;
flag=1;
if i>127;
i=1;
j=j+1;
else
i=i+1;
end
break;
end
end
end
f=uint8(f*4+35);