本文系《数字图像处理原理与实践(MATLAB版)》一书之代码系列的Part5,辑录该书第225至第280页之代码(此处应部分读者之需求调整了代码发布的顺序——详细说明请见下面的文章链接),供有需要读者下载研究使用。代码执行结果请参见原书配图,建议下载代码前阅读下文:
关于《数字图像处理原理与实践(MATLAB版)》一书代码发布的说明
http://blog.csdn.net/baimafujinji/article/details/40987807
P245
I = imread(‘lena.png‘);
fcoef=fft2(double(I)); %FFT变换
tmp1 =log(1+abs(fcoef));
spectrum = fftshift(fcoef); %调整中心
tmp2 = log(1+abs(spectrum));
ifcoef = ifft2(fcoef); %逆变换
figure %显示处理结果
subplot(2,2,1), imshow(I), title(‘source image‘);
subplot(2,2,2), imshow(tmp1,[]), title(‘FFT image‘);
subplot(2,2,3), imshow(tmp2,[]), title(‘shift FFT image‘);
subplot(2,2,4), imshow(ifcoef,[]), title(‘IFFT image‘);
P251
J= double(imread(‘lena.bmp‘));
K = dct2(J);
figure, imshow(K,[0 255])
P252-1
J= double(imread(‘lena.bmp‘));
K = dct2(J);
figure, imshow(K,[0 255]);
K_i = idct2(K);
figure, imshow(K_i,[0 255])
P252-2
J= double(imread(‘lena.bmp‘));
A = J(1:8,1:8);
D = dctmtx(8);
dct_1 = D*A;
dct_2 = D‘*dct_1;
P252-3
J= double(imread(‘lena.bmp‘));
A = J(1:8,1:8);
D = dctmtx(8);
dct_1 = D*A*D‘;
dct_2 = dct2(A);
P253
I = imread(‘cameraman.tif‘);
I = im2double(I);
T = dctmtx(8);
dct = @(block_struct) T * block_struct.data * T‘;
B = blockproc(I,[8 8],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 = blockproc(B,[8 8],@(block_struct) mask .* block_struct.data);
invdct = @(block_struct) T‘ * block_struct.data * T;
I2 = blockproc(B2,[8 8],invdct);
imshow(I), figure, imshow(I2)
P262
a = [0 0 1 1 0 0 1 1];
b = fwht(a);
P263
I = imread(‘baboon.bmp‘);
I1 = double(I);
T = hadamard(8);
myFun1 = @(block_struct)T*block_struct.data*T/64;
H = blockproc(I1, [8 8], myFun1);
H(abs(H)<3.5)=0;
myFun2 = @(block_struct)T*block_struct.data*T;
I2 = blockproc(H, [8 8], myFun2);
subplot(121), imshow(I1,[]), title(‘original image‘);
subplot(122), imshow(I2,[]), title(‘zipped image‘);
P264
I = imread(‘baboon.bmp‘);
I1 = double(I);
[m n] =size(I);
sizi = 8;
num = 16;
%分块进行离散沃尔什变换
T = hadamard(sizi);
myFun1 = @(block_struct)T*block_struct.data*T/(sizi.^2);
hdcoe = blockproc(I1, [sizi, sizi], myFun1);
%重新排列系数
coe = im2col(hdcoe, [sizi, sizi], ‘distinct‘);
coe_t = abs(coe);
[Y, ind] = sort(coe_t);
%舍去绝对值较小的系数
[m_c, n_c] = size(coe);
for i = 1:n_c
coe(ind(1:num, i), i)=0;
end
%重建图像
re_hdcoe = col2im(coe, [sizi, sizi], [m, n], ‘distinct‘);
myFun2 = @(block_struct)T*block_struct.data*T;
re_s = blockproc(re_hdcoe, [sizi, sizi], myFun2);
subplot(121), imshow(I1,[]), title(‘original image‘);
subplot(122), imshow(re_s,[]), title(‘compressed image‘);
P268
dim1 = [1 1 1 2 2 2 3 3 3];
dim2 = [1 2 3 1 2 3 1 2 3];
dim3 = [63 75 78 50 56 65 70 71 80];
sum( (dim1-mean(dim1)) .* (dim2-mean(dim2)) ) / ( 9-1 ) % 0
sum( (dim1-mean(dim1)) .* (dim3-mean(dim3)) ) / ( 9-1 ) % 0.625
sum( (dim2-mean(dim2)) .* (dim3-mean(dim3)) ) / (9-1 ) % 5
std(dim1)^2 % 0.75
std(dim2)^2 % 0.75
std(dim3)^2 % 100.7778
P274
X = [2 2; 2 3; 3 4; 4 3; 5 4; 5 5];
[COEFF,SCORE,latent,tsquare] = princomp(X);
P275-1
X0=X-repmat(mean(X),6,1);
SCORE_1 = X0*COEFF;
P275-2
X = [2 2; 2 3; 3 4; 4 3; 5 4; 5 5];
V = cov(X);
[COEFF,latent] = pcacov(V)
P277
I = imread(‘baboon.bmp‘);
x = double(I)/255;
[m,n]=size(x);
y =[];
%拆解图像
for i = 1:m/8;
for j = 1:n/8;
ii = (i-1)*8+1;
jj = (j-1)*8+1;
y_app = reshape(x(ii:ii+7,jj:jj+7),1,64);
y=[y;y_app];
end
end
%KL变换
[COEFF,SCORE,latent] = princomp(y);
kl = y * COEFF;
kl1 = kl;
kl2 = kl;
kl3 = kl;
%置零压缩过程
kl1(:, 33:64)=0;
kl2(:, 17:64)=0;
kl3(:, 9:64)=0;
%KL逆变换
kl_i = kl*COEFF‘;
kl1_i = kl1*COEFF‘;
kl2_i = kl2*COEFF‘;
kl3_i = kl3*COEFF‘;
image = ones(256,256);
image1 = ones(256,256);
image2 = ones(256,256);
image3 = ones(256,256);
k=1;
%重组图像
for i = 1:m/8;
for j = 1:n/8;
y = reshape(kl_i(k, 1:64),8,8);
y1 = reshape(kl1_i(k, 1:64),8,8);
y2 = reshape(kl2_i(k, 1:64),8,8);
y3 = reshape(kl3_i(k, 1:64),8,8);
ii = (i-1)*8+1;
jj = (j-1)*8+1;
image(ii:ii+7,jj:jj+7) = y;
image1(ii:ii+7,jj:jj+7) = y1;
image2(ii:ii+7,jj:jj+7) = y2;
image3(ii:ii+7,jj:jj+7) = y3;
k=k+1;
end
end
(代码发布未完,请待后续...)