图像预处理的一般方法(一)空域图像增强技术1.灰度线性变换addpath('C:\');I=imread('C:\lzs.jpeg');imshow(I);I=double(I);[M,N]=size(I);fori=1:Mforj=1:NifI(i,j)=30I(i,j)=I(i,j);elseifI(i,j)=150I(i,j)=(210-30)/(160-30)*(I(i,j)-30)+30;elseI(i,j)=(256-210)/(256-160)*(I(i,j)-160)+210;endendendendfigure(2);imshow(uint8(I));2.直方图均衡化addpath('C:\');I=imread('C:\lzs.jpeg');figuresubplot(221);imshow(I);subplot(222);imhist(I);I1=histeq(I);figure;subplot(221);imshow(I1);subplot(222);imhist(I1)3.均值滤波functiontest1I=imread('C:\lzs.jpeg');[M,N]=size(I);II1=zeros(M,N);fori=1:16;II(:,:,i)=imnoise(I,'gaussian',0,0.01);II1=II1+double(II(:,:,i));ifor(or(i==1,i==4),or(i==8,i==16));figure;imshow(uint8(II1/i));endend4.梯度锐化操作addpath('C:\');I=imread('C:\lzs.jpeg');subplot(131);imshow(I);H=fspecial('Sobel');H=H';TH=filter2(H,I);subplot(132);imshow(TH,[]);H=H';TH=filter2(H,I);subplot(133),imshow(TH,[]);(二)图像分割1.迭代式阈值选择法addpath('C:\');f=imread('C:\lzs.jpeg');subplot(1,2,1);imshow(f);title('原始图像');f=double(f);T=(min(f(:))+max(f(:)))/2;done=false;i=0while~doner1=find(f=T);r2=find(fT);Tnew=(mean(f(r1))+mean(f(r2)))/2;done=abs(Tnew-T)1T=Tnew;i=i+1;endf(r1)=0;f(r2)=1;subplot(1,2,2)imshow(f);title('迭代阀值二值化图像');2.用Otsu法进行阈值选择addpath('C:\');f=imread('C:\lzs.jpeg');subplot(2,2,1)imshow(f);title('原始图像');T=graythresh(f);g=im2bw(f,T);subplot(2,2,2);imshow(g);title('Otsu方法二值化图像');3.用watershed算法分割图像addpath('C:\');f=imread('C:\lzs.jpeg');subplot(2,2,1)imshow(f);title('(a)原始图像');subplot(2,2,2);f=double(f);hv=fspecial('prewitt');hh=hv.';gv=abs(imfilter(f,hv,'replicate'));gh=abs(imfilter(f,hv,'replicate'));g=sqrt(gv.^2+gh.^2);subplot(2,2,2);L=watershed(g);wr=L==0;imshow(wr);title('(b)分水岭');f(wr)=255;subplot(2,2,3);imshow(uint8(f));title('(c)分割结果');rm=imregionalmin(g);subplot(2,2,4);imshow(rm);title('(d)局部极小值');(三)膨胀和腐蚀1.膨胀操作addpath('C:\');I=imread('C:\yb.jpg');subplot(121);imshow(I);title('原始图像')se=strel('ball',8,8);I2=imdilate(I,se);subplot(122);imshow(I2);title('膨胀后的图像')2.腐蚀操作addpath('C:\');I=imread('C:\yb.jpg');subplot(121);imshow(I);title('原始图像')se=strel('ball',8,8);I2=imerode(I,se);subplot(122);imshow(I2);title('腐蚀操作后的图像')