forked from larrylindsey/imageprocessing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMser.cpp
More file actions
547 lines (386 loc) · 13.9 KB
/
Copy pathMser.cpp
File metadata and controls
547 lines (386 loc) · 13.9 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#include <util/Logger.h>
#include "Mser.h"
logger::LogChannel mserlog("mserlog", "[Mser] ");
template <typename Precision>
Precision Mser<Precision>::MaxValue;
template <typename Precision>
Mser<Precision>::Mser() {
MaxValue = std::numeric_limits<Precision>::max();
registerInput(_image, "image");
registerInput(_parameters, "parameters");
registerOutput(_componentTree, "component tree");
_neighborOffsets.push_back(util::point<int>(-1, 0)); // left
_neighborOffsets.push_back(util::point<int>( 0, -1)); // up
_neighborOffsets.push_back(util::point<int>( 1, 0)); // right
_neighborOffsets.push_back(util::point<int>( 0, 1)); // down
}
template <typename Precision>
void
Mser<Precision>::updateOutputs() {
process();
}
template <typename Precision>
void
Mser<Precision>::process() {
LOG_DEBUG(mserlog) << "starting MSER extraction..." << std::endl;
allocate();
copyImage();
if (_parameters->darkToBright) {
LOG_DEBUG(mserlog) << "...from dark to bright..." << std::endl;
process(true);
}
if (_parameters->brightToDark) {
LOG_DEBUG(mserlog) << "...from bright to dark..." << std::endl;
process(false);
}
createComponentTree();
deallocate();
LOG_DEBUG(mserlog) << "done" << std::endl;
}
template <typename Precision>
void
Mser<Precision>::allocate() {
_size = _image->width()*_image->height();
LOG_ALL(mserlog) << "allocating memory for " << _size << " pixels and " << (MaxValue+1) << " values" << std::endl;
_values.resize(_size);
_visited.resize(_size);
_pixelList.resize(_size);
_nextNeighbors.resize(_size);
_stacks.resize(MaxValue+1);
_histories.resize(_size);
_regions.resize(MaxValue+2);
}
template <typename Precision>
void
Mser<Precision>::deallocate() {
LOG_ALL(mserlog) << "deallocating memory" << std::endl;
std::vector<Precision>().swap(_values);
std::vector<bool>().swap(_visited);
std::vector<Precision>().swap(_nextNeighbors);
std::vector<std::stack<unsigned int> >().swap(_stacks);
std::vector<GrowHistory>().swap(_histories);
std::vector<mser::Region>().swap(_regions);
_pixelList.clear();
}
template <typename Precision>
void
Mser<Precision>::copyImage() {
if (!_parameters->sameIntensityComponents) {
unsigned int i = 0;
for (Image::iterator p = _image->begin(); p != _image->end(); i++, p++)
_values[i] = (Precision)((*p)*(double)MaxValue);
} else {
for (unsigned int x = 0; x < _image->width(); x++)
for (unsigned int y = 0; y < _image->height(); y++) {
Precision value = (Precision)((*_image)(x, y)*(double)MaxValue);
if (x > 0) {
Precision leftValue = (Precision)((*_image)(x - 1, y)*(double)MaxValue);
if (leftValue != value)
value = 0;
}
if (y > 0) {
Precision topValue = (Precision)((*_image)(x, y - 1)*(double)MaxValue);
if (topValue != value)
value = 0;
}
_values[positionToIndex(util::point<unsigned int>(x, y))] = value;
}
}
}
template <typename Precision>
void
Mser<Precision>::reset() {
// reset the contents of the per-pixel data strucures
for (int i = 0; i < _size; i++) {
_visited[i] = false;
_nextNeighbors[i] = 0;
_histories[i] = GrowHistory();
}
// reset the contents of the per-value data structures
for (unsigned int i = 0; i < _stacks.size(); i++)
_stacks[i] = std::stack<unsigned int>();
for (unsigned int i = 0; i < _regions.size(); i++)
_regions[i] = mser::Region();
// reset counters
_currentRegion = 0;
_currentHistory = 0;
// clear the found msers
_msers.clear();
}
template <typename Precision>
void
Mser<Precision>::process(bool darkToBright) {
LOG_DEBUG(mserlog) << "Processing from " << (darkToBright ? "dark to bright" : "bright to dark") << std::endl;
reset();
// add dummy region
_regions[_currentRegion] = mser::Region(MaxValue+1, &_pixelList, _image, &(*_parameters));
// setup indices
_curIndex = 0;
_curPosition = util::point<int>(0, 0);
util::point<int> neighborPosition(0, 0);
// get value of first pixel to process
_curValue = (darkToBright ? _values[_curIndex] : MaxValue - _values[_curIndex]);
// start a first region
_currentRegion++;
_regions[_currentRegion] = mser::Region(_curValue, &_pixelList, _image, &(*_parameters));
// remember that we have been here
_visited[_curIndex] = true;
// select the stack of pixel indices that have the current value
int curStack = _curValue;
// initialise a variable to measure the progress
int progress = 0;
while (true) {
// TODO:
//setProgress(progress);
// as long as we didn't see every neighbor
while (_nextNeighbors[_curIndex] < 4) {
// set neighborPosition to next neighbor of current position
neighborPosition = _curPosition + _neighborOffsets[_nextNeighbors[_curIndex]];
// check if we are still inside the image
if (neighborPosition.x < 0 || neighborPosition.x >= _image->width() ||
neighborPosition.y < 0 || neighborPosition.y >= _image->height()) {
// process next neighbor
_nextNeighbors[_curIndex]++;
continue;
}
// get the index of the neighbor pixel
int neighborIndex = positionToIndex(neighborPosition);
// if not visited already
if (!_visited[neighborIndex]) {
// remember that we looked at this pixel already
_visited[neighborIndex] = true;
progress++;
// get the value of the neighbor
Precision neighborValue = (darkToBright ? _values[neighborIndex] : MaxValue - _values[neighborIndex]);
// neighbor value smaller than current value?
if (neighborValue < _curValue) {
// add _current_ pixel to bundary heap and continue
// processing the _neighbor_ pixel instead
_stacks[curStack].push(_curIndex);
// done with this neighbor
_nextNeighbors[_curIndex]++;
// continue with neighbor pixel
curStack = neighborValue;
_curIndex = neighborIndex;
_curPosition = neighborPosition;
_curValue = neighborValue;
// create a new region for it
_currentRegion++;
_regions[_currentRegion] = mser::Region(_curValue, &_pixelList, _image, &(*_parameters));
// look at the neighbors of the new current pixel
continue;
// neighbor value equal or bigger than current value
} else {
// add the neighbor pixel to the boundary heap
_stacks[neighborValue].push(neighborIndex);
}
}
// continue with the next neighbor
_nextNeighbors[_curIndex]++;
}
// add current pixel to current region
_regions[_currentRegion].addPosition(_curIndex, indexToPosition(_curIndex), _values[_curIndex]);
// try to get the next pixel of equal value from the boundary heap
// if there are some...
if (!_stacks[curStack].empty()) {
// ...make the first one the current pixel
_curIndex = _stacks[curStack].top();
_stacks[curStack].pop();
_curValue = (darkToBright ? _values[_curIndex] : MaxValue - _values[_curIndex]);
// get the position from the index
_curPosition = indexToPosition(_curIndex);
// if there are none...
} else {
// start searching in the next stack...
curStack++;
// ...until we find a non-empty one
int nextValue = 0;
for (int i = curStack; i <= MaxValue; i++) {
if (!_stacks[curStack].empty()) {
nextValue = i;
break;
}
curStack++;
}
// there was a next non-empty heap
if (nextValue != 0) {
// set current pixel to next one in the heap
_curIndex = _stacks[curStack].top();
_stacks[curStack].pop();
_curValue = (darkToBright ? _values[_curIndex] : MaxValue - _values[_curIndex]);
_curPosition = indexToPosition(_curIndex);
processStack(nextValue);
} else { // no next non-empty heap
break;
}
} // if current heap empty
} // while (true)
// TODO:
//setProgress(progress);
}
template <typename Precision>
void
Mser<Precision>::processStack(int nextValue) {
while (true) {
processCurrentRegion();
if (nextValue < _regions[_currentRegion - 1].getValue()) {
setCurrentRegionValue(nextValue);
return;
} else {
_regions[_currentRegion - 1].merge(&_regions[_currentRegion], &_histories[_currentHistory]);
_currentRegion--;
_currentHistory++;
if (nextValue <= _regions[_currentRegion].getValue())
return;
}
}
}
template <typename Precision>
void
Mser<Precision>::processCurrentRegion() {
// check for stability of the current region
if (_regions[_currentRegion].isStable()) {
/*
* (stable) A A (can grow further)
* / \ ⇒ |
* / \ A' (stable copy)
* / \ / \
* B C B C
*/
// create a copy of the current region
// (We need to do this, because the original region will be further
// worked with. In particular, it will grow. Therefore, we just add an
// artificial region in between that will not be changed)
_msers.push_back(_regions[_currentRegion]);
int copyId = _msers.size() - 1;
// all children of the current region are not top-level msers anymore
foreach (int child, _regions[_currentRegion].getChildRegions())
_msers[child].setTopLevel(false);
// make the original region point to this copy as a child
_regions[_currentRegion].setChildRegion(copyId);
}
}
template <typename Precision>
void
Mser<Precision>::setCurrentRegionValue(int value) {
_regions[_currentRegion].addHistory(&_histories[_currentHistory]);
_regions[_currentRegion].setValue(value);
_currentHistory++;
}
template <typename Precision>
void
Mser<Precision>::createComponentTree() {
LOG_DEBUG(mserlog) << "creating component tree for " << _msers.size() << " regions" << std::endl;
_componentTree->clear();
/* Here, we "straighten out" the pixel list that was build by the mser
* algorithm. This means, that
*
* b e
* value 5 3 6 9 2 8 1 0 4 7
* -------------------
* index 0 1 2 3 4 5 6 7 8 9
*
* becomes
*
* b e
* value 1 3 9 7 0 5 8 4 2 6
* -------------------
* index 0 1 2 3 4 5 6 7 8 9
* .
*
* The conversion is done by traversing through all found regions in the
* component tree and converting the old pixel indices on-the-fly into 2D
* pixel positions.
*
* For that, the global begin and end of the old pixel list has to be found
* first.
*/
// find the first and last pixel index
int begin = PixelList::None;
int end = PixelList::None;
for (unsigned int i = 0; i < _pixelList.size() && (begin == PixelList::None || end == PixelList::None); i++) {
if (_pixelList.prev[i] == PixelList::None)
begin = i;
if (_pixelList.next[i] == PixelList::None)
end= i;
}
// create an artifical root region that contains all pixels
mser::Region root(MaxValue, &_pixelList, _image, &(*_parameters), begin, end);
// add all top-level msers as children to root
for (unsigned int i = 0; i < _msers.size(); i++)
if (_msers[i].isTopLevel())
root.addChildRegion(i);
// add it to list of msers
_msers.push_back(root);
int rootId = _msers.size() - 1;
// create a shared new pixel list
boost::shared_ptr<std::vector<util::point<unsigned int> > > sharedPixelList = boost::make_shared<std::vector<util::point<unsigned int> > >(_pixelList.size());
// the current pixel in the new pixel list
unsigned int currentPixel = 0;
// create the tree and straighten out the pixel list on-the-fly
boost::shared_ptr<ComponentTree::Node> rootNode = createSubComponentTree(sharedPixelList, currentPixel, rootId);
// set the root of the component tree
_componentTree->setRoot(rootNode);
LOG_DEBUG(mserlog) << "created component tree" << std::endl;
}
template <typename Precision>
boost::shared_ptr<ComponentTree::Node>
Mser<Precision>::createSubComponentTree(boost::shared_ptr<std::vector<util::point<unsigned int> > > sharedPixelList, unsigned int& currentPixel, int mserId) {
// get the old pixel list indices
int head = _msers[mserId].getHeadIndex();
int tail = _msers[mserId].getTailIndex();
// the begin of this component in the shared pixel list
int begin = currentPixel;
// get the start indices of all children
std::map<unsigned int, int> indexToChildId;
foreach (int child, _msers[mserId].getChildRegions())
indexToChildId[_msers[child].getHeadIndex()] = child;
// create a list of children
std::vector<boost::shared_ptr<ComponentTree::Node> > children;
/* Go over all old pixel indices of this region. Whenever we detect the
* beginning of a child region, we let the child fill the pixel list.
*/
for (int i = head;; i = _pixelList.next[i]) {
// we are at the beginning of a child region
if (indexToChildId.count(i)) {
int childId = indexToChildId[i];
// let the child fill the pixel list
children.push_back(createSubComponentTree(sharedPixelList, currentPixel, childId));
// continue behind the child pixels
i = _msers[childId].getTailIndex();
continue;
}
// we found a pixel that exclusively belongs to us
(*sharedPixelList)[currentPixel] = util::point<unsigned int>(i%_image->width(), i/_image->width());
currentPixel++;
if (i == tail)
break;
}
// the end (exclusive) of this component in the shared pixel list
int end = currentPixel;
// create a new connected component
boost::shared_ptr<Image> source = _image;
boost::shared_ptr<ConnectedComponent> component = boost::make_shared<ConnectedComponent>(source, (double)_msers[mserId].getValue()/(double)MaxValue, sharedPixelList, begin, end);
// create a component tree node for this connected component
boost::shared_ptr<ComponentTree::Node> componentNode = boost::make_shared<ComponentTree::Node>(component);
// add the children
foreach (boost::shared_ptr<ComponentTree::Node> childNode, children)
componentNode->addChild(childNode);
return componentNode;
}
template <typename Precision>
util::point<unsigned int>
Mser<Precision>::indexToPosition(unsigned int index) {
util::point<int> position;
position.x = index%_image->width();
position.y = index/_image->width();
return position;
}
template <typename Precision>
unsigned int
Mser<Precision>::positionToIndex(const util::point<int>& position) {
return position.y*_image->width() + position.x;
}
// explicit template instantiation
template class Mser<unsigned char>;
template class Mser<unsigned short>;