|
a |
|
b/Image Segmentation/region growing/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 |
%Check to see if they provided a max distance away from the mean to stop |
|
|
29 |
%algorithm |
|
|
30 |
if(exist('reg_maxdist','var')==0), reg_maxdist=0.2; end |
|
|
31 |
if(exist('y','var')==0), figure, imshow(I,[]); [y,x]=getpts; y=round(y(1)); x=round(x(1)); end |
|
|
32 |
|
|
|
33 |
J = zeros(size(I)); % Output |
|
|
34 |
Isizes = size(I); % Dimensions of input image |
|
|
35 |
|
|
|
36 |
reg_mean = I(x,y); % The mean of the segmented region |
|
|
37 |
reg_size = 1; % Number of pixels in region |
|
|
38 |
|
|
|
39 |
% Free memory to store neighbours of the (segmented) region |
|
|
40 |
neg_free = 10000; neg_pos=0; |
|
|
41 |
neg_list = zeros(neg_free,3); |
|
|
42 |
|
|
|
43 |
pixdist=0; % Distance of the region newest pixel to the region mean |
|
|
44 |
|
|
|
45 |
% Neighbor locations (footprint) |
|
|
46 |
neigb=[-1 0; 1 0; 0 -1;0 1]; |
|
|
47 |
|
|
|
48 |
% Start region growing until distance between region and posible new pixels become |
|
|
49 |
% higher than a certain treshold |
|
|
50 |
while(pixdist<reg_maxdist&®_size<numel(I)) |
|
|
51 |
|
|
|
52 |
% Add new neighbors pixels |
|
|
53 |
for j=1:4, |
|
|
54 |
% Calculate the neighbour coordinate |
|
|
55 |
xn = x +neigb(j,1); yn = y +neigb(j,2); |
|
|
56 |
|
|
|
57 |
% Check if neighbour is inside or outside the image |
|
|
58 |
ins=(xn>=1)&&(yn>=1)&&(xn<=Isizes(1))&&(yn<=Isizes(2)); |
|
|
59 |
|
|
|
60 |
% Add neighbor if inside and not already part of the segmented area |
|
|
61 |
if(ins&&(J(xn,yn)==0)) |
|
|
62 |
neg_pos = neg_pos+1; |
|
|
63 |
neg_list(neg_pos,:) = [xn yn I(xn,yn)]; J(xn,yn)=1; |
|
|
64 |
end |
|
|
65 |
end |
|
|
66 |
|
|
|
67 |
% Add a new block of free memory |
|
|
68 |
if(neg_pos+10>neg_free), neg_free=neg_free+10000; neg_list((neg_pos+1):neg_free,:)=0; end |
|
|
69 |
|
|
|
70 |
% Add pixel with intensity nearest to the mean of the region, to the region |
|
|
71 |
dist = abs(neg_list(1:neg_pos,3)-reg_mean); |
|
|
72 |
[pixdist, index] = min(dist); |
|
|
73 |
J(x,y)=2; reg_size=reg_size+1; |
|
|
74 |
|
|
|
75 |
% Calculate the new mean of the region |
|
|
76 |
reg_mean= (reg_mean*reg_size + neg_list(index,3))/(reg_size+1); |
|
|
77 |
|
|
|
78 |
% Save the x and y coordinates of the pixel (for the neighbour add proccess) |
|
|
79 |
x = neg_list(index,1); y = neg_list(index,2); |
|
|
80 |
|
|
|
81 |
% Remove the pixel from the neighbour (check) list |
|
|
82 |
neg_list(index,:)=neg_list(neg_pos,:); neg_pos=neg_pos-1; |
|
|
83 |
end |
|
|
84 |
|
|
|
85 |
% Return the segmented area as logical matrix |
|
|
86 |
J=J>1; |
|
|
87 |
|
|
|
88 |
|
|
|
89 |
|
|
|
90 |
|