当前位置:文档之家› 附录:程序源代码

附录:程序源代码


%%%%%%%%%%%%基于matlab的图像处理系统设计与实现(高曙光)%%%%%%%%%%%%%%%

采用imfinfo函数查询图像文件的信息。其语句格式如下:
Info=imfinfo(‘biyesheji.jpg’)
----------------------------------------------------------------------
采用rgb2gray函数进行真彩色图像与灰阶强度图像的转变其格式调用如下
A=imread('biyesheji.jpg');
imshow(A);
I=rgb2gray(A);
figure;
imshow(I)
-----------------------------------------------------------------------
用于调整图像的灰度值或颜色图。其格式调用如下:、
i=imread('biyesheji.jpg');
j=imadjust(i,[0.3 0.7],[]);
imshow(i);
figure;
imshow(j)
%%%%%%%%
Histeq 直方图均衡
i=imread('biyesheji.jpg');
j=histeq(i);
imshow(i);
figure
imshow(j)
-----------------------------------------------------------------------
3种噪声污染了的图像:
I=imread('biyesheji.Jpg'); %读取图像
I1=imnoise(I,'gaussian'); %加高斯噪声
I2=imnoise(I,'salt & pepper',0.02); %加椒盐噪声
I3=imnoise(I,'speckle'); %加乘性噪声
subplot(221),imshow(I); %显示图像I
title('原图像');
subplot(222),imshow(I1);
title('高斯噪声污染的图像');
subplot(223),imshow(I2);
title('椒盐噪声污染的图像');
subplot(224),imshow(I3);
title('乘性噪声污染的图像');
-----------------------------------------------------------------------
两个平均值滤波算子对图像进行滤波操作。
取H1,程序如下:
I=imread('biyesheji.jpg');
I1=imnoise(I,'gaussian');
I2=imnoise(I,'salt & pepper',0.02);
I3=imnoise(I,'speckle');
H1=ones(3,3)/9; %3×3领域模板
J=imfilter(I,H1); %领域平均
J1=imfilter(I1,H1);
J2=imfilter(I2,H1);
J3=imfilter(I3,H1);
subplot(221),imshow(J);
subplot(222),imshow(J1);
subplot(223),imshow(J2);
subplot(224),imshow(J3);
运行结果如图
取H2,程序如下:
RGB=imread('biyesheji.jpg');
I=rgb2gray(RGB);
I1=imnoise(I,'gaussian');
I2=imnoise(I,'salt & pepper',0.02);
I3=imnoise(I,'speckle');
H2=ones(5,5)/25; %5×5领域模板
J=imfilter(I,H2); %领域平均
J1=imfilter(I1,H2);
J2=imfilter(I2,H2);
J3=imfilter(I3,H2);
subplot(221),imshow(J);
title('原图像滤波后');
subplot(222),imshow(J1);
title('高斯污染图像滤波后');
subplot(223),imshow(J2);
title('椒盐污染图像滤波后');
subplot(224),imshow(J3);
title('乘法污染图像滤波后');
----------------------------------------------------------------------
二维滤波器d;
[f1,f2]=freqspace(25,'meshgrid');
Hd=zeros(25,25);
d=sqrt(f1.^2+f2.^2)<0.5; %0.5为截止半径大小
Hd(d)=1;
h=fsamp2(Hd);
figure,freqz2(h,[64,64]);
----------------------------------------------------------------------
用所构建的二维滤波器对以上图像进行滤波
RGB=imread('biyesheji.jpg');
I=rgb2gray(RGB);
I1=imnoise(I,'gaussian');
I2=imnoise(I,'salt & pepper',0.02);
I3=imnoise(I,'speckle');
J=imfilter(I,h,'replicate');
J1=imfilter(I1,h,'replicate');

J2=imfilter(I2,h,'replicate');
J3=imfilter(I3,h,'replicate');
subplot(221),imshow(J);
title('原图像滤波后');
subplot(222),imshow(J1);
title('高斯污染图像滤波后');
subplot(223),imshow(J2);
title('椒盐污染图像污染后');
subplot(224),imshow(J3);
title('乘法污染图像滤波后');
---------------------------------------------------------------------
图像放大和缩小
X=imresize(RGB,0.5,'nearest');
figure
imshow(X)
title('最邻近插入法')
Y=imresize(RGB,2,'bilinear');
figure
imshow(Y)
title('双三次插入法')
-----------------------------------------------------------------------
图像任意角度的旋转
B=imrotate(RGB,90,'nearest','crop'); %图像旋转90°。
figure
imshow(B)
title('图像角度旋转')
imwrite(B,’text.tif’)
-----------------------------------------------------------------------
图像直方图统计和均衡
figure
imhist(I)
title('直方图')
figure
H=histeq(I);
imshow(H)
title('直方图均衡处理图片')
figure
imhist(H)
title('直方图均衡')
---------------------------------------------------------------------
图像分析-边缘检测
hg=zeros(3,3); %设定高斯平滑滤波模板的大小为3*3
delta=0.5;
for x=1:1:3
for y=1:1:3
u=x-2;
v=y-2;
hg(x,y)=exp(-(u^2+v^2)/(2*pi*delta^2));
end
end
h=hg/sum(hg(:));
%%%%%%%%%%读入图像%%%%%%%
g = imread('biyesheji.jpg'); % 读入图像文件
f=rgb2gray(im2double(g));
subplot(2,2,1),imshow(f)
title('原始图像');
[m,n]=size(f);
ftemp=zeros(m,n);
rowhigh=m-1;
colhigh=n-1;
%%%高斯滤波%%%
for x=2:1:rowhigh-1
for y=2:1:colhigh-1
mod=[f(x-1,y-1) f(x-1,y) f(x-1,y+1); f(x,y-1) f(x,y) f(x,y+1);f(x+1,y-1) f(x+1,y) f(x+1,y+1)];
A=h.*mod;
ftemp(x,y)=sum(A(:));
end
end
f=ftemp
subplot(2,2,2),,imshow(f)
title('高斯滤波器后的图像');
%%利用第一种算法进行边缘检测%%%
%%%%3*3的prewitt算子%%%%%%%%
sx=[-1 0 1;-1 0 1;-1 0 1];
sy=[-1 -1 -1;0 0 0;1 1 1];

for x=2:1:rowhigh-1
for y=2:1:colhigh-1
mod=[f(x-1,y-1) f(x-1,y) f(x-1,y+1); f(x,y-1) f(x,y) f(x,y+1);f(x+1,y-1) f(x+1,y) f(x+1,y+1)];
fsx=sx.*mod;
fsy=sy.*mod;
ftemp(x,y)=sqrt((sum(fsx(:)))^2+(sum(fsy(:)))^2);
end
end
fs=im2uint8(ftemp);
subplot(2,2,3),imshow(fs)
title('用prewitt检测的原始图像');
%%%域值分割%%%
TH2=200; %设定阈值
for x=2:1:rowhigh-1
for y=2:1:colhigh-1
if (fs(x,y)>=TH2)&((fs(x,y-1) <= fs(x,y)) & (fs(x,y) > fs(x,y+1)) )
fs(x,y)=200;
elseif(fs(x,y)>=TH2)&( (fs(x-1,y) <=fs(x,y)) & (fs(x,y) >fs(x+1,y)))
fs(x,y)=200;
else fs(x,y)=50;
end
end
end
subplot(2,2,4),imshow(fs)
title('用prewitt检测并细化后的图像');
-----------------------------------------------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%结束&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

相关主题
文本预览
相关文档 最新文档