a b/combinedDeepLearningActiveContour/functions/regiongrowing.m
1
function J=regiongrowing(I,x,y,reg_maxdist)
2
% This function performs "region growing" in an image from a specified
3
% seedpoint (x,y)
4
%
5
% J = regiongrowing(I,x,y,t) 
6
% 
7
% I : input image 
8
% J : logical output image of region
9
% x,y : the position of the seedpoint (if not given uses function getpts)
10
% t : maximum intensity distance (defaults to 0.2)
11
%
12
% The region is iteratively grown by comparing all unallocated neighbouring pixels to the region. 
13
% The difference between a pixel's intensity value and the region's mean, 
14
% is used as a measure of similarity. The pixel with the smallest difference 
15
% measured this way is allocated to the respective region. 
16
% This process stops when the intensity difference between region mean and
17
% new pixel become larger than a certain treshold (t)
18
%
19
% Example:
20
%
21
% I = im2double(imread('medtest.png'));
22
% x=198; y=359;
23
% J = regiongrowing(I,x,y,0.2); 
24
% figure, imshow(I+J);
25
%
26
% Author: D. Kroon, University of Twente
27
28
if(exist('reg_maxdist','var')==0), reg_maxdist=0.2; end
29
if(exist('y','var')==0), figure, imshow(I,[]); [y,x]=getpts; y=round(y(1)); x=round(x(1)); end
30
31
J = zeros(size(I)); % Output 
32
Isizes = size(I); % Dimensions of input image
33
34
reg_mean = I(x,y); % The mean of the segmented region
35
reg_size = 1; % Number of pixels in region
36
37
% Free memory to store neighbours of the (segmented) region
38
neg_free = 10000; neg_pos=0;
39
neg_list = zeros(neg_free,3); 
40
41
pixdist=0; % Distance of the region newest pixel to the regio mean
42
43
% Neighbor locations (footprint)
44
neigb=[-1 0; 1 0; 0 -1;0 1];
45
46
% Start regiogrowing until distance between regio and posible new pixels become
47
% higher than a certain treshold
48
while(pixdist<reg_maxdist&&reg_size<numel(I))
49
50
    % Add new neighbors pixels
51
    for j=1:4,
52
        % Calculate the neighbour coordinate
53
        xn = x +neigb(j,1); yn = y +neigb(j,2);
54
        
55
        % Check if neighbour is inside or outside the image
56
        ins=(xn>=1)&&(yn>=1)&&(xn<=Isizes(1))&&(yn<=Isizes(2));
57
        
58
        % Add neighbor if inside and not already part of the segmented area
59
        if(ins&&(J(xn,yn)==0)) 
60
                neg_pos = neg_pos+1;
61
                neg_list(neg_pos,:) = [xn yn I(xn,yn)]; J(xn,yn)=1;
62
        end
63
    end
64
65
    % Add a new block of free memory
66
    if(neg_pos+10>neg_free), neg_free=neg_free+10000; neg_list((neg_pos+1):neg_free,:)=0; end
67
    
68
    % Add pixel with intensity nearest to the mean of the region, to the region
69
    dist = abs(neg_list(1:neg_pos,3)-reg_mean);
70
    [pixdist, index] = min(dist);
71
    J(x,y)=2; reg_size=reg_size+1;
72
    
73
    % Calculate the new mean of the region
74
    reg_mean= (reg_mean*reg_size + neg_list(index,3))/(reg_size+1);
75
    
76
    % Save the x and y coordinates of the pixel (for the neighbour add proccess)
77
    x = neg_list(index,1); y = neg_list(index,2);
78
    
79
    % Remove the pixel from the neighbour (check) list
80
    neg_list(index,:)=neg_list(neg_pos,:); neg_pos=neg_pos-1;
81
end
82
83
% Return the segmented area as logical matrix
84
J=J>1;
85
86
87
88