Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
con_comp.h
Go to the documentation of this file.
1 /**********************************************************************
2  * File: con_comp.h
3  * Description: Declaration of a Connected Component class
4  * Author: Ahmad Abdulkader
5  * Created: 2007
6  *
7  * (C) Copyright 2008, Google Inc.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  *
18  **********************************************************************/
19 
20 #ifndef CONCOMP_H
21 #define CONCOMP_H
22 
23 // The ConComp class implements the functionality needed for a
24 // Connected Component object and Connected Component (ConComp) points.
25 // The points consituting a connected component are kept in a linked-list
26 // The Concomp class provided methods to:
27 // 1- Compare components in L2R and R2L reading orders.
28 // 2- Merge ConComps
29 // 3- Compute the windowed vertical pixel density histogram for a specific
30 // windows size
31 // 4- Segment a ConComp based on the local windowed vertical pixel
32 // density histogram local minima
33 
34 namespace tesseract {
35 
36 // Implments a ConComp point in a linked list of points
37 class ConCompPt {
38  public:
39  ConCompPt(int x, int y) {
40  x_ = x;
41  y_ = y;
42  next_pt_ = NULL;
43  }
44  inline int x() { return x_; }
45  inline int y() { return y_; }
46  inline void Shift(int dx, int dy) {
47  x_ += dx;
48  y_ += dy;
49  }
50  inline ConCompPt * Next() { return next_pt_; }
51  inline void SetNext(ConCompPt *pt) { next_pt_ = pt; }
52 
53  private:
54  int x_;
55  int y_;
56  ConCompPt *next_pt_;
57 };
58 
59 class ConComp {
60  public:
61  ConComp();
62  virtual ~ConComp();
63  // accessors
64  inline ConCompPt *Head() { return head_; }
65  inline int Left() const { return left_; }
66  inline int Top() const { return top_; }
67  inline int Right() const { return right_; }
68  inline int Bottom() const { return bottom_; }
69  inline int Width() const { return right_ - left_ + 1; }
70  inline int Height() const { return bottom_ - top_ + 1; }
71 
72  // Comparer used for sorting L2R reading order
73  inline static int Left2RightComparer(const void *comp1,
74  const void *comp2) {
75  return (*(reinterpret_cast<ConComp * const *>(comp1)))->left_ +
76  (*(reinterpret_cast<ConComp * const *>(comp1)))->right_ -
77  (*(reinterpret_cast<ConComp * const *>(comp2)))->left_ -
78  (*(reinterpret_cast<ConComp * const *>(comp2)))->right_;
79  }
80 
81  // Comparer used for sorting R2L reading order
82  inline static int Right2LeftComparer(const void *comp1,
83  const void *comp2) {
84  return (*(reinterpret_cast<ConComp * const *>(comp2)))->right_ -
85  (*(reinterpret_cast<ConComp * const *>(comp1)))->right_;
86  }
87 
88  // accessors for attribues of a ConComp
89  inline bool LeftMost() const { return left_most_; }
90  inline bool RightMost() const { return right_most_; }
91  inline void SetLeftMost(bool left_most) { left_most_ = left_most; }
92  inline void SetRightMost(bool right_most) { right_most_ = right_most;
93  }
94  inline int ID () const { return id_; }
95  inline void SetID(int id) { id_ = id; }
96  inline int PtCnt () const { return pt_cnt_; }
97  // Add a new pt
98  bool Add(int x, int y);
99  // Merge two connected components in-place
100  bool Merge(ConComp *con_comp);
101  // Shifts the co-ordinates of all points by the specified x & y deltas
102  void Shift(int dx, int dy);
103  // segments a concomp based on pixel density histogram local minima
104  ConComp **Segment(int max_hist_wnd, int *concomp_cnt);
105  // creates the vertical pixel density histogram of the concomp
106  int *CreateHistogram(int max_hist_wnd);
107  // find out the seg pts by looking for local minima in the histogram
108  int *SegmentHistogram(int *hist_array, int *seg_pt_cnt);
109 
110  private:
111  int id_;
112  bool left_most_;
113  bool right_most_;
114  int left_;
115  int top_;
116  int right_;
117  int bottom_;
118  ConCompPt *head_;
119  ConCompPt *tail_;
120  int pt_cnt_;
121 };
122 }
123 
124 #endif // CONCOMP_H