1.将附件中的图像1转换成灰度图像,然后进行霍夫曼编码和解码
I = imread('1.jpg');
I = imresize(I,0.5); %将图像大小缩小,因为霍夫曼编码编译时间好长
J = rgb2gray(I);
subplot(1,2,1)
imshow(J);title('编码图像')
[M,N] = size(J);
SIG = J(:); %转为一维向量
T = imhist(J); %灰度直方图
TRatio = T/sum(T); %每一个灰度出现比率
TRatioNO = find(TRatio~=-1);%编号排序出现的灰度值
dict = huffmandict(TRatioNO,TRatio); %霍夫曼字典函数
hcode = huffmanenco(SIG,dict); %霍夫曼编码
dhsig = huffmandeco(hcode,dict); %霍夫曼译码
% isequal(SIG,dhsig)
decoimage = reshape(dhsig,M,N); %将一维向量转为二位矩阵
subplot(1,2,2); %将二维矩阵转为灰度图像
imshow(mat2gray(decoimage));title('解码图像')
结果:
2.对附件中的图像1进行低通滤波,图像2进行高通滤波,然后将滤波结果合并,给出合并后的空域图像
d0 = 30; %低通阈值
D0 = 30; %高通阈值
W = 16; %窗口的宽度
Img1 = imread('1.jpg');
Img2 = imread('2.jpg');
s = size(Img1);
S = size(Img2);
Img1cut = Img1(2*W:s(1)-5*W,W:s(2)-4*W); %裁剪对齐五官
Img2cut = Img2(4*W:S(1)-W,3*W:S(2)-3*W); %裁剪对齐五官
% imshow(Img2cut); %测试
imag1 = imresize(Img1cut,[180*2,180*2],'bilinear');
imag2 = imresize(Img2cut,[180*2,180*2],'bilinear');
% imshow(imag1); %不需要???
% imag1 = rgb2gray(imag1);
% imag2 = rgb2gray(imag2);
%傅里叶变换
imag1_f = fftshift(fft2(imag1)); %傅里叶变换得到频谱
imag2_f = fftshift(fft2(imag2)); %移到中间
%低通滤波
[a,b] = size(imag1_f);
a0 = floor(a/2); %中心点坐标
b0 = floor(b/2); %中心点坐标
h = zeros(a,b); %高斯低通滤波器构造
for i = 1:a
for j = 1:b
d = ((i-a0)^2+(j-b0)^2);
h(i,j) = exp(-(d)/(2*(d0^2)));
end
end
imag1_lpf = h.*imag1_f;
imag1_lpf = ifftshift(imag1_lpf);
imag1_lpf = uint8(real(ifft2(imag1_lpf)));
subplot(1,3,1);imshow(imag1_lpf);title('低通滤波梦露酱');
%高通滤波
[m,n] = size(imag2_f);
m0 = floor(m/2);
n0 = floor(n/2);
H = zeros(m,n); %%高斯高通滤波器构造
for x = 1:m
for y = 1:n
D = ((x-m0)^2+(y-n0)^2);
H(x,y) = 1-exp(-(D)/(2*(D0^2)));
end
end
imag2_lpf = H.*imag2_f;
imag2_lpf = ifftshift(imag2_lpf);
imag2_lpf = uint8(real(ifft2(imag2_lpf)));
all_white = 255*ones(m,n);
all_white_uin8 = uint8(all_white);
imag2_lpf = imsubtract(all_white_uin8,imag2_lpf);
subplot(1,3,2);imshow(imag2_lpf);title('高通滤波爱因斯坦');
%合并
outimage = ifftshift(h.*imag1_f+H.*imag2_f);
outimage = uint8(real(ifft2(outimage)));
subplot(1,3,3);imshow(outimage);title('爱露');
结果: