I1=imread('D:\1.JPG');
subplot(5,5,1);imshow(I1)
title('原图')
GrayImage=rgb2gray(I1);%将真彩图像转换为灰度图像
subplot(5,5,2);imshow(GrayImage);%显示灰度图像
title('原图的灰度图像')
subplot(5,5,3);imhist(GrayImage) % 在新图中显示图像的直方图
title('原图的直方图')
I2=histeq(GrayImage); % 均衡化以后的图像存在I2数组中(图像灰度值扩展到整个灰度范围,对比度提高)
%均衡化增强的实质是压缩直方图中比例较少的像素所占的灰度范围,多出来的灰度空间按照统计比例分配给直方图中比例较高的像素使用
subplot(5,5,4);imshow(I2) % 显示均衡化后的图像
title('均衡化后的图像')
subplot(5,5,5);imhist(I2) % 显示均衡后的灰度值分布情况
title('均衡化后的直方图')
B1=medfilt2(GrayImage,[4 4]); %4*4窗口
subplot(5,5,6);imshow(B1)
title('对自己预设的4*4图像进行中值滤波')
B2=medfilt2(GrayImage); %默认3*3滤波窗口
subplot(5,5,7);imshow(B2)
title('对原图像进行中值滤波')%首先确定一个以某个像素为中心点的邻域,然后将邻域中各像素的灰度值排序,取其中间值作为中心像素灰度的新值
%这里领域被称为窗口,当窗口移动时,利用中值滤波可以对图像进行平滑处理。但其对点、线和尖顶多的图像不宜采用中值滤波,很容易自适应化
P1=imnoise(GrayImage,'salt & pepper',0.02); %加入方差为0.02的椒盐躁声
subplot(5,5,8);imshow(P1)
title('加入椒盐躁声后的灰度图像')
P2=imnoise(GrayImage,'gaussian',0.02); %加入方差为0.02的高斯躁声(此方差是实际方差除以灰度级的平方)
subplot(5,5,9);imshow(P2)
title('加入高斯躁声后的灰度图像')
B3=medfilt2(P1);
subplot(5,5,10);imshow(B3)
title('中值滤波除椒盐躁声')
B4=medfilt2(P2);
subplot(5,5,11);imshow(B4)
title('中值滤波除高斯躁声')
h=[1 1 1;1 0 1;1 1 1];
h=h/8;
J1=conv2(GrayImage,h);%卷积滤波,C=conv2(A,B,shape):A:输入图像,B:卷积核,shape=same时,返回与A同样大小的卷积中心部分
subplot(5,5,12),imshow(J1,[]);
title('对自己预设的4*4图像进行邻域平均');%???
J2=conv2(GrayImage,h);
subplot(5,5,13),imshow(J2,[]);
title('对原图像进行邻域平均');%用几个像素灰度的平均值来代替每个像素的灰度。有效抑制加性噪声,但容易引起图像模糊
J3=conv2(P1,h);%
subplot(5,5,14),imshow(J3,[]);
title('邻域平均除椒盐躁声');
J4=conv2(P2,h);
subplot(5,5,15),imshow(J4,[]);
title('邻域平均除高斯躁声');
S=edge(GrayImage,'sobel');
subplot(5,5,16);imshow(S);title('sobel算子提取的边界')
H=fspecial('log'); %应用log算子锐化图像
A=filter2(H,GrayImage); %log算子滤波锐化
subplot(5,5,17);imshow(A);%显示Laplacian算子锐化图像
title('Laplacian算子锐化图像');
I3=255-I1; %转化为反色图像
subplot(5,5,18);imshow(I3) %显示反色图像
title('图像反色');
m = imadjust(I1,[,],[0.5;1]) ;%图像变亮
%J = imadjust(I,[low_in; high_in],[low_out; high_out])
%将图像I中的亮度值映射到J中的新值,即将low_in至high_in之间的值映射到low_out至high_out之间的值。
%low_in 以下与 high_in 以上的值被剪切掉了
subplot(5,5,19);imshow(m);title('图像变亮')
n = imadjust(I1,[,],[0;0.5]) ;%图像变暗
subplot(5,5,20);imshow(n);title('图像变暗')
bw=im2bw(GrayImage,0.5);%选取阈值为0.5(图像二值化处理:level一般为0.5,也可以用graythresh获取)
subplot(5,5,21);imshow(bw);title('二值图像(阈值为0.5)');
J=find(GrayImage<150);
GrayImage(J)=0;
J=find(GrayImage>=150);
GrayImage(J)=255;
subplot(5,5,22);imshow(GrayImage);title(' 图像二值化 ( 阈值为150 ) ');