MATLAB数字图像处理
【课程总结】
彩色图像
方块彩色图像显示
- MATLAB中调色板色彩强度[0,1],0代表最暗,1代表最亮。
常用颜色的RGB值:
颜色 R G B 颜色 R G B
黑 0 0 0 洋红 1 0 1
白 1 1 1 青蓝 0 1 1
红 1 0 0 天蓝 0.67 0 1
绿 0 1 0 橘黄 1 0.5 0
蓝 0 0 1 深红 0.5 0 0
黄 1 1 0 灰 0.5 0.5 0.5
- 例:在黑色背景上,绘出下面方块中的彩色。显示RGB图像及其HSI分量图像
clear all,close all,clc
% RGB=cat(3,R,G,B);
RGB=zeros(400,512,3)
% R
RGB(26:125,15:165,1)=1;
RGB(26:125,182:332,1)=1;
RGB(151:250,15:165,1)=1;
RGB(151:250,349:500,1)=1;
% G
RGB(26:125,182:332,2)=1;
RGB(151:250,182:332,2)=1;
RGB(276:375,182:332,2)=1;
RGB(151:250,349:500,2)=1;
% B
RGB(26:125,349:500,3)=1;
RGB(151:250,15:165,3)=1;
RGB(151:250,182:332,3)=1;
RGB(151:250,349:500,3)=1;
figure;
imagesc(RGB);
axis off;
figure(2)
subplot(221)
imagesc(RGB)
title('\fontsize{20}the original image')
axis off
subplot(222)
imagesc(RGB(:,:,1))
colormap(gray)
title('\fontsize{20}R component')
axis off
subplot(223)
imagesc(RGB(:,:,2))
colormap(gray)
title('\fontsize{20}G component')
axis off
subplot(224)
imagesc(RGB(:,:,3))
colormap(gray)
title('\fontsize{20}B component')
axis off
HSI=rgb2hsi(RGB);
H=HSI(:,:,1);
S=HSI(:,:,2);
I=HSI(:,:,3);
figure(3)
subplot(221)
imagesc(RGB)
title('\fontsize{20}the original image')
axis off
subplot(222)
imagesc(H)
colormap(gray)
title('\fontsize{20}Hue component')
axis off
subplot(223)
imagesc(S)
colormap(gray)
title('\fontsize{20}Saturation component')
axis off
subplot(224)
imagesc(I)
colormap(gray)
title('\fontsize{20}Intensity component')
axis off
彩色变换
RGB—>HIS HIS = rgb2hsi(RGB);
HIS—>RGB RGB = hsi2rgb(HSI);通常在对HIS三分量进行操作后重新合成,显示合成的图片,需要将其转化位RGB形式显示
三个分量分离,对某一个分量进行增强后显示R = RGB(:,:,1),G = RGB(:,:,2),B = RGB(:,:,3);
H = HSI(:,:,1),S = HSI(:,:,2),I = HSI(:,:,3);
分量合成 RGB=cat(3,R,G,B)
HIS=cat(3,H,S,I);显示时,需要先转换成RGB在显示
- 例:针对低对比度彩色图像,分别显示其色调、饱和度和亮度分量;使用图像增强方法改进亮度分量图像的对比度,将增强后的亮度分量与色调、饱和度分量结合在一起,得到对比度增强彩色图像。显示增强前后的彩色图像。
clear all
close all
clc
c=imread('fig3_4.jpg');
HSI=rgb2hsi(c);
H=HSI(:,:,1);
S=HSI(:,:,2);
I=HSI(:,:,3);
m=mean2(I);
E=0.9;
I1=1./(1+(m./(I+eps)).^E);%改变亮度分量
I1=mat2gray(I1);
g=cat(3,H,S,I1);
g=hsi2rgb(g);
figure;
subplot(211)
imagesc(c)
title('\fontsize{10}the original image')
axis off
subplot(212)
imagesc(g)
title('\fontsize{10}Enhanced image')
axis off
灰度变换、平滑、饱和度、边缘锐化
x=imread(‘x.jpg’);
x=rbg2gray(x); %转成灰度图像
k=medfilt2(x); %中值滤波,默认为3X3矩阵
figure, imshow(k);
medfilt2(A,[M,N]):使用 M X N 的模板读A矩阵做中值滤
空间域增强
灰度变化
1.线性变换
f=imread('Fig3.10(b).jpg');
f=im2double(f);
imshow(f);
figure;imhist(f);
a=min(f(:));
b=max(f(:));
g=1*(f-a)/(b-a);
figure;
imshow(g);
figure;imhist(g);
2.2. 分段线性变换
- 例:使用以下命令创建一幅暗图像
“c=imread(‘cameraman.tif’);[x,map]=gray2ind©;”使用分段线性灰度变换改进图像的对比度
clear all
close all
clc
c=imread('D:\Program Files\MATLAB\R2011b\toolbox\images\imdemos\cameraman.tif');
[x,map]=gray2ind(c);
r=im2double(x);
r1=0.1;r2=0.28;
s1=0.05;s2=0.9;
k1=s1/r1;k2=(s2-s1)/(r2-r1);k3=(1-s2)/(1-r2);
s=k1*r.*(r<=r1)+(k2*(r-r1)+s1).*(r>=r1 & r<r2)...
+(k3*(r-r2)+s2).*(r>=r2);
rr=0:0.01:1;
ss=k1*rr.*(rr<r1)+(k2*(rr-r1)+s1).*(rr>=r1 & rr<r2)...
+(k3*(rr-r2)+s2).*(rr>=r2);
subplot(221)
imhist(r)
title('\fontsize{10}输入图像的直方图')
subplot(222)
plot(rr,ss)
title('\fontsize{10}灰度变换曲线')
grid on
subplot(223)
imshow(x)
title('\fontsize{10}输入图像')
subplot(224)
imshow(s)
title('\fontsize{10}输出图像')
3.灰度反转变换
r=imread('H:\data\thry\chpt3\Fig3.04(a).jpg');
r=double(r);
mx=max(r(:));
s=mx-r;
subplot(121)
imshow(r,[])
title('\fontsize{40}Input image')
subplot(122)
imshow(s,[])
title('\fontsize{40}Output image')
直方图
imhist(f):显示f图像的直方图
- 例1:下面表格给出了一幅图像中[0,7]范围内的每个灰度级对应的像素数。绘制这些灰度级对应的直方图。接着执行直方图均衡化,绘制均衡化后的直方图。
0 1 2 3
3244 3899 4559 2573
4 5 6 7
1428 530 101 50
clear all
close all
clc
r=0:7;
L=length(r);
nr=[3244 3899 4559 2573 1428 530 101 50];
n=sum(nr);
pr=nr./n;
Tr=cumsum(pr);
s=r;
ps=zeros(1,L);
for r1=0:L-1
s1=round(Tr(r1+1)*(L-1));
ps(s1+1)=ps(s1+1)+pr(r1+1);
end
subplot(121)
stem(r,pr)
xlabel('r')
ylabel('p_r(r)')
title('Histogram before equalization')
subplot(122)
stem(s,ps)
xlabel('s')
ylabel('p_s(s)')
title('Histogram after equalization')
- 例2:下面图像包含[0,19]范围内的灰度级。试绘制直方图均衡化前后的图像及其直方图
clear all
close all
clc
f=[12 6 5 13 14 14 16 15
11 10 8 5 8 11 14 14
9 8 3 4 7 12 18 19
10 7 4 2 10 12 13 17
16 6 13 13 16 19 19 17
12 10 14 15 18 18 16 14
11 8 10 12 14 13 14 15
8 6 3 7 9 11 12 12]
r=0:7;
for k=r
g=(f==k);
nr(k+1)=sum(g(:));
end
n=sum(nr);
pr=nr/n
Tr=cumsum(pr)
s=round(Tr*7)
ps=zeros(1,8);
f_equal=zeros(size(f));
for k=r
ps(s(k+1)+1)=ps(s(k+1)+1)+pr(k+1);
f_equal(f==k)=s(k+1);
end
ps
f_equal
subplot(221)
imshow(f,[])
title('Image before equalization')
subplot(222)
stem(r,nr)
xlabel('r')
ylabel('p_r(r)')
title('Histogram before equalization')
subplot(223)
imshow(f_equal,[])
title('Image after equalization')
subplot(224)
stem(0:7,ps)
xlabel('s')
ylabel('p_s(s)')
title('Histogram after equalization')
- 例3:使用下面命令创建一幅暗图像
“c=imread(‘cameraman.tif’); [x,map]=gray2ind©; ”矩阵x是摄影师图像的一个暗版本。对该图像使用直方图均衡化处理,比较均衡化图像与原图像。
c=imread('cameraman.tif');
[x,map]=gray2ind(c);
subplot(221),imshow(x);
title('the original image');
subplot(223)
imhist(x);
title('histogram')
ylim('auto')
g=histeq(x,256);
subplot(222),imshow(g)
title('image after equalization of histogram')
subplot(224),imhist(g)
title('histogram after equalization')
空间域锐化滤波器
拉普拉斯算子:
w4=[0 1 0;1 -4 1;0 1 0]; 中心系数为-4的拉普拉斯算子 fspecial(‘laplacian’,0)
w8=[1 1 1;1 -8 1;1 1 1]; 中心系数为-8的拉普拉斯算子
f=im2double(f);
g1=imfilter(f,w4,‘replicate’);
g4=f-4*g1;imshow(g4,[])
1)利用Sobel算子锐化数字数字图像, 如:
i=imread(‘104_8.tif’);
h=[1,2,1;0,0,0;-1,-2,-1];%Sobel算子
j=filter2(h,i);
2)利用拉氏算子锐化数字数字图像, 如:
i=imread(‘104_8.tif’);
j=double(i);
h=[0,1,0;1,-4,0;0,1,0];%拉氏算子
k=conv2(j,h,‘same’);
m=j-k;
傅里叶变换
性质(主要:平移不变性)
变换
频率域图像增强
平滑(3种)
1 理想低通滤波器
2 巴特沃斯低通滤波器
3 高斯低通滤波器
- 例:打开cameraman.tif图像:使用如下滤波器对图像进行频域滤波:
(a) 理想低通滤波器,
(b) 巴特沃斯低通滤波器,
© 高斯低通滤波器。
人脸仍然能够被分辨的理想低通滤波器的最小半径是多少?
理想低通滤波器
clc;clear all;close all;
f=imread('D:\Program Files\MATLAB\R2011b\toolbox\images\imdemos\cameraman.tif');
subplot(221);
imshow(f,[]);
title('\fontsize{10}original image');
[M,N]=size(f);
F=fftshift(fft2(f));
u=1:M;
v=1:N;
[V,U]=meshgrid(v,u);
D=sqrt((U-floor(M/2)).^2+(V-floor(N/2)).^2);
%The coordinate on the left top corner is (1,1)£¬
% the center is (M/2,N/2)
H=zeros(size(f));
k=2;
for D0=[15 30 50]
H(D<D0)=1;
G=F.*H;
g=ifft2(ifftshift(G));
subplot(2,2,k)
imshow(real(g),[]);
title(['\fontsize{10}ideal low pass filter with radius<',num2str(D0)]);
k=k+1;
end
人脸仍然能够被分辨的理想低通滤波器的最小半径是50
巴特沃斯低通滤波器
clc;clear all;close all;
f=imread('D:\Program Files\MATLAB\R2011b\toolbox\images\imdemos\cameraman.tif');
subplot(231);
imshow(f,[]);
title('\fontsize{10}original image');
[M,N]=size(f);
F=fftshift(fft2(f));
u=1:M;
v=1:N;
[V,U]=meshgrid(v,u);
D=sqrt((U-floor(M/2)).^2+(V-floor(N/2)).^2);
H=zeros(size(f));
k=2;
n=2;
for D0=[5 15 30 80 230]
H=1./(1+(D./D0).^(2*n));
G=F.*H; %low pass filter on frequency domain
g=ifft2(ifftshift(G));
subplot(2,3,k)
k=k+1;
imshow(real(g),[]);
title({'\fontsize{10}Butterworth low pass';...
['\fontsize{10}filter with radius<',num2str(D0)]});
end
高斯低通滤波器
clc;clear all;close all;
f=imread('D:\Program Files\MATLAB\R2011b\toolbox\images\imdemos\cameraman.tif');
subplot(231);
imshow(f,[]);
title('\fontsize{10}original image');
[M,N]=size(f);
F=fftshift(fft2(f));
u=1:M;
v=1:N;
[V,U]=meshgrid(v,u);
D=sqrt((U-floor(M/2)).^2+(V-floor(N/2)).^2);
H=zeros(size(f));
k=2;
n=2;
for D0=[5 15 30 80 230]
H=exp(-D.^2./(2*D0.^2));
G=F.*H; %low pass filter on frequency domain
g=ifft2(ifftshift(G));
subplot(2,3,k)
k=k+1;
imshow(real(g),[]);
title({'\fontsize{10}Gaussian lowpass';...
['\fontsize{10}filter with radius<',num2str(D0)]});
end
锐化(4种)
图像复原
模型
模糊模型:大气模糊、运动模糊
大气模糊
f=imread('e:\chenpc\data\thry\chpt5\Fig5.25(a).jpg');
f=im2double(f);
subplot(221);
imshow(f,[])
title('\fontsize{30}original image')
F=fftshift(fft2(f));
[M,N]=size(F);
u=1:M;
v=1:N;
[V,U]=meshgrid(v,u);
m=2;
for k=[0.0025 0.001 0.00025] %severe turbulence
H=exp(-k*((U-floor(M/2)).^2+(V-floor(N/2)).^2).^(5/6));
G=F.*H;
g=real(ifft2(ifftshift(G)));
subplot(2,2,m);
imshow(g,[])
if k==0.0025
title('\fontsize{30}severe turbulence')
save h:\data\thry\chpt5\Fig5.25(b).mat g
elseif k==0.001
title('\fontsize{30}mild turbulence')
else
title('\fontsize{30}low turbulence')
end
m=m+1;
end
运动模糊
[M,N]=size(f);
u=1:M;
v=1:N;
[V,U]=meshgrid(v,u);
a=0.1*M;b=0.1*N;T1=1;T2=3;
t1=pi*((U-floor(M/2))*a/M);
t2=pi*((V-floor(N/2))*b/N);
H=(T1*sin(t2+eps).*exp(-j*t2)./(t2+eps))+(T2*sin(t1+eps).*exp(-j*t2).*exp(-j*(t1+t2))./(t1+eps));
F=fftshift(fft2(f));
G=F.*H;
g=real(ifft2(ifftshift(G)));
subplot(132)
imshow(g,[])A
复原方式
最小约束平方滤波
clear all
close all
clc
f=imread('e:\chenpc\data\thry\chpt5\Fig5.26(a).jpg');
f=im2double(f);
[M,N]=size(f);
u=1:M;
v=1:N;
[V,U]=meshgrid(v,u);
a=0.1*M;
b=0.1*N;
T=1;
tmp=pi.*((U-(floor(M/2)+1))*a/M+(V-(floor(N/2)+1))*b/N);
H=sin(tmp+eps).*exp(-j*tmp).*T./(tmp+eps);
F=fftshift(fft2(f));
G=F.*H;
g=real(ifft2(ifftshift(G))); %produce the image blurred by motion
m=1;
Sf=abs(fftshift(fft2(f))).^2; %power spectrum of signal
p=[0 1 0;1 -4 1;0 1 0]; %Laplacian operator
P=fftshift(fft2(p,M,N)); %frequency spectrum of Laplacian operator
k1=5;
for sig=[650 65 0.0065]
noise=imnoise(zeros(M,N),'gaussian',0,sig); %add Gaussian noise
gb=g+noise;
subplot(3,3,m)
imshow(gb,[])
title(['Gaussian noise(\sigma=',num2str(sig),')'])
G=fftshift(fft2(gb));
Sn=abs(fftshift(fft2(noise))).^2; %power spectrum of noise
k=Sn./Sf;
F=conj(H).*G./(abs(H).^2+k); %Wiener filter
fr=real(ifft2(ifftshift(F)));
subplot(3,3,m+1)
imshow(fr,[])
title('Wiener filter')
F=conj(H).*G./(abs(H).^2+k1.*abs(P).^2+eps);
%Constrained least square restoration
fr=real(ifft2(ifftshift(F)));
subplot(3,3,m+2)
imshow(fr,[])
title('constrained least square filter')
m=m+3;
end
维纳滤波复原
K=eps;
F=conj(H).*G./(abs(H).^2+K);
f=real(ifft2(ifftshift(F)));
subplot(133)
imshow(f,[])
title('\fontsize{10}Wiener filter result');
形态学
4个基本操作
1.膨胀:是在二值化数字数字图像中“加长”或“变粗”的操作,函数imdilate执行膨胀运算,如:
a=imread(‘104_7.tif’);%输入二值数字数字图像
b=[0 1 0;1 1 1;01 0];
c=imdilate(a,b);
2.腐蚀:函数imerode执行腐蚀,如:
a=imread(‘104_7.tif’);%输入二值数字数字图像
b=strel(‘disk’,1);
c=imerode(a,b);
3.开运算:先腐蚀后膨胀称为开运算,用imopen来实现,如:
a=imread(‘104_8.tif’);
b=strel(‘square’,2);
c=imopen(a,b);
4.闭运算:先膨胀后腐蚀称为闭运算,用imclose来实现,如:
a=imread(‘104_8.tif’);
b=strel(‘square’,2);
c=imclose(a,b);
重构
腐蚀
se=ones(15,1);
marker=imerode(f,se);
restore=imreconstruct(marker,f);
imshow(restore,[])
三种寻找mark的方式
相关
clear all;close all;clc
f=imread('Fig0903(a).tif');
f=im2double(f);
imshow(f,[])
title('Image including objects')
[mf,nf]=size(f);
mask=roipoly(f); %ÊÖ¶¯Ñ¡È¡ÂÖÀªÏß
title('image')
b=f.*mask;
[r,c,v]=find(b~=0);
r1=min(r);r2=max(r);
c1=min(c);c2=max(c);
h=b(r1:r2,c1:c2);% Object
[mh,nh]=size(h);
figure
M=mf+mh-1;
N=nf+nh-1;
f(M,N)=0; %pad zeros to avoid aliasing of adjacent period to
subplot(221)
imshow(f,[])
title('image')
subplot(222)
imshow(h,[])
title('Object')
h(M,N)=0; %pad zeros to avoid aliasing of adjacent period to
F=fft2(f);
H=fft2(h);
corr=real(ifft2(F.*conj(H)));
[r,c]=find(corr==max(corr(:)));
subplot(223)
imshow(f,[])
hold on
plot(c(1),r(1),'ws')
mask=zeros(size(f));
mask(r(1):r(1)+mh,c(1):c(1)+nh)=1;
object=(f & mask);
subplot(224)
imshow(object,[])
击中击不中
bwhitmiss(A,B1,B2) 先用结构元素B1对A进行腐蚀,然后再用结构元素B进行腐蚀
mask=roipoly(f); %手动选取轮廓线
g=mask.*f;
[r,c]=find(g);
g=g(min(r):max(r),min(c):max(c));
B1=g;
B2=~g;
marker=bwhitmiss(f,B1,B2);
restore=imreconstruct(marker,f);
imshow(restore,[])
填孔洞
g=imfill(f,‘holes’);
%fill the holes surrounded by stroks of characters
subplot(335)
imshow(g,[])
%display the image after filling holes
title(’(e)Holes filled’)
手动轮廓线
clear all;close all;clc
f=imread('F:\picture process\t3.jpg');
f=im2double(f);
f=im2bw(f,0.5);
figure
imshow(f,[])
mask=roipoly(f); %ÊÖ¶¯Ñ¡È¡ÂÖÀªÏß
g=mask.*f;
[r,c,v]=find(g~=0);
h=g(min(r):max(r),min(c):max(c));
[mh,nh]=size(h);
figure
imshow(h,[])
B1=[0 0 0 0;
0 1 1 0;
0 1 1 0;
0 0 0 0];
B2=~B1;
marker=bwhitmiss(f,B1,B2);
restore=imreconstruct(marker,f);
figure
imshow(restore,[])