资源简介
这是用基于区域的种子区域生长方法实现的彩色图像分割,分割效果相当好,最重要的是实现了彩色图像的分割,欢迎大家分享。
代码片段和文件信息
%using function
%AA=newrag(watnumregionXsizeYsize);生成邻接关系矩阵AA
%[XsizeYsizewat]=remove(XsizeYsizewat);去掉wat中的边界值
%BB=updatebb(TTBBnumregion);用于更新BB矩阵的区域邻接关系
resorce=imread(‘1000.jpg‘);
hsvimage=rgb2hsv(resorce);
[XsizeYsizeZsize]=size(resorce);
figureimshow(resorce)title(‘原图像‘);
fr=resorce(::1);
fg=resorce(::2);
fb=resorce(::3);
[x1map1]=rgb2ind(resorce8‘dither‘);
figureimshow(x1map1);title(‘加抖动‘);
imwrite(x1map1‘dith.jpg‘‘jpg‘);
map2=rgb2gray(map1);
figureimshow(x1map2)title(‘灰度图‘);
imwrite(x1map2‘gray.jpg‘‘jpg‘);
I=medfilt2(x1);
%se=strel(‘disk‘1);
%I=imerode(x1se);
figureimshow(Imap2)title(‘中值滤波后‘);
imwrite(Imap2‘fushi.jpg‘‘jpg‘);
%gs=fspecial(‘gaussian‘);
%I=imfilter(x1gs);
%I=imfilter(Igs);
hy = fspecial(‘sobel‘);
hx = hy‘;
Iy = imfilter(double(I) hy ‘replicate‘);
Ix = imfilter(double(I) hx ‘replicate‘);
gradmag = sqrt(Ix.^2 + Iy.^2);
wat=watershed(gradmag);
%wat=watershed(map2);
figureimshow(wat)title(‘水线‘);
imwrite(wat‘watershedline.bmp‘‘bmp‘);
w=imread(‘watershedline.bmp‘);
seg=imcomplement(w);
resultfr=imadd(segfr);
resultfg=imadd(segfg);
resultfb=imadd(segfb);
result=cat(3resultfrresultfgresultfb);
figureimshow(result)title(‘水线分割结果‘);
imwrite(result‘watershedConbine.jpg‘‘jpg‘);
%水线分割完毕
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%computing RAG 2005-5
%AA=newrag(watnumregionXsizeYsize);
%1代表邻接,0代表不邻接
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
numregion = double(max(wat(:)));
AA=newrag(watnumregionXsizeYsize);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%将原图从RGB空间转换到HSV空间
hsvimage=rgb2hsv(resorce);
H=hsvimage(::1);
S=hsvimage(::2);
V=hsvimage(::3);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%计算水线分割后每个区域中象素的Hue值的和并存储在数组HH中数组tmp中存储的是每个区域包含的象素个数.
%计算水线分割后每个区域中象素的Saturation值的和并存储在数组SS中
%数组Hmean中存储的是每个区域的Hue均值数组Smean中存储的是每个区域的Saturation均值
HH = zeros(1numregion);
SS = zeros(1numregion);
tmp = zeros(1numregion);
Hmean = zeros(1numregion);
Smean = zeros(1numregion);
for i=1:Xsize
for j=1:Ysize
k = wat(ij);
if (k ~= 0)
if H(ij)==0
HH(k)= HH(k)+V(ij);
else
HH(k)= HH(k)+H(ij);
end;
if S(ij)==0
SS(k)= SS(k)+V(ij);
else
SS(k)= SS(k)+S(ij);
end;
tmp(k) = tmp(k) + 1;
end;
end;
end;
sumtmp=0;
for i=1:numregion
Hmean(i)=HH(i)/tmp(i);
Smean(i)=SS(i)/tmp(i);
sumtmp=sumtmp+tmp(i);
end;
Hmeanback=Hmean;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%矩阵BB存储水线分割后每个区域的邻接矩阵
%其中BB(i1)为每个区域的邻接区域个数
%BB(inumregion+2)
- 上一篇:迫零均衡MATLAB仿真程序
- 下一篇:matlab 正六边形随机撒点
评论
共有 条评论