图像反转:
代码:
I=imread('coins.png');
R=255-I;
subplot(1,2,1);
imshow(I);
title('原图');
subplot(1,2,2);
imshow(R);
title('反转后');
二值图像:
代码:
I=imread('coins.png')
YZmax=max(max(I))
YZmin=min(min(I))
initialYZ=(YZmax+YZmin)/2
flag=1
[m,n]=size(I)
while(flag)
maxg=0
ming=0
maxgsum=0
mingsum=0
for i=1:m
for j=1:n
tmp=I(i,j)
if(tmp>initialYZ)
maxg=maxg+1
maxgsum=maxgsum+double(tmp)
else
ming=ming+1
mingsum=mingsum+double(tmp)
end
end
end
l1=maxgsum/maxg
l2=mingsum/ming
YZtmp=uint8((l1+l2)/2)
if(YZtmp==initialYZ)
flag=0;
else
initialYZ=YZtmp
end
end
newI=im2bw(I,double(initialYZ)/255)
imshow(newI)
8连通域:
代码:
I=imread('coins.png');
I=I>=128;%二值图像
[m n]=size(I);
I=[zeros(m,1) I zeros(m,1)];
I=[zeros(1,n+2);I;zeros(1,n+2)];
top=1;
tail=1;
num=0;%连通域个数
nb=[-1 -1;-1 0;-1 1;0 1;1 1;1 0;1 -1;0 -1];%8个方向
for i=2:m+1
for j=2:n+1
if(I(i,j)==1)
num=num+1;
I(i,j)= mod((i+j),254)+1;%只是想每一块连通域显示不同的颜色
k=1;
p(top,1)=i;
p(top,2)=j;
p(top,3)=k;
top=top+1;
while top~=tail %站内没有东西就代表一个连通域查找结束
top=top-1;
x=p(top,1);
y=p(top,2);
d=p(top,3);
while d<=8%循环八个方向
subx= x+nb(d,1);
suby= y+nb(d,2);
if(I(subx,suby)==1)%如果这个点8个方向中有一个方向是连通的就把这个点压进栈
p(top,1)=x;
p(top,2)=y;
p(top,3)=d+1;
top=top+1;
I(subx,suby)=mod((i+j),254)+1;
x=subx;%然后利用新找到的连通点循环这个过程
y=suby;
d=0;
end
d=d+1;
end
end
end
end
end
disp(num);
imshow(I,[]);
图像加噪声:
代码:
I=imread('cameraman.tif');
J1=imnoise(I,'salt & pepper',0.1); %椒盐
J2=imnoise(I,'gaussian',0.10); %gauss
subplot(1,3,1)'
imshow(I);
title('原图');
subplot(1,3,2)'
imshow(J1);
title('椒盐噪声');
subplot(1,3,3)'
imshow(J2);
title('高斯噪声');
均值平滑:
代码:
I=imread('cameraman.tif');
J1=imnoise(I,'salt & pepper',0.1); %椒盐
[m,n]=size(J1);
J1=double(J1);
subJ1=zeros(m,n);
subJ1(1,1)=(J1(1,1)+J1(1,2)+J1(2,2)+J1(2,1))/4;%角的特殊情况
subJ1(1,n)=(J1(1,n)+J1(1,n-1)+J1(2,n-1)+J1(2,n))/4;
subJ1(m,1)=(J1(m,1)+J1(m,2)+J1(m-1,2)+J1(m-1,1))/4;
subJ1(m,n)=(J1(m,n)+J1(m,n-1)+J1(m-1,n-1)+J1(m-1,n))/4;
for i=2:n-1 %4条边的特殊情况
subJ1(1,i)=(J1(1,i-1)+J1(1,i)+J1(1,i+1)+J1(2,i-1)+J1(2,i)+J1(2,i+1))/6;
end
for i=2:n-1 %4条边的特殊情况
subJ1(m,i)=(J1(m,i-1)+J1(m,i)+J1(m,i+1)+J1(m-1,i-1)+J1(m-1,i)+J1(m-1,i+1))/6;
end
for i=2:m-1 %4条边的特殊情况
subJ1(i,1)=(J1(i-1,1)+J1(i,1)+J1(i+1,1)+J1(i-1,2)+J1(i,2)+J1(i+1,2))/6;
end
for i=2:m-1 %4条边的特殊情况
subJ1(i,n)=(J1(i-1,n)+J1(i,n)+J1(i+1,n)+J1(i-1,n-1)+J1(i,n-1)+J1(i+1,n-1))/6;
end
for i=2:m-1 %除了4个角和4条边的其他所有点的情况
for j=2:n-1
subJ1(i,j)=(J1(i-1,j-1)+J1(i-1,j)+J1(i-1,j+1)+J1(i,j-1)+J1(i,j)+J1(i,j+1)+J1(i+1,j-1)+J1(i+1,j)+J1(i+1,j+1))/9;
end
end
imshow(subJ1,[]);
title('均值平滑');
中值平滑:
代码:
I=imread('cameraman.tif');
J1=imnoise(I,'gaussian',0.10); %gauss
[m,n]=size(J1);
subJ1=zeros(m,n);
A=[J1(1,1) J1(1,2) J1(2,2) J1(2,1)];%角的特殊情况
subJ1(1,1)=median(A);
A=[J1(1,n) J1(1,n-1) J1(2,n-1) J1(2,n)];
subJ1(1,n)=median(A);
A=[J1(m,1) J1(m,2) J1(m-1,2) J1(m-1,1)];
subJ1(m,1)=median(A);
A=[J1(m,n) J1(m,n-1) J1(m-1,n-1) J1(m-1,n)];
subJ1(m,n)=median(A);
for i=2:n-1%边的特殊情况
A=[J1(1,i-1) J1(1,i) J1(1,i+1) J1(2,i-1) J1(2,i) J1(2,i+1)];
subJ1(1,i)=median(A);
end
for i=2:n-1%边的特殊情况
A=[J1(m,i-1) J1(m,i) J1(m,i+1) J1(m-1,i-1) J1(m-1,i) J1(m-1,i+1)];
subJ1(m,i)=median(A);
end
for i=2:m-1%边的特殊情况
A=[J1(i-1,1) J1(i,1) J1(i+1,1) J1(i-1,2) J1(i,2) J1(i+1,2)];
subJ1(i,1)=median(A);
end
for i=2:m-1%边的特殊情况
A=[J1(i-1,n) J1(i,n) J1(i+1,n) J1(i-1,n-1) J1(i,n-1) J1(i+1,n-1)];
subJ1(i,n)=median(A);
end
for i=2:m-1 %除了角和边的情况
for j=2:n-1
A=[J1(i-1,j-1) J1(i-1,j) J1(i-1,j+1) J1(i,j-1) J1(i,j) J1(i,j+1)J1(i+1,j-1) J1(i+1,j) J1(i+1,j+1)];
subJ1(i,j)=median(A);
end
end
imshow(subJ1,[]);
soble算子和laplace算子锐化
代码:
I=imread('cameraman.tif');
subplot(2,2,1),imshow(I);
title('原始图像');
H=fspecial('sobel'); %选择sobel算子
J=filter2(H,I1); %卷积运算
subplot(2,2,3),imshow(J);
title('sobel算子锐化图像');
h=[0 1 0;1 -4 1;0 1 0]; %拉普拉斯算子
J1=conv2(I,h,'same'); %卷积运算
subplot(2,2,4),imshow(J1);
title('拉普拉斯算子锐化图像');
直方图和直方图均衡化:
代码:
I=imread('cameraman.tif');
subplot(2,2,1);%画子图
imshow(I);
title('原图');
num=zeros(1,256);
newnum=zeros(1,256);
[m,n]=size(I);
for i=1:m
for j=1:n
num(I(i,j)+1)= 1+ num(I(i,j)+1);%统计各像素的总个数
end
end
x=0:255;
y=num/(m*n);%求出各像素点的百分比
subplot(2,2,2);
bar(x,y,0);
xlabel('灰度值');
ylabel('概率');
title('原图的直方图');
for i=2:256
y(i)=y(i)+y(i-1); %计算累计直方图分布
end
y=y*256; %找出直方图均衡化后原来的像素点所对应的新像素点
y=round(y);%四舍五入
for i=1:m
for j=1:n
I(i,j)= y(I(i,j)+1)-1;%更改原图成为直方图均衡化后的图
newnum(I(i,j)+1)= 1+ newnum(I(i,j)+1);%统计新图各像素点的个数
end
end
y2=newnum/(m*n);%求出各像素点的百分比
subplot(2,2,3);%画子图
imshow(I);
title('直方图均衡化后的效果');
subplot(2,2,4);
bar(x,y2,0);
xlabel('灰度值');
ylabel('概率');
title('直方图均衡化后的直方图');
理想的滤波器:
理想低通滤波器(ILPF): d=10
代码:
I=imread('cameraman.tif');
J=imnoise(I,'salt & pepper',0.1);
f=double(J);
subJ=fftshift(fft2(f));%空间域到频率域
[m,n]=size(subJ);
for i=1:m
for j=1:n
a=1;
if(sqrt((i-m/2)^2+(j-n/2)^2)>10)
a=0;
end
subJ(i,j)=subJ(i,j)*a;
end
end
resJ=uint8(real(ifft2(subJ)));%频率域到空间域
imshow(resJ,[]);
理想高通滤波器(IHPF) d=10
代码:
I=imread('cameraman.tif');
J=imnoise(I,'salt & pepper',0.1);
f=double(J);
subJ=fftshift(fft2(f));%空间域到频率域
[m,n]=size(subJ);
for i=1:m
for j=1:n
a=1;
if(sqrt((i-m/2)^2+(j-n/2)^2)<=10)
a=0;
end
subJ(i,j)=subJ(i,j)*a;
end
end
resJ=uint8(real(ifft2(subJ)));%频率域到空间域
imshow(resJ,[]);
高斯低通滤波器(GLPF):
代码:
I=imread('cameraman.tif');
J1=imnoise(I,'salt & pepper',0.1);
f=double(J1);
subJ=fftshift(fft2(f));%空间域到频率域
[m,n]=size(subJ);
for i=1:m
for j=1:n
a=exp(-((i-m/2)^2+(j-n/2)^2)/(2*100)); %d=10
subJ(i,j)=subJ(i,j)*a;
end
end
resJ=uint8(real(ifft2(subJ)));%频率域到空间域
imshow(resJ,[]);
高斯高通滤波器(GHPF):
代码:
I=imread('cameraman.tif');
J1=imnoise(I,'salt & pepper',0.1);
f=double(J1);
subJ=fftshift(fft2(f));%空间域到频率域
[m,n]=size(subJ);
for i=1:m
for j=1:n
a=1-exp(-((i-m/2)^2+(j-n/2)^2)/(2*100)); %d=10
subJ(i,j)=subJ(i,j)*a;
end
end
resJ=uint8(real(ifft2(subJ)));%频率域到空间域
imshow(resJ,[]);
提取红色区域:
I=imread('greens.jpg');
subplot(1,2,1);
imshow(I);
title('原始图像');
[m,n,l]=size(I);
b=double(I)./255;
r=0.1765;
a=[0.6863,0.1608,0.1922];
for i=1:m
for j=1:n
dis=(b(i,j,1)-a(1))^2+(b(i,j,2)-a(2))^2+(b(i,j,3)-a(3))^2;
if(dis>r^2)
I(i,j,1)=127;
I(i,j,2)=127;
I(i,j,3)=127;
end
end
end
subplot(1,2,2);
imshow(I,[]);
title('提取后');