Hello Mat

 找回密码
 立即注册
查看: 6593|回复: 0

区域生长算法Region Growing

[复制链接]

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
发表于 2017-3-30 21:47:10 | 显示全部楼层 |阅读模式

  1. % Segment based on area, Region Growing;  
  2. clear  all; close all; clc  
  3. [fileName,pathName] = uigetfile('*.*','Please select an image');%文件筐,选择文件  
  4. if(fileName)  
  5.     fileName = strcat(pathName,fileName);  
  6.     fileName = lower(fileName);%一致的小写字母形式  
  7. else   
  8.     J = 0;%记录区域生长所分割得到的区域  
  9.     msgbox('Please select an image');  
  10.     return; %退出程序  
  11. end  
  12.   
  13. I = imread(fileName);  
  14. if( ~( size(I,3)-3 ))  
  15.     I = rgb2gray(I);%转化为单通道灰度图  
  16. end  
  17. I = im2double(I); %图像灰度值归一化到[0,1]之间  
  18. Ireshape = imresize(I,[600,800]);  
  19. I = Ireshape(51:475,200:699);  
  20. gausFilter = fspecial('gaussian',[5 5],0.5);  
  21. I = imfilter(I,gausFilter,'replicate');  
  22.   
  23. %种子点的交互式选择  
  24. if( exist('x','var') == 0 && exist('y','var') == 0)  
  25.     subplot(2,2,1),imshow(I,[]);  
  26.     hold on;  
  27.     [y,x] = getpts;%鼠标取点  回车确定  
  28.     x = round(x(1));%选择种子点  
  29.     y = round(y(1));  
  30. end  
  31.   
  32. if( nargin == 0)  
  33.     reg_maxdist = 0.1;  
  34.     %nargin是matlab代码编写中常用的一个技巧,主要用于计算当前主函数的输入参数个  
  35.     %数,一般可以根据nargin的返回值来确定主函数输入参数的缺省值。在实现中,如果  
  36.     %用户输入的参数个数为零,那么默认为0.2  
  37. end  
  38. J = zeros(size(I)); % 主函数的返回值,记录区域生长所得到的区域  
  39. Isizes = size(I);  
  40. reg_mean = I(x,y);%表示分割好的区域内的平均值,初始化为种子点的灰度值  
  41. reg_size = 1;%分割的到的区域,初始化只有种子点一个  
  42. neg_free = 10000; %动态分配内存的时候每次申请的连续空间大小  
  43. neg_list = zeros(neg_free,3);  
  44. %定义邻域列表,并且预先分配用于储存待分析的像素点的坐标值和灰度值的空间,加速  
  45. %如果图像比较大,需要结合neg_free来实现matlab内存的动态分配  
  46. neg_pos = 0;%用于记录neg_list中的待分析的像素点的个数  
  47. pixdist = 0;  
  48. %记录最新像素点增加到分割区域后的距离测度  
  49. %下一次待分析的四个邻域像素点和当前种子点的距离  
  50. %如果当前坐标为(x,y)那么通过neigb我们可以得到其四个邻域像素的位置  
  51. neigb = [ -1 0;  
  52.           1  0;  
  53.           0 -1;  
  54.           0  1];  
  55. %开始进行区域生长,当所有待分析的邻域像素点和已经分割好的区域像素点的灰度值距离  
  56. %大于reg_maxdis,区域生长结束  
  57.    
  58. while (pixdist < 0.06 && reg_size < numel(I))  
  59.      %增加新的邻域像素到neg_list中  
  60.      for j=1:4  
  61.          xn = x + neigb(j,1);  
  62.          yn = y + neigb(j,2);  
  63.          %检查邻域像素是否超过了图像的边界  
  64.          ins = (xn>=1)&&(yn>=1)&&(xn<=Isizes(1))&&(yn<=Isizes(1));  
  65.          %如果邻域像素在图像内部,并且尚未分割好;那么将它添加到邻域列表中  
  66.          if( ins && J(xn,yn)==0)  
  67.              neg_pos = neg_pos+1;  
  68.              neg_list(neg_pos,:) =[ xn, yn, I(xn,yn)];%存储对应点的灰度值  
  69.              J(xn,yn) = 1;%标注该邻域像素点已经被访问过 并不意味着,他在分割区域内  
  70.          end  
  71.      end  
  72.     %如果分配的内存空问不够,申请新的内存空间  
  73.     if (neg_pos+10>neg_free)  
  74.         neg_free = neg_free + 100000;  
  75.         neg_list((neg_pos +1):neg_free,:) = 0;  
  76.     end  
  77.     %从所有待分析的像素点中选择一个像素点,该点的灰度值和已经分割好区域灰度均值的  
  78.     %差的绝对值时所待分析像素中最小的  
  79.     dist = abs(neg_list(1:neg_pos,3)-reg_mean);  
  80.     [pixdist,index] = min(dist);  
  81.     %计算区域的新的均值  
  82.     reg_mean = (reg_mean * reg_size +neg_list(index,3))/(reg_size + 1);  
  83.     reg_size = reg_size + 1;  
  84.     %将旧的种子点标记为已经分割好的区域像素点  
  85.     J(x,y)=2;%标志该像素点已经是分割好的像素点  
  86.     x = neg_list(index,1);  
  87.     y = neg_list(index,2);  
  88. %     pause(0.0005);%动态绘制  
  89. %     if(J(x,y)==2)  
  90. %     plot(x,y,'r.');  
  91. %     end  
  92.     %将新的种子点从待分析的邻域像素列表中移除  
  93.     neg_list(index,:) = neg_list(neg_pos,:);  
  94.     neg_pos = neg_pos -1;  
  95. end  
  96.    
  97. J = (J==2);%我们之前将分割好的像素点标记为2  
  98. hold off;  
  99. subplot(2,2,2),imshow(J);  
  100. J = bwmorph(J,'dilate');%补充空洞  
  101. subplot(2,2,3),imshow(J);  
  102. subplot(2,2,4),imshow(I+J);  
复制代码


参考链接:http://blog.csdn.net/shenziheng1/article/details/50878911
算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-4-20 14:48 , Processed in 0.211965 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表