数字图像处理
一、 图像复原
1、(1)matlab程序代码(使用电路板图片)
a=imread('C:\Users\SUN\Desktop\a.tif'); %读图像
b=imnoise(a,'gaussian',0,800/255^2); %加入0均值,方差800的噪声。
c=imnoise(b,'salt & pepper',0.1); % 在(a)的基础上,进一步加入椒盐噪声进一步污染图像( Pa=Pb=0.1的椒盐噪声)
subplot(2,4,1),imshow(a) ,title('原始图像') %显示图片
subplot(2,4,2),imshow(b) ,title('高斯噪声污染')
subplot(2,4,3),imshow(c) ,title('继续被椒盐噪声污染')
h=ones(5,5)/25; %算术均值滤波
g1=imfilter(c,h);
subplot(2,4,4),imshow(g1) ,title('算数均值滤波')
%g2=power(exp(imfilter(log(double(c)),4*fspecial('average',2))),1/4); %几何均值滤波
g2=exp(imfilter(log(double(c)),fspecial('average',1)));
subplot(2,4,5),imshow(uint8(g2)) ,title('几何均值滤波')
g3= medfilt2(c,[5,5]); %中值滤波
subplot(2,4,6),imshow(g3) ,title('中值滤波')
i=ones(5,5)/20;
g4=imfilter(c,i);
subplot(2,4,7),imshow(g4) ,title('修正后的α均值滤波')
运行结果:
(2)matlab程序代码(使用含有自己的图片)
a=imread('C:\Users\SUN\Desktop\i.jpg'); %读图像
a1=rgb2gray(a);
b=imnoise(a1,'gaussian',0,800/255^2); %加入0均值,方差800的噪声。
c=imnoise(b,'salt & pepper',0.1); % 在(a)的基础上,进一步加入椒盐噪声进一步污染图像( Pa=Pb=0.1的椒盐噪声)
subplot(2,4,1),imshow(a) ,title('原始图像') %显示图片
subplot(2,4,2),imshow(b) ,title('高斯噪声污染')
subplot(2,4,3),imshow(c) ,title('继续被椒盐噪声污染')
h=ones(5,5)/25; %算术均值滤波
g1=imfilter(c,h);
subplot(2,4,4),imshow(g1) ,title('算数均值滤波')
%g2=power(exp(imfilter(log(double(c)),4*fspecial('average',2))),1/4); %几何均值滤波
g2=exp(imfilter(log(double(c)),fspecial('average',1)));
subplot(2,4,5),imshow(uint8(g2)) ,title('几何均值滤波')
g3= medfilt2(c,[5,5]); %中值滤波
subplot(2,4,6),imshow(g3) ,title('中值滤波')
i=ones(5,5)/20;
g4=imfilter(c,i);
subplot(2,4,7),imshow(g4) ,title('修正后的α均值滤波')
运行结果:
2、(1)matlab程序代码(使用电路板图片)
A=imread('C:\Documents and Settings\Administrator\桌面\a.tif'); %读图像
B=imnoise(A,'gaussian',0,1000/255^2); %加入0均值,方差1000的噪声
subplot(2,3,1),imshow(A) ,title('原始图像'); %显示图片
subplot(2,3,2),imshow(B) ,title('高斯噪声污染');
h=ones(7,7)/49;
g1=imfilter(B,h);
subplot(2,3,4),imshow(g1) ,title('算数均值滤波') ; %算数均值滤波
g2=power(exp(imfilter(log(double(B)),49*fspecial('average',7))),1/49); %几何均值滤波
subplot(2,3,5),imshow(uint8(g2)) ,title('几何均值滤波');
for n=1:1:444
for m=1:1:444
D=B(n:n+6,m:m+6);
C=B(n+3,m+3);
a=var(double(D(:)));
b=sum(double(D(:)))/numel(D);
E=C-1000/225^2/a*(C-b);
F(n,m)=uint8(E);
end
end
g3=F;
subplot(2,3,6),imshow(g3) ,title('自适应噪声消减滤波') %自适应噪声消减滤波
运行结果:
(2)matlab程序代码(使用含有自己的图片)
A=imread('C:\Documents and Settings\Administrator\桌面\i.jpg'); %读图像
I=rgb2gray(A);
B=imnoise(I,'gaussian',0,1000/255^2); %加入0均值,方差1000的噪声
subplot(2,3,1),imshow(I) ,title('原始图像'); %显示图片
subplot(2,3,2),imshow(B) ,title('高斯噪声污染');
h=ones(7,7)/49;
g1=imfilter(B,h);
subplot(2,3,4),imshow(g1) ,title('算数均值滤波') ; %算数均值滤波
g2=power(exp(imfilter(log(double(B)),49*fspecial('average',7))),1/49); %几何均值滤波
subplot(2,3,5),imshow(uint8(g2)) ,title('几何均值滤波');
for n=1:1:359 %自适应噪声消减滤波
for m=1:1:398
D=B(n:n+6,m:m+6);
C=B(n+3,m+3);
a=var(double(D(:)));
b=sum(double(D(:)))/numel(D);
E=C-1000/225^2/a*(C-b);
F(n,m)=uint8(E);
end
end
g3=F;
subplot(2,3,6),imshow(g3) ,title('自适应噪声消减滤波')
运行结果:
3、(1)matlab程序代码(使用电路板图片)
a=imread('C:\Users\SUN\Desktop\a.tif'); %读图像
b=imnoise(a,'salt & pepper',0.25); %加入概率Pa=Pb=0.25的椒盐噪声污染了的图像
subplot(2,2,1),imshow(a) ,title('原始图像') %显示图片
subplot(2,2,2),imshow(b) ,title('椒盐噪声污染')
g1= medfilt2(b,[7,7]); %中值滤波
subplot(2,2,3),imshow(g1) ,title('中值滤波')
[Im,In]=size(b); %自适应中值滤波
nmin=3;
nmax=7;
Imf=b;
I_ex=[zeros((nmax-1)/2,In+(nmax-1));zeros(Im,(nmax-1)/2),b,zeros(Im,(nmax-1)/2);zeros((nmax-1)/2,In+(nmax-1))];
for x=1:Im
for y=1:In
for n=nmin:2:nmax
Sxy=I_ex(x+(nmax-1)/2-(n-1)/2:x+(nmax-1)/2+(n-1)/2,y+(nmax-1)/2-(n-1)/2:y+(nmax-1)/2+(n-1)/2);
Smax=max(max(Sxy));
Smin=min(min(Sxy));
Smed=median(median(Sxy));
if (Smed>Smin && Smed
=Smax)
Imf(x,y)=Smed;
end
break
end
end
Imf(x,y)=Smed;
end
end
subplot(2,2,4),imshow(Imf) ,title('自适应中值滤波')
(2)matlab程序代码(使用含有自己的图片)
a=imread('C:\Users\SUN\Desktop\i.jpg'); %读图像
a1=rgb2gray(a);
b=imnoise(a1,'salt & pepper',0.25); %加入概率Pa=Pb=0.25的椒盐噪声污染了的图像
subplot(2,2,1),imshow(a) ,title('原始图像') %显示图片
subplot(2,2,2),imshow(b) ,title('椒盐噪声污染')
g1= medfilt2(b,[7,7]); %中值滤波
subplot(2,2,3),imshow(g1) ,title('中值滤波')
[Im,In]=size(b); %自适应中值滤波
nmin=3;
nmax=7;
Imf=b;
I_ex=[zeros((nmax-1)/2,In+(nmax-1));zeros(Im,(nmax-1)/2),b,zeros(Im,(nmax-1)/2);zeros((nmax-1)/2,In+(nmax-1))];
for x=1:Im
for y=1:In
for n=nmin:2:nmax
Sxy=I_ex(x+(nmax-1)/2-(n-1)/2:x+(nmax-1)/2+(n-1)/2,y+(nmax-1)/2-(n-1)/2:y+(nmax-1)/2+(n-1)/2);
Smax=max(max(Sxy));
Smin=min(min(Sxy));
Smed=median(median(Sxy));
if (Smed>Smin && Smed=Smax)
Imf(x,y)=Smed;
end
break
end
end
Imf(x,y)=Smed;
end
end
subplot(2,2,4),imshow(Imf) ,title('自适应中值滤波')
clear
运行结果:
二、图像增强
(1)频域锐化增强
matlab程序代码:
I=imread('C:\Users\SUN\Desktop\b.tif'); %读图像
subplot(1,2,1),imshow(I); title('原图像');
s=fftshift(fft2(I));
[a,b]=size(s);
a0=round(a/2);
b0=round(b/2);
d=10;
p=0.2;q=0.5;
for i=1:a
for j=1:b
distance=sqrt((i-a0)^2+(j-b0)^2);
if distance<=d h=0;
else h=1;
end;
s(i,j)=(p+q*h)*s(i,j);
end;
end;
s=uint8(real(ifft2(ifftshift(s))));
subplot(1,2,2),imshow(s+I);title('频域增强');
运行结果:
(2)空域锐化增强
matlab程序代码:
I=imread('C:\Users\SUN\Desktop\b.tif'); %读图像
I=im2double(I);
figure;
subplot(1,2,1),imshow(I);title('原图像')
[height width R]=size(I);
for i=2:height-1
for j=2:width-1
L(i,j)=4*I(i,j)-I(i-1,j)-I(i+1,j)-I(i,j-1)-I(i,j+1);
end
end
T=L;
G(i,j)=0.3*L(i,j)+0.7*I(i,j);
for i=1:height-1
for j=1:width-1
if (L(i,j)<0.12)
L(i,j)=1;
else L(i,j)=0;
end
end
end
[m,n]=size(T);
AL(1:m,1:n)=I(1:m,1:n)+T(1:m,1:n);
subplot(1,2,2),imshow(AL);title('空域增强');
运行结果:
三、人脸
检测
工程第三方检测合同工程防雷检测合同植筋拉拔检测方案传感器技术课后答案检测机构通用要求培训
matlab程序代码:
clear
image_source=imread('C:\Users\SUN\Desktop\h.jpg');
figure,imshow(image_source),title('源图像');
image_templet=imread('C:\Users\SUN\Desktop\i.jpg');
R=image_source(:,:,1);
G=image_source(:,:,2);
B=image_source(:,:,3);
R1=image_templet(:,:,1);
G1=image_templet(:,:,2);
B1=image_templet(:,:,3);
image3=zeros(size(image_source));
image4=image_source;
R3=image3(:,:,1);
G3=image3(:,:,2);
B3=image3(:,:,3);
r=30;%颜色半径30
[m,n]=size(R1);
sum=0;
for i=1:m
for j=1:n
sum=sum+double(R1(i,j));
end
end
average_R=sum/(m*n);%R模板平均灰度
sum=0;
for i=1:m
for j=1:n
sum=sum+double(G1(i,j));
end
end
average_G=sum/(m*n);%G模板平均灰度
sum=0;
for i=1:m
for j=1:n
sum=sum+double(B1(i,j));
end
end
average_B=sum/(m*n);%B模板平均灰度
[m,n]=size(R);
for i=1:m
for j=1:n
ifR(i,j)<=average_R+r&&R(i,j)>=average_R&&G(i,j)<=average_G+r&&G(i,j)>=average_G&&B(i,j)<=average_B+r&&B(i,j)>=average_B
R3(i,j)=R(i,j);
end
ifR(i,j)<=average_R&&R(i,j)>=average_R-r&&G(i,j)<=average_G&&G(i,j)>=average_G-r&&B(i,j)<=average_B&&B(i,j)>=average_B-r
R3(i,j)=R(i,j);
end
end
end
for i=1:m
for j=1:n
ifG(i,j)<=average_G+r&&G(i,j)>=average_G&&R(i,j)<=average_R+r&&R(i,j)>=average_R&& B(i,j)<=average_B+r&&B(i,j)>=average_B
G3(i,j)=G(i,j);
end
ifG(i,j)<=average_G&&G(i,j)>=average_G-r&&R(i,j)<=average_R&&R(i,j)>=average_R-r&& B(i,j)<=average_B&&B(i,j)>=average_B-r
G3(i,j)=G(i,j);
end
end
end
for i=1:m
for j=1:n
ifB(i,j)<=average_B+r&&B(i,j)>=average_B&&R(i,j)<=average_R+r&&R(i,j)>=average_R&&G(i,j)<=average_G+r&&G(i,j)>=average_G
B3(i,j)=B(i,j);
end
ifB(i,j)<=average_B&&B(i,j)>=average_B-r&&R(i,j)<=average_R&&R(i,j)>=average_R-r&&G(i,j)<=average_G&&G(i,j)>=average_G-r
B3(i,j)=B(i,j);
end
end
end
image3(:,:,1)=R3;
image3(:,:,2)=G3;
image3(:,:,3)=B3;
figure,imshow(image3),title('分通道检测图像');
image3gray=rgb2gray(image3);
figure,imshow(image3gray),title('分通道检测图像灰度化');
image3bw=im2bw(image3gray);
SE1=strel('square',1);%腐蚀
image3erode=imerode(image3bw,SE1,'same');%腐蚀
figure,imshow(image3erode),title('分通道检测图像二值化腐蚀');
SE2=strel('square',3);
image3dilate=imdilate(image3erode,SE2,'same');%膨胀
figure,imshow(image3dilate),title('分通道检测图像二值化膨胀');%膨胀
[L,num]=bwlabel(image3dilate,4);%区域标记
temp1=zeros(1,num);
if (num>0)
for k=1:num
for i=1:m
for j=1:n
if(L(i,j)==k)
temp1(1,k)=temp1(1,k)+1;%面积
end
end
end
end
for k=1:num
if(temp1(1,k)<600)%如果不用腐蚀膨胀则改小此值(600 100)
temp1(1,k)=0;
for i=1:m
for j=1:n
if(L(i,j)==k)
L(i,j)=0;
end
end
end
end
end
end
figure,imshow(L);title('去掉小面积区域');
SE2=strel('disk',2);
image3dilate=imdilate(L,SE2,'same');%膨胀
figure,imshow(image3dilate);title('膨胀面积区域');
L=imfill(image3dilate,'holes');
figure,imshow(L);title('填充区域');
for i=1:m
for j=1:n
if(L(i,j)==0)
image4(i,j,:)=0;
end
end
end
figure,imshow(image4);title('分割区域');
hold on;
figure,imshow(image_source);title('最终图像');
[l,num]=bwlabel(L,8);
status=regionprops(l,'BoundingBox');
for i=1:num
rectangle('Position',status(i).BoundingBox,'edgecolor','r');
end