forked from emreozanalkan/RegionGrowingAlgorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RegionGrowingSegmentation.m
213 lines (151 loc) · 5.75 KB
/
RegionGrowingSegmentation.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
function [ segmentedImage, binaryImage, regionMatrix ] = RegionGrowingSegmentation( image, neighborhoodType )
%REGIONGROWINGSEGMENTATION Region Growing algorithm for segmenting an image
% partition of an image into a set of non-overlapped regions whose union is the entire image
%
% image: Color or grayscale image
%
% neighborhood: 4 or 8 neighbor pixel connectivity. Default set to 8.
%
% If neighborhood is not given, we set it to 8
if nargin < 2
neighborhoodType = 8;
end
% image = double(image);
% Getting size of the image
[imageRowCount, imageColCount, imageChannelCount] = size(image);
% Same size as image, we mark visited pixels
visitedMatrix = zeros(imageRowCount, imageColCount);
% Same size as image, we mark region labels
regionMatrix = zeros(imageRowCount, imageColCount);
% Region Label Counter
currentRegionLabel = 1;
imageGray = image;
% If image is color image, we convert it to grayscale
if imageChannelCount > 1
imageGray = rgb2gray(image);
end
% We decided to find seeds in histogram peaks
% We get histogram of the image
[counts, x] = imhist(imageGray);
% We find peak points of histogram with threshold 100
[maxPeaks, ~] = peakdet(counts, 100, x);
% Peaks are sorted according to most frequent ones in descending order
maxPeaksSorted = sortrows(maxPeaks, -2);
% We get value column, removing frequency column
maxPeaksSorted = maxPeaksSorted(:, 1);
peakCount = numel(maxPeaksSorted);
display('peak count:');
display(peakCount);
% Loop while there is no unlabeled region in region matrix
while(~isempty(find(regionMatrix == 0, 1)))
% Getting unlabled seed in image
[seedRow, seedCol] = FindSeed(imageGray, regionMatrix, maxPeaksSorted);
% If no seed found
if seedRow == -1 || seedCol == -1
% tic;
% display('CheckFixUnlabeledRegions');
% regionMatrix = CheckFixUnlabeledRegions(regionMatrix);
% toc;
[seedRow, seedCol] = FindSeedFromUnlabeled(regionMatrix);
if isempty(seedRow) || isempty(seedCol)
break;
% segmentedImage = 0;
% display(regionMatrix);
% imagesc(regionMatrix);
% return;
end
% [I, J] = find(regionMatrix == 0);
%
% %error('We need to handle this part');
% segmentedImage = 0;
% display(regionMatrix);
% imagesc(regionMatrix);
% return;
end
% Region keeping t
currentRegion = 0;
if imageChannelCount > 1
currentRegion = [ image(seedRow, seedCol, 1), image(seedRow, seedCol, 2), image(seedRow, seedCol, 3) ];
else
currentRegion = [ image(seedRow, seedCol) ];
end
% Marking seed point as visited
visitedMatrix(seedRow, seedCol) = 1;
% Marking seed with current region label
regionMatrix(seedRow, seedCol) = currentRegionLabel;
% Initial Threshold for adding neighbors to region
threshold = mean(std(double(image)));
if imageChannelCount > 1
threshold = [threshold(:, :, 1), threshold(:, :, 2), threshold(:, :, 3)];
end
% Using Java.Util's ArrayDeque data structure for queue
import java.util.ArrayDeque
neighborList = ArrayDeque();
addedNeighborList = ArrayDeque();
% We first adding neighbors
AddNeighbors(imageRowCount, imageColCount, seedRow, seedCol, neighborhoodType, neighborList, visitedMatrix);
while ~neighborList.isEmpty()
%neighborListTemp = neighborList.clone();
while ~neighborList.isEmpty()
neighborData = neighborList.pop();
neighborRow = neighborData(1);
neighborCol = neighborData(2);
if imageChannelCount > 1
currentPixel = [ image(neighborRow, neighborCol, 1), image(neighborRow, neighborCol, 2), image(neighborRow, neighborCol, 3) ];
else
currentPixel = [ image(neighborRow, neighborCol) ];
end
if visitedMatrix(neighborRow, neighborCol) == 1
continue;
else
visitedMatrix(neighborRow, neighborCol) = 1;
end
regionMean = mean(currentRegion);
diff = abs( double(currentPixel) - double(regionMean) );
if diff <= threshold
currentRegion = [currentRegion; currentPixel];
regionMatrix(neighborRow, neighborCol) = currentRegionLabel;
addedNeighborList.add([neighborRow, neighborCol]);
end
end
while ~addedNeighborList.isEmpty()
addedNeighborData = addedNeighborList.pop();
AddNeighbors(imageRowCount, imageColCount, addedNeighborData(1), addedNeighborData(2), neighborhoodType, neighborList, visitedMatrix);
end
if imageChannelCount > 1
threshold = 1.6503 * mean(std(double(currentRegion)));
% threshold = 78;
else
threshold = 1.6503 * std(double(currentRegion));
% threshold = 78;
end
end
currentRegionLabel = currentRegionLabel + 10;
end
% medfilt2(regionMatrix, [5 5]);
[ segmentedImage, binaryImage ] = ColorSegments(regionMatrix);
ele = [5 5];
segmentedImage(:, :, 1) = medfilt2(segmentedImage(:, :, 1), ele);
segmentedImage(:, :, 2) = medfilt2(segmentedImage(:, :, 2), ele);
segmentedImage(:, :, 3) = medfilt2(segmentedImage(:, :, 3), ele);
se = strel('disk', 3);
segmentedImage(:, :, 1) = imclose(segmentedImage(:, :, 1), se);
segmentedImage(:, :, 2) = imclose(segmentedImage(:, :, 2), se);
segmentedImage(:, :, 3) = imclose(segmentedImage(:, :, 3), se);
%
% segmentedImage(:, :, 1) = imopen(segmentedImage(:, :, 1), se);
% segmentedImage(:, :, 2) = imopen(segmentedImage(:, :, 2), se);
% segmentedImage(:, :, 3) = imopen(segmentedImage(:, :, 3), se);
binaryImage = medfilt2(binaryImage);
figure,
% subplot(1, 3, 1);
imshow(image);
% subplot(1, 3, 2);
figure,
imshow(segmentedImage);
% subplot(1, 3, 3);
figure,
imshow(binaryImage);
% display(regionMatrix);
% imagesc(regionMatrix);
end