大工至善|大学至真分享 http://blog.sciencenet.cn/u/lcj2212916

博文

[转载]【源码】区域生长仿真——用强度平均法从种子点生长出一个区域进行分割

已有 1555 次阅读 2019-1-23 23:15 |系统分类:科研笔记|文章来源:转载


本代码是从一个单一种子点进行“区域生长”的简单但有效的例子。

Simple but effective example of "Region Growing" from a single seed point.


该区域通过将所有未分配的相邻像素与该区域进行比较来迭代生长。

The region is iteratively grown by comparing all unallocated neighbouring pixels to the region.


像素强度值和区域平均值之间的差异被用作相似性的度量

The difference between a pixel's intensity value and the region's mean, is used as a measure of similarity. 


以这种方式测量的差异最小的像素被分配到该区域。

The pixel with the smallest difference measured this way is allocated to the region.


当区域平均值和新像素之间的强度差大于某个固定值时,此过程停止。

This process stops when the intensity difference between region mean and new pixel becomes larger than a certain treshold.


function J=regiongrowing(I,x,y,reg_maxdist)

% This function performs "region growing" in an image from a specified

% seedpoint (x,y)

%

% J = regiongrowing(I,x,y,t)

%

% I : input image

% J : logical output image of region

% x,y : the position of the seedpoint (if not given uses function getpts)

% t : maximum intensity distance (defaults to 0.2)

%

% The region is iteratively grown by comparing all unallocated neighbouring pixels to the region.

% The difference between a pixel's intensity value and the region's mean,

% is used as a measure of similarity. The pixel with the smallest difference

% measured this way is allocated to the respective region.

% This process stops when the intensity difference between region mean and

% new pixel become larger than a certain treshold (t)

%

% Example:

%

% I = im2double(imread('medtest.png'));

% x=198; y=359;

% J = regiongrowing(I,x,y,0.2);

% figure, imshow(I+J);

%

% Author: D. Kroon, University of Twente


if(exist('reg_maxdist','var')==0), reg_maxdist=0.2; end

if(exist('y','var')==0), figure, imshow(I,[]); [y,x]=getpts; y=round(y(1)); x=round(x(1)); end


J = zeros(size(I)); % Output

Isizes = size(I); % Dimensions of input image


reg_mean = I(x,y); % The mean of the segmented region

reg_size = 1; % Number of pixels in region


% Free memory to store neighbours of the (segmented) region

neg_free = 10000; neg_pos=0;

neg_list = zeros(neg_free,3);


pixdist=0; % Distance of the region newest pixel to the regio mean


% Neighbor locations (footprint)

neigb=[-1 0; 1 0; 0 -1;0 1];


% Start regiogrowing until distance between regio and posible new pixels become

% higher than a certain treshold

while(pixdist<reg_maxdist&&reg_size<numel(I))


   % Add new neighbors pixels

   for j=1:4,

       % Calculate the neighbour coordinate

       xn = x +neigb(j,1); yn = y +neigb(j,2);

       

       % Check if neighbour is inside or outside the image

       ins=(xn>=1)&&(yn>=1)&&(xn<=Isizes(1))&&(yn<=Isizes(2));

       

       % Add neighbor if inside and not already part of the segmented area

       if(ins&&(J(xn,yn)==0))

               neg_pos = neg_pos+1;

               neg_list(neg_pos,:) = [xn yn I(xn,yn)]; J(xn,yn)=1;

       end

   end


   % Add a new block of free memory

   if(neg_pos+10>neg_free), neg_free=neg_free+10000; neg_list((neg_pos+1):neg_free,:)=0; end

   

   % Add pixel with intensity nearest to the mean of the region, to the region

   dist = abs(neg_list(1:neg_pos,3)-reg_mean);

   [pixdist, index] = min(dist);

   J(x,y)=2; reg_size=reg_size+1;

   

   % Calculate the new mean of the region

   reg_mean= (reg_mean*reg_size + neg_list(index,3))/(reg_size+1);

   

   % Save the x and y coordinates of the pixel (for the neighbour add proccess)

   x = neg_list(index,1); y = neg_list(index,2);

   

   % Remove the pixel from the neighbour (check) list

   neg_list(index,:)=neg_list(neg_pos,:); neg_pos=neg_pos-1;

end


% Return the segmented area as logical matrix

J=J>1;




源码下载地址:

http://page2.dfpan.com/fs/5lac8jc2e2b142f9166/  


更多精彩文章请关注微信号:qrcode_for_gh_60b944f6c215_258.jpg




https://blog.sciencenet.cn/blog-69686-1158739.html

上一篇:[转载]【Tom M. Mitchell课件】机器学习——深度信念网络
下一篇:[转载]【新书推荐】【2016.06】一条未知之路:美国移动洲际弹道导弹战略与技术发展的隐秘历史
收藏 IP: 117.60.77.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-9-20 03:31

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部