可以看出,原图像的灰度有所变化。 (4)、任选一副灰度图像对其进行幂次变换,选择不同的r(分别取r=0.5,r= 1,r=5),观察分析其显示结果 参考程序如下: clear all; close all;
I0=imread(‘test.jpg’); I0=I0/255;
I1=I0.^0.5;%r=0.5幂次变换 I2=I0.^5; %r=5幂次变换
subplot(1,3,1),imshow(I0,[]),title(‘原图’)
subplot(1,3,2), imshow(I0,[]),title(‘r=0.5幂次变换’); subplot(1,3,2), imshow(I0,[]),title(‘r=5幂次变换’);
从显示结果可以看出来,r<1时,黑色区域被扩展,变得清晰,当r<1,黑色区域被压缩,变得几乎不可见。
(5)、练习教学资料上分段线性灰度变换例子。
可练习的参考程序如下,该程序可实现灰度变换(2)题的操作:
x1=imread('pout.tif');
subplot(221),imshow(X1);title('原图') f0=0;g0=0; f1=60;g1=30; f2=180;g2=240; f3=255;g3=255; % 绘制变换曲线
subplot(222),plot([f0 f1 f2 f3],[g0 g1 g2 g3]);
axis tight,xlabel('f'),ylabel('g'),title('灰度变换曲线') r1=(g1-g0)/(f1-f0);b1=g0-r1*f0; r2=(g2-g1)/(f2-f1);b2=g1-r2*f1; r3=(g3-g2)/(f3-f2);b3=g2-r3*f2; [m n]=size(X1); X2=double(X1);
% 变换矩阵中的每一个元素 for I=1:m for J=1:n f=X2(I,J); g(I,J)=0;
if (f>=0)&(f<=f1) g(I,J)=r1*f+b1; elseif (f>f1)&(f<=f2) g(I,J)=r2*f+b2; elseif (f>f2)&(f<=f3) g(I,J)=r3*f+b3; end end end
subplot(223),imshow(mat2gray(g));title('灰度变换图')
显示结果如下:
2、直方图变换(直方图均衡化和直方图规定化)实现图像增强(histeq,imhist函数) (1)、任选一幅灰度图像,显示原图像,绘制其及直方图,然后对图像进行均衡化处理,并显示处理后图像及其直方图,与原图像作比较; 参考程序如下:
I=imread('Lenna.tif'); M=histeq(I);
J0=histeq(I,32);
subplot(2,4,1),imshow(I);title(‘原图’);
subplot(2,4,2),imhist(I); title(‘原图直方图’);
subplot(2,4,3),imshow(M); title(‘直方图均衡化图像’);
subplot(2,4,4),imhist(M); title(‘直方图均衡化后的直方图’);
subplot(2,4,5),imshow(J0); title(‘32个灰度级直方图均衡化图像’); subplot(2,4,6),imhist(J0); title(‘32个灰度级直方图均衡化的直方图’);
[counts,x]=imhist(J); J1=histeq(Q,counts); subplot(2,4,7),imshow(J1); title(‘32个灰度级直方图规定化’);
subplot(2,4,8),imhist(J1); title(‘32个灰度级直方图规定化的直方图’); %可将J0%与J1进行比较
显示结果如下:
(2)、对原图做直方图规定化,目标直方图服从正态分布----N(4,8),观察经过规定化处理的图像效果。 clear all; close all;
I0=rgb2gray(imread('lena.jpg')); %构造正态分布直方图
N=32;2个灰度级 i=0:N-1;
Hist0=exp(-(i-1).^2/8); Hist0=Hist0/sum(Hist0); %Hist_cu=cumsum(Hist0); I1=histeq(I0,Hist0);
subplot(3,2,1),imshow(I0,[]),title('原图'); subplot(3,2,2),imhist(I0),title('原图直方图')
subplot(3,2,3),stem([0:31],Hist0),title('正态分布目标直方图');%正态分布直方图
subplot(3,2,5),imshow(I1,[]),title('直方图规定化后的图像'); subplot(3,2,6),imhist(I1),title('直方图规定化后的图像直方图');
图像做过均衡化处理后比原图的对比度增强了,图像的细节更加清楚了,均衡化后的直方图比原直方图分布均匀了;直方图规定化是以直方图均衡化做基础,可以按人的意愿去变换,选择合适的目标直方图来实现规定化,可以突出感兴趣的灰度范围,使用起来更灵活。
3、空间域滤波
(1)、选择任一副图像作为源图像,使用函数imnoise分别添加两种不同的噪声,用ordfilt2实现图像增强,然后设置不同的参数,比较显示结果有什么不同。 参考考程序如下:
I = imread('moon.tif');
J = imnoise(I,'salt & pepper',0.04); G = imnoise(I,'gaussian',0.04);
K1 = ordfilt2(J,1,ones(3,3));%最小滤波 K2 = ordfilt2(J,9,ones(3,3));%最大滤波 K3 = ordfilt2(J,5,ones(3,3));%中值滤波 K4 = ordfilt2(G,1,ones(3,3)); K5 = ordfilt2(G,9,ones(3,3)); K6 = ordfilt2(G,5,ones(3,3));
subplot(3,3,1);imshow(I);title(‘原图’)
subplot(3,3,2);imshow(J); title(‘添加椒盐噪声后’) subplot(3,3,3);imshow(G); title(‘添加高斯噪声后’) subplot(3,3,4);imshow(K1); title(‘最小滤波椒盐噪’) subplot(3,3,5);imshow(K2); title(‘最大滤波椒盐噪’) subplot(3,3,6);imshow(K3); title(‘中值滤波椒盐噪’) subplot(3,3,7);imshow(K4); title(‘最小滤波高斯噪’) subplot(3,3,8);imshow(K5); title(‘最大滤波高斯噪’) subplot(3,3,9);imshow(K6); title(‘中值滤波高斯噪’)
(2)、请采用二维中值滤波函数medfilt2对受椒盐噪声干扰的任意一副图像滤波,窗口分别采用3*3,5*5,显示结果(提示:首先对图像添加椒盐噪声,最后调用mefilter2函数滤波) 参考程序如下:
I = imread('moon.tif');
J = imnoise(I,'salt & pepper',0.04);
K1 = medfilt2(J,[3 3]); K2= medfilt2(J,[5 5]);
subplot(2,2,1);imshow(I);title(‘原图’)
subplot(2,2,2);imshow(J);title(‘添加椒盐噪声后的图像’) subplot(2,2,3);imshow(K1);title(‘3*3中值滤波’) subplot(2,2,4);imshow(K2);title(‘5*5中值滤波’)
(3)、选择合适的MATLAB函数对受高斯噪声干扰的一副图像进行均值滤波(提示:首先对图像添加高斯噪声,然后用fspecial函数构造均值滤波器h,最后调用filter2函数滤波); 参考程序如下:
I=imread('Lenna.tif');
K=imnoise(I,'gaussian',0.04); h1=fspecial('average',[3,3]); h2=fspecial('average',[5,5]); J=filter2(h1,I); M=filter2(h2,I);
subplot(2,2,1),imshow(I,[]);title(‘原图’)
subplot(2,2,2),imshow(K,[]);title(‘加高斯噪声后’)
subplot(2,2,3),imshow(J,[]);title(‘3*3均值滤波高斯噪声后’) subplot(2,2,4),imshow(M,[]); title(‘5*5均值滤波高斯噪声后’) (4)、任意采用三种不同锐化算子对任一副图像进行锐化处理,显示原图像及处理后的图像效果。(可以用fpecial,也可使用直接写出算子) 参考程序如下:
I=imread('Lenna.tif'); h1=fspecial('unsharp',0);
I1=filter2(h1,I);h2=fspecial('sobel');%h2=[]可直接写sobel算子矩阵 I2=filter2(h2,I);
h3=fspecial('prewitt');%h3=可直接写算子矩阵 I3=filter2(h3,I);
subplot(221),imshow(I,[]); subplot(222),imshow(I1,[]); subplot(223),imshow(I2,[]); subplot(224),imshow(I3,[]);
(5)、选择任意幅图像作为源图像,练习使用fspecial和filter2函数实现各种算子如:拉普拉斯,sobel,prewitt等完成图像锐化,比较各算子的操作效果有何不同。
参考程序如下:注:各算子也可直接写出算子的形式
I=imread('coins.png');
h1=fspecial('unsharp',0); % 二维拉普拉斯滤器h1=[-1 -1 -1;-1 9 -1;-1 -1 -1]; I1=filter2(h1,I); h2=fspecial('sobel'); I2=filter2(h2,I);
h3=fspecial('prewitt'); I3=filter2(h3,I);
subplot(221),imshow(I,[]); subplot(222),imshow(I1,[]);
subplot(223),imshow(I2,[]); subplot(224),imshow(I3,[]);

