图像分割技术

图像分割技术

图像分割就是把图像分成若干个特定的、具有独特性质的区域并提出感兴趣目标的技术和过程。它是由图像处理到图像分析的关键步骤。现有的图像分割方法主要分以下几类:基于阈值的分割方法、基于区域的分割方法、基于边缘的分割方法以及基于特定理论的分割方法等。1998年以来,研究人员不断改进原有的图像分割方法并把其它学科的一些新理论和新方法用于图像分割,提出了不少新的分割方法。图像分割后提取出的目标可以用于图像语义识别,图像搜索等等领域。

一、建立GUI界面

1.在主界面输入命令:guide 


2.选择blank

3.接下来添加菜单按钮
选择:Tools-》menu editor->the new menu button

4.修改按钮的名称和属性,得到如下界面

5.为GUI主界面按钮添加处理逻辑(函数)
在menu editor界面点击控件右下角view,对函数功能进行编辑


6.最终结果
二、程序实现
打开图片
% 打开图片
[fn,pn]=uigetfile('*.jpg;*.png;.bmp','选择一张图片');
I=imread([pn fn]);
imwrite(I,'im.png');
subplot(1,1,1);  %必须有逗号,否则图片不在GUI内部
imshow(I);
title('原始图像');


--(1)|边缘检测算法demo1

%  一、边缘检测算法 demo1

clc;
I=imread('im.png');
isuo=im2bw(I);

%sobert、robert、prewitt、log、canny算子检测图像边缘
esobel=edge(isuo,'sobel',0.04);     % 0.04为阈值
erob=edge(isuo,'roberts',0.04);
eprew=edge(isuo,'prewitt',0.04);
elog=edge(isuo,'log',0.004,2);      %2为高斯滤波器的标准偏差
ecanny=edge(isuo,'canny',0.04);

subplot(3,3,1);
imshow(I);title('原始图像');
subplot(3,3,2);
imshow(isuo);title('灰度图像');
subplot(3,3,4);
imshow(esobel);title('sobel算子提取');
subplot(3,3,5);
imshow(erob);title('roberts算子提取');
subplot(3,3,6);
imshow(eprew);title('prewitt算子提取');
subplot(3,3,7);
imshow(elog);title('LOG算子提取-标准偏差=2');
subplot(3,3,8);
imshow(ecanny);title('canny算子提取');


--(2)|阈值分割法

1. 直方图双峰法demo2

% --------------------------------------------------------------------
function histogram_Callback(hObject, eventdata, handles)
% hObject    handle to histogram (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%  二、	阈值分割法  
%  1.直方图双峰法 demo02

clear all;clc;
i = imread('im.png');
subplot(2,2,1);
imshow(i);
title('原始图像');

i2 = im2bw(i,80/255);
subplot(2,2,2);
imshow(i2);
title('双峰法80');

i3 = im2bw(i,75/255);
subplot(2,2,3);
imshow(i3);
title('双峰法75');


i4 = im2bw(i,200/255);
subplot(2,2,4);
imshow(i4);
title('双峰法200');

ii = rgb2gray(i);
subplot(2,2,4);
imhist(ii);
title('灰度直方图');


2.    最大类间方差法

%  二、阈值分割法
%  2.最大类间方差法 demo03

clc;
I=imread('im.png');  %读取图像
subplot(121),
imshow(I);
title('原始图像')
level=graythresh(I);
BW=im2bw(I,level);  %最大类间方差法分割图像
subplot(122),
imshow(BW)
title('最大类间方差法分割图像')
disp(strcat('graythresh 计算灰度阈值:',num2str(uint8(level*255))))、



3.    迭代法

%  二、阈值分割法
%  3.迭代法 demo04

clc;
I=imread('im.png');     	%读取图像
ZMax=max(max(I));
ZMin=min(min(I));
TK=(ZMax+ZMin)/2;
bCal=1;
iSize=size(I);
while(bCal)
iForeground=0;
iBackground=0;
ForegroundSum=0;
BackgroundSum=0;
for i=1:iSize(1)
for j=1:iSize(2)
tmp=I(i,j);
if(tmp>=TK)
iForeground=iForeground+1;
ForegroundSum=ForegroundSum+double(tmp);
else
iBackground=iBackground+1;
BackgroundSum=BackgroundSum+double(tmp);
end
end
end
ZO=ForegroundSum/iForeground;
ZB=BackgroundSum/iBackground;
TKTmp=uint8((ZO+ZB)/2);
if(TKTmp==TK)
bCal=0;
else
TK=TKTmp;
end
end
disp(strcat('迭代后的阈值',num2str(TK)));
newI=im2bw(I,double(TK)/255);       
subplot(121),
imshow(I); 
title('原始图像')
subplot(122),
imshow(newI);
title('迭代处理后的图像 ')


--(3)|区域分割

1. 区域生长法

%  三、区域分割法
%  1.区域生长法demo05
clc;
A0=imread('im.png');  
seed=[100,220];          		%选择起始位置
thresh=16;            		%相似性选择阈值
A=rgb2gray(A0);  
A=imadjust(A,[min(min(double(A)))/255,max(max(double(A)))/255],[]);
A=double(A); 
B=A;
 [r,c]=size(B);       			%图像尺寸 r为行数,c为列数
n=r*c;            			%计算图像所包含点的个数
pixel_seed=A(seed(1),seed(2)); 	%原图起始点灰度值
q=[seed(1) seed(2)];    		%q用来装载起始位置
top=1;          				%循环判断flag
M=zeros(r,c);           		%建立一个与原图形同等大小的矩阵
M(seed(1),seed(2))=1;  
count=1;%计数器
while top~=0          		%循环结束条件
r1=q(1,1);  
c1=q(1,2);  
p=A(r1,c1);  
dge=0;
for i=-1:1            
for j=-1:1
if r1+i<=r && r1+i>0 && c1+j<=c && c1+j>0 
if abs(A(r1+i,c1+j)-p)<=thresh && M(r1+i,c1+j)~=1
top=top+1; 
q(top,:)=[r1+i c1+j]; 
M(r1+i,c1+j)=1;
count=count+1;
B(r1+i,c1+j)=1;      				%满足判定条件将B中相对应的点赋为1
end
if M(r1+i,c1+j)==0; 
dge=1;        					%将dge赋为1
end
else
dge=1;         					%点在图像外将dge赋为1
end
end
end 
if dge~=1
B(r1,c1)=A(seed(1),seed(2)); 		%将原图像起始位置灰度值赋予B
end
if count>=n          
top=1;
end
q=q(2:top,:);
top=top-1;
end
subplot(1,2,1),
imshow(A,[]);
title('原图像')
subplot(1,2,2),
imshow(B,[]); 
title('生长法分割图像 ')



2.区域分裂合并 

%  三、区域分割法
%  2.区域分裂合并 demo06

clc;
Image=imread('im.png');   	%读取图像
Image1=im2bw(Image);
S=qtdecomp(Image1,0.25);		%其中0.25为每个方块所需要达到的最小差值
Image2=full(S); 
subplot(1,2,1);
imshow(Image);         		%显示前后两张图片
title('原图像')
subplot(1,2,2);
imshow(Image2);
title('处理后的图像')


--(4)|分水岭分割

%  四、分水岭分割 demo07

clear all;clc;
filename=('im.png'); 	%读入图像
f=imread(filename);
Info=imfinfo(filename);
if Info.BitDepth>8
   f=rgb2gray(f);
end
subplot(2,2,1),
mesh(double(f));         	%显示图像,类似集水盆地
b=im2bw(f,graythresh(f));  	%二值化,注意应保证集水盆地的值较低(为0),否则就要对b取反
d=bwdist(b);        	%求零值到最近非零值的距离,即集水盆地到分水岭的距离    
l=watershed(-d);     	%MATLAB自带分水岭算法,l中的零值即为风水岭
w=l==0;          	%取出边缘
g=b&~w;          	%用w作为mask从二值图像中取值
subplot(2,2,2),
imshow(f);
subplot(2,2,3),
imshow(g);



--(5)K-Means聚类分割

%  五、K-Means聚类分割 demo08

clear all; clc;
I = imread('im.png');
[m, n, p] = size(I);

[C, label, J] = kmeans(I, 2);
I_seg1 = reshape(C(label, :), m, n, p);
[C, label, J] = kmeans(I, 3);
I_seg2 = reshape(C(label, :), m, n, p);
[C, label, J] = kmeans(I, 5);
I_seg3 = reshape(C(label, :), m, n, p);

subplot(2, 2, 1), imshow(I, []), title('原图')
subplot(2, 2, 2), imshow(uint8(I_seg1), []), title('聚类图-k=2')
subplot(2, 2, 3), imshow(uint8(I_seg2), []), title('聚类图-k=3')
subplot(2, 2, 4), imshow(uint8(I_seg3), []), title('聚类图-k=5')
figure
plot(1:length(J), J), xlabel('#iterations')



源码链接: 图像分割

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值