File: connected_components.hpp

package info (click to toggle)
gamera 1:3.4.2+git20160808.1725654-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 22,312 kB
  • ctags: 24,991
  • sloc: xml: 122,324; ansic: 52,869; cpp: 50,664; python: 35,034; makefile: 118; sh: 101
file content (805 lines) | stat: -rw-r--r-- 26,451 bytes parent folder | download | duplicates (2)
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
/*
 *
 * Copyright (C) 2001-2005 Ichiro Fujinaga, Michael Droettboom, Karl MacMillan
 *               2009      Jonathan Koch, Christoph Dalitz
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef kwm01032002_connected_component_hpp
#define kwm01032002_connected_component_hpp

#include "dimensions.hpp"
#include "image.hpp"
#include "gamera.hpp"
#include "image_view_iterators.hpp"
#include "connected_components_iterators.hpp"
#include "vigra_iterators.hpp"

#include <stdexcept>
#include <exception>
#include <map>
#include <vector>

#include <stdio.h>

/*
  ConnectedComponent
	
  This class implements a filtered image view. Within a ConnectedComponent
  only those pixels that match the assigned label will be shown. This requires
  the use of a proxy type for iterator types that require an lvalue be returned
  from dereferencing (see CCProxy below). KWM

  MultiLabelCC

  This class implements a filtered image view similar to ConnectedComponent,
  but with pixels visible that not only match a single label, but a
  set of possible labels.
*/

namespace Gamera {

  template <class T> class MultiLabelCC;    
    
  template<class T>
  class ConnectedComponent
    : public ImageBase<typename T::value_type> {
  public:
    using ImageBase<typename T::value_type>::ncols;
    using ImageBase<typename T::value_type>::nrows;
    using ImageBase<typename T::value_type>::offset_x;
    using ImageBase<typename T::value_type>::offset_y;

    // standard STL typedefs
    typedef typename T::value_type value_type;
    typedef typename T::pointer pointer;
    typedef typename T::reference reference;
    typedef typename T::difference_type difference_type;
    // Gamera specific
    typedef T data_type;
	
    // Vigra typedefs
    typedef value_type PixelType;

    // convenience typedefs
    typedef ConnectedComponent self;
    typedef ImageBase<typename T::value_type> base_type;

    //
    // CONSTRUCTORS
    //
    ConnectedComponent() : base_type() {
      m_image_data = 0;
      m_label = 0;
    }

    ConnectedComponent(T& image_data)
      : base_type(image_data.offset(),
		  image_data.dim()) {
      m_image_data = &image_data;
      range_check();
      calculate_iterators();
    }
    ConnectedComponent(T& image_data, value_type label,
		       const Rect& rect)
      : base_type(rect), m_label(label) {
      m_image_data = &image_data;
      range_check();
      calculate_iterators();
    }
    ConnectedComponent(T& image_data, value_type label,
		       const Point& upper_left,
		       const Point& lower_right)
      : base_type(upper_left, lower_right), m_label(label) {
      m_image_data = &image_data;
      range_check();
      calculate_iterators();
    }
    ConnectedComponent(T& image_data, value_type label,
		       const Point& upper_left,
		       const Size& size)
      : base_type(upper_left, size), m_label(label) {
      m_image_data = &image_data;
      range_check();
      calculate_iterators();
    }

    ConnectedComponent(T& image_data, value_type label,
		       const Point& upper_left,
		       const Dim& dim)
      : base_type(upper_left, dim), m_label(label) {
      m_image_data = &image_data;
      range_check();
      calculate_iterators();
    }

    //
    // COPY CONSTRUCTORS
    //
    ConnectedComponent(const self& other, const Rect& rect)
      : base_type(rect) {
      m_image_data = other.m_image_data;
      m_label = other.label();
      range_check();
      calculate_iterators();
    }
    ConnectedComponent(const self& other, const Point& upper_left,
		       const Point& lower_right)
      : base_type(upper_left, lower_right) {
      m_image_data = other.m_image_data;
      m_label = other.label();
      range_check();
      calculate_iterators();
    }
    ConnectedComponent(const self& other, const Point& upper_left,
		       const Size& size)
      : base_type(upper_left, size) {
      m_image_data = other.m_image_data;
      m_label = other.label();
      range_check();
      calculate_iterators();
    }
    ConnectedComponent(const self& other, const Point& upper_left,
		       const Dim& dim)
      : base_type(upper_left, dim) {
      m_image_data = other.m_image_data;
      m_label = other.label();
      range_check();
      calculate_iterators();
    }

    //
    //Conversion
    //
    MultiLabelCC<T>* convert_to_mlcc(){
        MultiLabelCC<T>* mlcc=new MultiLabelCC<T>( *((T*)this->data()), m_label, this->ul(), this->lr() );
        return mlcc;
    }
    
    //
    //  FUNCTION ACCESS
    //

    value_type get(const Point& p) const {
      value_type tmp = *(m_const_begin + (p.y() * m_image_data->stride()) + p.x());
      if (tmp == m_label)
      	return tmp;
      else
      	return 0;
    }

    void set(const Point& p, value_type value) {
      // we simply set the pixel value regardless of the label
      // warning: when value different from Cc.labels, the pixel will
      // appear to be white, even though we have written a different value
      *(m_begin + (p.y() * m_image_data->stride()) + p.x()) = value;
    }

    //
    // DIMENSIONS
    //
    // redefine the dimensions change function from Rect
    virtual void dimensions_change() {
      range_check();
      calculate_iterators();
    }

    //
    // Misc
    //
    virtual T* data() const { return m_image_data; }
    ImageView<T> parent() {
      return ImageView<T>(*m_image_data, 0, 0, m_image_data->nrows(),
			   m_image_data->ncols());
    }
    ImageView<T> image() {
      return ImageView<T>(*m_image_data, this->origin(), this->dim());
    }
    value_type label() const {
      return m_label;
    }

    void label(value_type label) {
      m_label = label;
    }

    //
    // Iterators
    //
    typedef CCDetail::RowIterator<self, typename T::iterator> row_iterator;
    row_iterator row_begin() {
      return row_iterator(this, m_begin);
    }
    row_iterator row_end() {
      return row_iterator(this, m_end);
    }

    typedef CCDetail::ColIterator<self, typename T::iterator> col_iterator;
    col_iterator col_begin() {
      return col_iterator(this, m_begin);
    }
    col_iterator col_end() {
      return col_iterator(this, m_begin + ncols());
    }

    //
    // Const Iterators
    //
    typedef CCDetail::ConstRowIterator<const self, typename T::const_iterator> const_row_iterator;
    const_row_iterator row_begin() const {
      return const_row_iterator(this, m_const_begin);
    }
    const_row_iterator row_end() const {
      return const_row_iterator(this, m_const_end);
    }

    typedef CCDetail::ConstColIterator<const self, typename T::const_iterator> const_col_iterator;
    const_col_iterator col_begin() const {
      return const_col_iterator(this, m_const_begin);
    }
    const_col_iterator col_end() const {
      return const_col_iterator(this, m_const_begin + ncols());
    }

    //
    // 2D iterators
    //
    typedef Gamera::ImageIterator<ConnectedComponent, typename T::iterator> Iterator;

    Iterator upperLeft() {
      return Iterator(this, m_image_data->begin(), m_image_data->stride())
	+ Diff2D(offset_x() - m_image_data->page_offset_x(), offset_y() - m_image_data->page_offset_y());
    }
    Iterator lowerRight() {
      return Iterator(this, m_image_data->begin(), m_image_data->stride())
	+ Diff2D(offset_x() + ncols() - m_image_data->page_offset_x(),
		 offset_y() + nrows() - m_image_data->page_offset_y());
    }

    typedef Gamera::ConstImageIterator<const ConnectedComponent,
				  typename T::const_iterator> ConstIterator;
    ConstIterator upperLeft() const {
      return ConstIterator(this, static_cast<const T*>(m_image_data)->begin(), m_image_data->stride())
	+ Diff2D(offset_x() - m_image_data->page_offset_x(),
		 offset_y() - m_image_data->page_offset_y());
    }
    ConstIterator lowerRight() const {
      return ConstIterator(this, static_cast<const T*>(m_image_data)->begin(), m_image_data->stride())
	+ Diff2D(offset_x() + ncols() - m_image_data->page_offset_x(),
		 offset_y() + nrows() - m_image_data->page_offset_y());
    }

    //
    // Vector iterator
    //
    typedef CCDetail::VecIterator<self, row_iterator, col_iterator> vec_iterator;
    vec_iterator vec_begin() { return vec_iterator(row_begin()); }
    vec_iterator vec_end() { return vec_iterator(row_end()); }

    typedef CCDetail::ConstVecIterator<self,
      const_row_iterator, const_col_iterator> const_vec_iterator;
    const_vec_iterator vec_begin() const {
      return const_vec_iterator(row_begin());
    }
    const_vec_iterator vec_end() const {
      return const_vec_iterator(row_end());
    }

    //
    // OPERATOR ACCESS
    //
    col_iterator operator[](size_t n) {
      return col_iterator(this, m_begin + (n * data()->stride())); }
    const_col_iterator operator[](size_t n) const {
      return const_col_iterator(this, m_begin + (n * data()->stride())); }
  private:
    /*
      We pre-compute iterators here in an effort to make begin() and end()
      methods a little faster. Unfortunately we have to to keep around both
      normal and const iterators.
    */
    void calculate_iterators() {
      m_begin = m_image_data->begin()
        // row offset
        + (m_image_data->stride() * (offset_y() - m_image_data->page_offset_y()))
        // col offset
        + (offset_x() - m_image_data->page_offset_x());
      m_end = m_image_data->begin()
        // row offset
        + (m_image_data->stride() * ((offset_y() - m_image_data->page_offset_y()) + nrows()))
        // column offset
        + (offset_x() - m_image_data->page_offset_x());
      const T* cmd = static_cast<const T*>(m_image_data);
      m_const_begin = cmd->begin()
        // row offset
	+ (m_image_data->stride() * (offset_y() - m_image_data->page_offset_y()))
        // col offset
        + (offset_x() - m_image_data->page_offset_x());
      m_const_end = cmd->begin()
        // row offset
        + (m_image_data->stride() * ((offset_y() - m_image_data->page_offset_y()) + nrows()))
        // column offset
        + (offset_x() - m_image_data->page_offset_x());
    }
    void range_check() {
      if (offset_y() + nrows() - m_image_data->page_offset_y() > m_image_data->nrows() ||
	  offset_x() + ncols() - m_image_data->page_offset_x() > m_image_data->ncols()
	  || offset_y() < m_image_data->page_offset_y()
	  || offset_x() < m_image_data->page_offset_x()) {
	char error[1024];
	sprintf(error, "Image view dimensions out of range for data\n");
	sprintf(error, "%s\tnrows %d\n", error, (int)nrows());
	sprintf(error, "%s\toffset_y %d\n", error, (int)offset_y());
	sprintf(error, "%s\tdata nrows %d\n", error, (int)m_image_data->nrows());
	sprintf(error, "%s\tncols %d\n", error, (int)ncols());
	sprintf(error, "%s\toffset_x %d\n", error, (int)offset_x());
	sprintf(error, "%s\tdata ncols %d\n", error,(int)m_image_data->ncols());
	throw std::range_error(error);
      }
    }
    // Pointer to the data for this view
    T* m_image_data;
    // Cached iterators - see calculate_iterators above.
    typename T::iterator m_begin, m_end;
    typename T::const_iterator m_const_begin, m_const_end;
    // The label for this connected-component
    value_type m_label;
  };


  template<class T>
  class MultiLabelCC : public ImageBase<typename T::value_type> {
  public:
    using ImageBase<typename T::value_type>::ncols;
    using ImageBase<typename T::value_type>::nrows;
    using ImageBase<typename T::value_type>::offset_x;
    using ImageBase<typename T::value_type>::offset_y;

    // standard STL typedefs
    typedef typename T::value_type value_type;
    typedef typename T::pointer pointer;
    typedef typename T::reference reference;
    typedef typename T::difference_type difference_type;
    // Gamera specific
    typedef T data_type;
	
    // Vigra typedefs
    typedef value_type PixelType;

    // convenience typedefs
    typedef MultiLabelCC self;
    typedef ImageBase<typename T::value_type> base_type;

    //
    // CONSTRUCTORS
    //
    MultiLabelCC() : base_type() {
      m_image_data = 0;
    }

    MultiLabelCC(T& image_data)
      : base_type(image_data.offset(),
		  image_data.dim()) {
      m_image_data = &image_data;
      range_check();
      calculate_iterators();
    }
    
    MultiLabelCC(T& image_data, value_type label,
		       const Rect& rect)
      : base_type(rect){
      m_image_data = &image_data;
      range_check();
      calculate_iterators();

      m_labels[label]=new Rect(rect);
    }
    MultiLabelCC(T& image_data, value_type label,
		       const Point& upper_left,
		       const Point& lower_right)
      : base_type(upper_left, lower_right){
      m_image_data = &image_data;
      range_check();
      calculate_iterators();

      m_labels[label]=new Rect(upper_left, lower_right);
    }
    MultiLabelCC(T& image_data, value_type label,
		       const Point& upper_left,
		       const Size& size)
      : base_type(upper_left, size){
      m_image_data = &image_data;
      range_check();
      calculate_iterators();

      m_labels[label]=new Rect(upper_left, size);
    }

    MultiLabelCC(T& image_data, value_type label,
		       const Point& upper_left,
		       const Dim& dim)
      : base_type(upper_left, dim){
      m_image_data = &image_data;
      range_check();
      calculate_iterators();

      m_labels[label]=new Rect(upper_left, dim);
    }
    
    //
    // DESTRUCTOR
    //
    virtual ~MultiLabelCC(){
      for(it=m_labels.begin(); it!=m_labels.end(); it++){
         delete it->second;
      }
    }

    //
    // COPY CONSTRUCTORS
    //
    MultiLabelCC(const self& other, const Rect& rect)
      : base_type(rect) {
      m_image_data = other.m_image_data;
      copy_labels(other);
      m_neighbors=other.m_neighbors;
      range_check();
      calculate_iterators();
    }
    MultiLabelCC(const self& other, const Point& upper_left,
		       const Point& lower_right)
      : base_type(upper_left, lower_right) {
      m_image_data = other.m_image_data;
      copy_labels(other);
      m_neighbors=other.m_neighbors;
      range_check();
      calculate_iterators();
    }
    MultiLabelCC(const self& other, const Point& upper_left,
		       const Size& size)
      : base_type(upper_left, size) {
      m_image_data = other.m_image_data;
      copy_labels(other);
      m_neighbors=other.m_neighbors;
      range_check();
      calculate_iterators();
    }
    MultiLabelCC(const self& other, const Point& upper_left,
		       const Dim& dim)
      : base_type(upper_left, dim) {
      m_image_data = other.m_image_data;
      copy_labels(other);

      m_neighbors=other.m_neighbors;
      range_check();
      calculate_iterators();
    }
      
    //
    //Conversion
    //
    typename std::list<ConnectedComponent<T>*>* convert_to_cc_list(){
      typename std::list<ConnectedComponent<T>*>* cc_list=new std::list<ConnectedComponent<T>*>();
      ConnectedComponent<T>* cc;
      for(it=m_labels.begin(); it!=m_labels.end(); it++){
        cc=new ConnectedComponent<T>( *((T*)this->data()), it->first, *(it->second));
        cc_list->push_back(cc);
      }
      return cc_list;
    }

    ConnectedComponent<T>* convert_to_cc(){
      // we must use iterators because set() is blocked for MlCc's
      typename MultiLabelCC::vec_iterator i = this->vec_begin();
      int label = m_labels.begin()->first;
      for ( ; i != this->vec_end(); ++i) {
        if (is_black(*i)) *i = label;
      }
      for(it=m_labels.begin(); it!=m_labels.end(); it++){
         delete it->second;
      }
      m_labels.clear();
      m_labels[label] = new Rect((Rect)*this);
      ConnectedComponent<T>* cc=new ConnectedComponent<T>( *((T*)this->data()), label, this->ul(), this->lr() );
      return cc;
    }

    //
    //  FUNCTION ACCESS
    //
    value_type get(const Point& point) const{
      value_type tmp = *(m_const_begin + (point.y() * m_image_data->stride()) + point.x());
      if(m_labels.find(tmp) != m_labels.end())
        return tmp;
      else
        return 0;    		
      }

    void set(const Point& p, value_type value) {
      // we simply set the pixel value regardless of the label
      // warning: when value is neither of the labels, the pixel will
      // appear to be white, even though we have written a different value
      *(m_begin + (p.y() * m_image_data->stride()) + p.x()) = value;
    }

    //
    // DIMENSIONS
    //
    // redefine the dimensions change function from Rect
    virtual void dimensions_change() {
      range_check();
      calculate_iterators();
    }

    //
    // Misc
    //
    virtual T* data() const { return m_image_data; }
    ImageView<T> parent() {
      return ImageView<T>(*m_image_data, 0, 0, m_image_data->nrows(),
			   m_image_data->ncols());
    }
    ImageView<T> image() {
      return ImageView<T>(*m_image_data, this->origin(), this->dim());
    }
    
/*
    typename std::map<value_type, Rect*> labels(){
      return m_labels;
    }
*/

    void get_labels(typename std::vector<int> &labels){
      for (it=m_labels.begin(); it!=m_labels.end(); it++){
        labels.push_back(it->first);
      }
    }
    
    bool has_label(value_type label) const {
    	return m_labels.find(label) != m_labels.end();
    }
    
    void add_label(value_type label, Rect& rect) {
      if(m_labels.size()==0){
        this->rect_set(rect.ul(),rect.lr());
      }
      //beware rect is only a reference and m_labels just stores pointers => you have to make a copy of rect
      m_labels[label]=new Rect(rect);
      this->union_rect(rect);
    }

    void remove_label(value_type label){
      it=m_labels.find(label);
      if(it!=m_labels.end()){
        delete it->second;
        m_labels.erase(label);
        find_bounding_box();
      }
    }
    
    void find_bounding_box(){
      if(m_labels.size()==0){
        this->rect_set(Point(0,0), Point(0,0));
      } else {
        coord_t maxX = 0;
        coord_t maxY = 0;
        coord_t minX = std::numeric_limits<coord_t>::max();
        coord_t minY = std::numeric_limits<coord_t>::max();
        
        for(this->it=this->m_labels.begin(); this->it!=this->m_labels.end(); this->it++){
          if(maxX<this->it->second->lr_x()) maxX=this->it->second->lr_x();
          if(maxY<this->it->second->lr_y()) maxY=this->it->second->lr_y();
          if(minX>this->it->second->ul_x()) minX=this->it->second->ul_x();
          if(minY>this->it->second->ul_y()) minY=this->it->second->ul_y();
        }
        
        this->rect_set(Point(minX,minY), Point(maxX,maxY));
      }
    }
    
    void add_neighbors(value_type i, value_type j){
      m_neighbors.push_back(i);
      m_neighbors.push_back(j);
    }

    void get_neighbors(typename std::vector<int> &neighbors){
      neighbors=this->m_neighbors;
    }

    void relabel(typename std::vector<typename std::vector<int>* >& labelVector, typename std::vector<self*>& mlccs){
      self* mlcc;
      for (size_t i=0; i<labelVector.size(); i++){
        mlcc=new self(*((T*)this->data()));
        mlccs.push_back(mlcc);
        for (size_t j=0; j<labelVector[i]->size(); j++){
          Rect* rect=m_labels[labelVector[i]->at(j)];
          if(rect!=NULL){
            value_type label=(value_type)(labelVector[i]->at(j));
            mlcc->add_label(label, *rect);
          } else {
            //tidy up
            for (size_t k=0; k<mlccs.size(); k++)
              delete mlccs[k];
            char error[64];
            sprintf(error, "There is no label %d stored in this MLCC.\n", labelVector[i]->at(j));
            throw std::runtime_error(error);
          }
        }
      }
    }

    //
    // Iterators
    //
    typedef MLCCDetail::RowIterator<self, typename T::iterator> row_iterator;
    row_iterator row_begin() {
      return row_iterator(this, m_begin);
    }
    row_iterator row_end() {
      return row_iterator(this, m_end);
    }

    typedef MLCCDetail::ColIterator<self, typename T::iterator> col_iterator;
    col_iterator col_begin() {
      return col_iterator(this, m_begin);
    }
    col_iterator col_end() {
      return col_iterator(this, m_begin + ncols());
    }

    //
    // Const Iterators
    //
    typedef MLCCDetail::ConstRowIterator<const self, typename T::const_iterator> const_row_iterator;
    const_row_iterator row_begin() const {
      return const_row_iterator(this, m_const_begin);
    }
    const_row_iterator row_end() const {
      return const_row_iterator(this, m_const_end);
    }

    typedef MLCCDetail::ConstColIterator<const self, typename T::const_iterator> const_col_iterator;
    const_col_iterator col_begin() const {
      return const_col_iterator(this, m_const_begin);
    }
    const_col_iterator col_end() const {
      return const_col_iterator(this, m_const_begin + ncols());
    }

    //
    // 2D iterators
    //
    typedef Gamera::ImageIterator<MultiLabelCC, typename T::iterator> Iterator;

    Iterator upperLeft() {
      return Iterator(this, m_image_data->begin(), m_image_data->stride())
        + Diff2D(offset_x() - m_image_data->page_offset_x(), offset_y() - m_image_data->page_offset_y());
    }
    Iterator lowerRight() {
      return Iterator(this, m_image_data->begin(), m_image_data->stride())
        + Diff2D(offset_x() + ncols() - m_image_data->page_offset_x(),
            offset_y() + nrows() - m_image_data->page_offset_y());
    }

    typedef Gamera::ConstImageIterator<const MultiLabelCC,
				  typename T::const_iterator> ConstIterator;
    ConstIterator upperLeft() const {
      return ConstIterator(this, static_cast<const T*>(m_image_data)->begin(), m_image_data->stride())
        + Diff2D(offset_x() - m_image_data->page_offset_x(),
            offset_y() - m_image_data->page_offset_y());
    }
    ConstIterator lowerRight() const {
      return ConstIterator(this, static_cast<const T*>(m_image_data)->begin(), m_image_data->stride())
        + Diff2D(offset_x() + ncols() - m_image_data->page_offset_x(),
            offset_y() + nrows() - m_image_data->page_offset_y());
    }

    //
    // Vector iterator
    //
    typedef MLCCDetail::VecIterator<self, row_iterator, col_iterator> vec_iterator;
    vec_iterator vec_begin() { return vec_iterator(row_begin()); }
    vec_iterator vec_end() { return vec_iterator(row_end()); }

    typedef MLCCDetail::ConstVecIterator<self,
      const_row_iterator, const_col_iterator> const_vec_iterator;
    const_vec_iterator vec_begin() const {
      return const_vec_iterator(row_begin());
    }
    const_vec_iterator vec_end() const {
      return const_vec_iterator(row_end());
    }

    //
    // OPERATOR ACCESS
    //
    col_iterator operator[](size_t n) {
      return col_iterator(this, m_begin + (n * data()->stride())); }
    const_col_iterator operator[](size_t n) const {
      return const_col_iterator(this, m_begin + (n * data()->stride())); }

    //for initialization of iterators
    const typename std::map<value_type, Rect*>* get_labels_pointer() const {
      return &m_labels;
    }
  private:
    void copy_labels(const self& other){
      typename std::map<value_type, Rect*>::const_iterator iter;
      for (iter = other.m_labels.begin(); iter != other.m_labels.end(); iter++){
        m_labels[iter->first]=new Rect(*(iter->second));
      }
    }

    /*
      We pre-compute iterators here in an effort to make begin() and end()
      methods a little faster. Unfortunately we have to keep around both
      normal and const iterators.
    */
    void calculate_iterators() {
      m_begin = m_image_data->begin()
        // row offset
        + (m_image_data->stride() * (offset_y() - m_image_data->page_offset_y()))
        // col offset
        + (offset_x() - m_image_data->page_offset_x());
      m_end = m_image_data->begin()
        // row offset
        + (m_image_data->stride() * ((offset_y() - m_image_data->page_offset_y()) + nrows()))
        // column offset
        + (offset_x() - m_image_data->page_offset_x());
      const T* cmd = static_cast<const T*>(m_image_data);
      m_const_begin = cmd->begin()
        // row offset
        + (m_image_data->stride() * (offset_y() - m_image_data->page_offset_y()))
        // col offset
        + (offset_x() - m_image_data->page_offset_x());
      m_const_end = cmd->begin()
        // row offset
        + (m_image_data->stride() * ((offset_y() - m_image_data->page_offset_y()) + nrows()))
        // column offset
        + (offset_x() - m_image_data->page_offset_x());
    }
    void range_check() {
      if (offset_y() + nrows() - m_image_data->page_offset_y() > m_image_data->nrows() ||
    	  offset_x() + ncols() - m_image_data->page_offset_x() > m_image_data->ncols()
    	  || offset_y() < m_image_data->page_offset_y()
    	  || offset_x() < m_image_data->page_offset_x()) {
      	char error[1024];
      	sprintf(error, "Image view dimensions out of range for data\n");
      	sprintf(error, "%s\tnrows %d\n", error, (int)nrows());
      	sprintf(error, "%s\toffset_y %d\n", error, (int)offset_y());
      	sprintf(error, "%s\tdata nrows %d\n", error, (int)m_image_data->nrows());
      	sprintf(error, "%s\tncols %d\n", error, (int)ncols());
      	sprintf(error, "%s\toffset_x %d\n", error, (int)offset_x());
      	sprintf(error, "%s\tdata ncols %d\n", error,(int)m_image_data->ncols());
      	throw std::range_error(error);
      }
    }
    // Pointer to the data for this view
    T* m_image_data;
    // Cached iterators - see calculate_iterators above.
    typename T::iterator m_begin, m_end;
    typename T::const_iterator m_const_begin, m_const_end;

    // The labels/rects for this connected-component
    typename std::map<value_type, Rect*> m_labels;
    typename std::map<value_type, Rect*>::iterator it;

    // The neighborhood-relations
    typename std::vector<int> m_neighbors;
  };
}

#endif