转自:http://hi.baidu.com/wang%5Fpw/blog/item/36354a637ac87b48eaf8f879.html
clc;
clear all;
I=imread('eight.tif');
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %用中值滤波,多维滤波,使用中心为-4,-8的拉普
% %拉斯滤波器,高斯低通滤波,拉普拉斯滤波器进行滤波处理
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure;%figure1
subplot(2,2,1);
imshow(I);
title('原始图像');
J=imnoise(I,'salt & pepper',0.04);%加椒盐噪声
title('加椒盐噪声');
subplot(2,2,2);
imshow(J);
K=medfilt2(J,[4,4])%进行中值滤波;
subplot(2,2,3);
imshow(K);
title('进行中值滤波');
h=ones(3,3)/9;%多维滤波
w=h;
g=imfilter(I,w,'conv','replicate')
subplot(2,2,4);
imshow(g);
title('多维滤波');
%使用中心为-4,-8的拉普拉斯滤波器,
w4=[1 1 1;
1 -4 1;
1 1 1];
w8=[1 1 1;
1 -8 1;
1 1 1];
f=im2double(I);
g4=f-imfilter(f,w4,'replicate');
g8=f-imfilter(f,w8,'replicate');
imshow(f);
figure;%figure2
subplot(1,2,1);
imshow(g4);
title('中心为-4的拉普拉斯滤波');
subplot(1,2,2);
imshow(g8);
title('中心为-8的拉普拉斯滤波');
h3=fspecial('gaussian',[3,3],0.5);%高斯低通滤波
figure;%figure3
B4=filter2(h3,I);
subplot(1,2,1);
imshow(B4,[ ]);
title('高斯低通滤波');
h4=fspecial('laplacian',0);%使用拉普拉斯滤波器
B5=filter2(h4,I);
subplot(1,2,2);
imshow(B5,[ ]);
title('拉普拉斯滤波器');
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %从空域的角度进行亮度变换
% %把灰度等级是10-100的变化到10-255
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure;%figure4
subplot(2,2,1);
imshow(I);
title('原始图像');
J2=imadjust(I,[],[],0.5);% 增强低灰度级
subplot(2,2,2);
imshow(J2);
title('增强低灰度级');
J3=imadjust(I,[ ],[ ],2);%增强高灰度级
subplot(2,2,3);
imshow(J3);
title('增强高灰度级');
a1=100/255;%把灰度等级是10-100的变化到10-255
a2=255/255;
a3=10/255;
J2=imadjust(I,[a3,a1],[a3,a2],[]);
subplot(2,2,4);
imshow(J2);
title('把灰度等级是10-100的变化到10-255');
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %从频域的角度进行亮度变换
% %fft2
% %由于能量主要集中在低频部分
% %所以对低频进行处理可以得到理想的效果
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
I=imread('eight.tif');
up=0.5;%设置处理频率上限
down=0.09%%设置处理频率下限
figure;%figure5
subplot(421);
imshow(I);
title('原始图像');
f=double(I);
subplot(4,2,3);
imshow(log(abs(f)),[]);
title('unit8转化为double');
g=fft2(f);
g=fftshift(g);
subplot(4,2,5);
imshow(log(abs(g)),[]);
title('FFT2变化后的图像');
[M,N]=size(g);% 转换数据矩阵
y1=max(max(abs(g)));%求出最大频率
y2=min(min(abs(g)));%%求出最小频率
y3=(y1-y2)*up+y2;%设置滤波上限
y4=(y1-y2)*down+y2;%%设置滤波下限
for i=1:M
for j=1:N
if (abs(g(i,j))<y4)
g(i,j)=g(i,j)^1.1;%对低频部分进行灰度增强
end
end
end
result=ifftshift(g);
J2=ifft2(result);
J3=uint8(abs(J2));
subplot(427);
imshow(J3,[ ]);
title('频域处理后的图像');
subplot(422)
imhist(I,64);
subplot(424)
imhist(f,64);
subplot(426)
imhist(g,64);
subplot(428)
imhist(J3,64);
*****************************
clc;
clear;
I=imread('a.jpg'); %读取图像
K1=medfilt2(I,[10 10]); %中值滤波
h1=ones(5,5)/25;
K2=imfilter(K1,h1); %均值滤波
figure, imshow(K2), title('滤波后的图像')