数字图像处理之matlab实验(二):图像增强

图像增强只要是指利用一系列线性或非线性变换,增强图像质量,如提高对比度、增强感兴趣区域的细节。主要有三种方式:线性变换、非线性变换、直方图均衡化。

1、线性变换

其中imhist 是显示其直方图,imadjust 是依据参数调整原图像的直方图分布,进而实现图像增强。

I=imread('medicine_pic.jpg');
g=imhist(I,256);            %显示其直方图
I1=imadjust(I,[0 1],[1 0]);    %灰度转换,实现明暗转换(负片图像)
I2=imadjust(I,[0.5 0.75],[0 1]); %将0.5到0.75的灰度级扩展到范围[0 1]
figure;
subplot(131),imshow(I),title("原图像")
subplot(132),imshow(I1),title("图像取反")
subplot(133),imshow(I2),titel("部分灰度级扩张")

2、非线性变换

非线性变换主要的作用有两个:增强暗区域的对比度或增加亮区域的对比度。 在进行非线性变换时,需要注意对数函数、指数函数、幂次函数的曲线。比如,对数函数会扩张低灰度区域压缩高灰度区域,因此低灰度区域细节会增强,图像整体会变亮,而指数函数则相反。

Image=(rgb2gray(imread('Goldilocks.bmp')));  
imwrite(Image,'GGoldilocks.bmp');
Image=double(Image);
NewImage1=46*log(Image+1);     %对数函数非线性灰度级变换
NewImage2=185*exp(0.325*(Image-225)/30)+1;%指数函数非线性灰度级变换
a=0.5; c=1.1;
NewImage3=[(Image/255).^a]*255*c; %幂次函数非线性灰度级变换
imwrite(uint8(NewImage1),'Goldilocks1.bmp');
imwrite(uint8(NewImage2),'Goldilocks2.bmp');
imwrite(uint8(NewImage2),'Goldilocks3.bmp');
imshow(Image,[]);title('Goldilocks灰度图像');
figure;imshow(NewImage1,[]);title('对数函数非线性灰度级变换');
figure;imshow(NewImage2,[]);title('指数函数非线性灰度级变换');
figure;imshow(NewImage3,[]);title('幂次函数非线性灰度级变换');

3、直方图均衡化

通过直方图均衡化增强图像对比度。可以看出,均衡化之后,像素灰度值分布更均匀。

I=imread('pollen.jpg');     % 读入原图像
J=histeq(I);             %对原图像进行直方图均衡化处理
 
figure; 
subplot(1,4,1);imshow(I);title('原图像');            %显示原图像
subplot(1,4,2);imhist(I,64);title('原图像直方图');   %将原图像直方图显示为64级灰度
subplot(1,4,3);imshow(J);title('直方图均衡化后的图像'); %显示直方图均衡化后的图像       
subplot(1,4,4);imhist(J,64);title('均衡变换后的直方图');%将均衡化后图像的直方图显示为64级灰度  

4、自编程实现直方图相关操作

好吧,感觉上一步直接调用histeq太简单了,然后开始作,毕竟是上了理论课的,然后自己编代码实现直方图均衡化。人生就是自虐的过程。其他没啥好说的,要自己理解。

grayimage=imread('pollen.jpg');
subplot(2,2,1),imshow(grayimage),title('原始图像');
[m,n]=size(grayimage);

gp=zeros(1,256);  %gp用来存原图像的每一个灰度值占总像素数目的比例
for i=1:256    
    gp(i)=length(find(grayimage==(i-1)))/(m*n);
end
subplot(2,2,2),bar(0:255,gp),title('原始图像直方图');  %显示原图像的直方图

s1=zeros(1,256);   %s1存储gp中元素的累加和
s2=zeros(1,256);   %s2求直方图均衡化之后,原灰度值对应的新灰度值
temp=0;
for i=1:256
    temp=temp+gp(i);
    s1(i)=temp;
end
s2=round(s1*255);%乘上255,再向下取整

newgp=zeros(1,256);
for i=1:256
     newgp(i)=sum(gp(find(s2==(i-1))));   %1.思考为什么需要用sum函数求和?
end


newgrayimage=grayimage;
for i=1:256
    newgrayimage(find(grayimage==(i-1)))=s2(i);
end
subplot(2,2,3),imshow(newgrayimage),title('直方图均衡化图像');
subplot(2,2,4),bar(0:255,newgp),title('均衡后直方图'); %显示均衡化后直方图
imwrite(newgrayimage,'new_pollen.jpg');%保存图像

猜你喜欢

转载自blog.csdn.net/u014655960/article/details/127494237