%对lena图像进行直方图均衡,给出处理前后的图像及其直方图 %用3*3的均值滤波器处理lena图像 %对lena图像施加(Pa=Pb=0.1)的椒盐噪声,然后采用3*3中值滤波器处理 %用Soble算子对lena图像进行锐化处理 %对lena图像进行直方图均衡,给出处理前后的图像及其直方图 clear all;close all; image=imread('D:\lena.bmp'); image_d=double(image); srcprobability=zeros(1,256); [M,N]=size(image); for m=1:M for n=1:N srcprobability(image_d(m,n)+1)=srcprobability(image_d(m,n)+1)+1; end end srcprobability=srcprobability./(M*N); %进行直方图均衡化处理 Sk=zeros(1,256); for i=1:256 if i==1 Sk(1)=srcprobability(1); else Sk(i)=Sk(i-1)+srcprobability(i); end end for i=1:256 Sk(i)=round(Sk(i)*255); end %将直方图均衡化的结果写到目标图像 image_hist=zeros(M,N); for m=1:M for n=1:N image_hist(m,n)=Sk(image_d(m,n)); end end drcprobability=zeros(1,256); for m=1:M for n=1:N drcprobability(image_hist(m,n)+1)=drcprobability(image_hist(m,n)+1)+1; end end drcprobability=drcprobability./(M*N); clear Sk; figure(1) subplot(1,2,1); bar(srcprobability); title('输入图像的直方图'); subplot(1,2,2); bar(drcprobability); title('进行直方图均衡化后的直方图'); figure(2) imshow(image); title('原始lena图像'); figure(3) image_hist=uint8(image_hist); imshow(image_hist); title('直方图均衡化处理后的lena图像'); %用3*3的均值滤波器处理lena图像 image_ave=zeros(M,N); %边缘值不处理 for i=1:N image_ave(1,i)=image_d(1,i); image_ave(M,i)=image_d(M,i); end for i=2:M-1 image_ave(i,1)=image_d(i,1); image_ave(i,N)=image_d(i,N); end for m=2:M-1 for n=2:N-1 template=image_d(m-1:m+1,n-1:n+1); template=reshape(template,1,9); image_ave(m,n)=sum(template)./9; end end figure(4) image_ave=uint8(image_ave); imshow(image_ave); title('用3*3的均值滤波器对lena图像进行处理后的结果'); %对lena图像施加(Pa=Pb=0.1)的椒盐噪声,然后采用3*3中值滤波器处理 image_noise=imnoise(image,'salt & pepper',0.1); %加入椒盐躁声 image_noise_d=double(image_noise); image_mid=zeros(M,N); %边缘值不处理 for i=1:N image_mid(1,i)=image_noise_d(1,i); image_mid(M,i)=image_noise_d(M,i); end for i=2:M-1 image_mid(i,1)=image_noise_d(i,1); image_mid(i,N)=image_noise_d(i,N); end for m=2:M-1 for n=2:N-1 template=image_noise_d(m-1:m+1,n-1:n+1); template=reshape(template,1,9); image_mid(m,n)=median(template); end end figure(5) clear image_noise_d; imshow(image_noise); title('施加(Pa=Pb=0.1)的椒盐噪声的lena图像'); figure(6) image_mid=uint8(image_mid); imshow(image_mid); title('用3*3的中值滤波器对加(Pa=Pb=0.1)的椒盐噪lena图像进行处理后的结果'); %用Soble算子对lena图像进行锐化处理 image_soble=zeros(M,N); %边缘值不处理 for i=1:N image_soble(1,i)=image_d(1,i); image_soble(M,i)=image_d(M,i); end for i=2:M-1 image_soble(i,1)=image_d(i,1); image_soble(i,N)=image_d(i,N); end for m=2:M-1 for n=2:N-1 template=image_d(m-1:m+1,n-1:n+1); template=reshape(template,1,9); Gx=abs(template(7)+2*template(8)+template(9)-template(1)-2*template(2)-template(3)); Gy=abs(template(3)+2*template(6)+template(9)-template(1)-2*template(4)-template(7)); image_soble(m,n)=Gx+Gy; end end figure(7) clear image_d; image_soble=uint8(image_soble); imshow(image_soble); title('用Soble算子对lena图像进行锐化处理的结果');