Tesseract  3.02
tesseract-ocr/cube/con_comp.h
Go to the documentation of this file.
00001 /**********************************************************************
00002  * File:        con_comp.h
00003  * Description: Declaration of a Connected Component class
00004  * Author:    Ahmad Abdulkader
00005  * Created:   2007
00006  *
00007  * (C) Copyright 2008, Google Inc.
00008  ** Licensed under the Apache License, Version 2.0 (the "License");
00009  ** you may not use this file except in compliance with the License.
00010  ** You may obtain a copy of the License at
00011  ** http://www.apache.org/licenses/LICENSE-2.0
00012  ** Unless required by applicable law or agreed to in writing, software
00013  ** distributed under the License is distributed on an "AS IS" BASIS,
00014  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  ** See the License for the specific language governing permissions and
00016  ** limitations under the License.
00017  *
00018  **********************************************************************/
00019 
00020 #ifndef CONCOMP_H
00021 #define CONCOMP_H
00022 
00023 // The ConComp class implements the functionality needed for a
00024 // Connected Component object and Connected Component (ConComp) points.
00025 // The points consituting a connected component are kept in a linked-list
00026 // The Concomp class provided methods to:
00027 // 1- Compare components in L2R and R2L reading orders.
00028 // 2- Merge ConComps
00029 // 3- Compute the windowed vertical pixel density histogram for a specific
00030 // windows size
00031 // 4- Segment a ConComp based on the local windowed vertical pixel
00032 // density histogram local minima
00033 
00034 namespace tesseract {
00035 
00036 // Implments a ConComp point in a linked list of points
00037 class ConCompPt {
00038  public:
00039     ConCompPt(int x, int y) {
00040       x_ = x;
00041       y_ = y;
00042       next_pt_ = NULL;
00043     }
00044     inline int x() { return x_; }
00045     inline int y() { return y_; }
00046     inline void Shift(int dx, int dy) {
00047       x_ += dx;
00048       y_ += dy;
00049     }
00050     inline ConCompPt * Next() { return next_pt_; }
00051     inline void SetNext(ConCompPt *pt) { next_pt_ = pt; }
00052 
00053  private:
00054     int x_;
00055     int y_;
00056     ConCompPt *next_pt_;
00057 };
00058 
00059 class ConComp {
00060  public:
00061     ConComp();
00062     virtual ~ConComp();
00063     // accessors
00064     inline ConCompPt *Head() { return head_; }
00065     inline int Left() const { return left_; }
00066     inline int Top() const { return top_; }
00067     inline int Right() const { return right_; }
00068     inline int Bottom() const { return bottom_; }
00069     inline int Width() const { return right_ - left_ + 1; }
00070     inline int Height() const { return bottom_ - top_ + 1; }
00071 
00072     // Comparer used for sorting L2R reading order
00073     inline static int Left2RightComparer(const void *comp1,
00074                                          const void *comp2) {
00075       return (*(reinterpret_cast<ConComp * const *>(comp1)))->left_ +
00076           (*(reinterpret_cast<ConComp * const *>(comp1)))->right_ -
00077           (*(reinterpret_cast<ConComp * const *>(comp2)))->left_ -
00078           (*(reinterpret_cast<ConComp * const *>(comp2)))->right_;
00079     }
00080 
00081     // Comparer used for sorting R2L reading order
00082     inline static int Right2LeftComparer(const void *comp1,
00083                                          const void *comp2) {
00084       return (*(reinterpret_cast<ConComp * const *>(comp2)))->right_ -
00085           (*(reinterpret_cast<ConComp * const *>(comp1)))->right_;
00086     }
00087 
00088     // accessors for attribues of a ConComp
00089     inline bool LeftMost() const { return left_most_; }
00090     inline bool RightMost() const { return right_most_; }
00091     inline void SetLeftMost(bool left_most) { left_most_ = left_most; }
00092     inline void SetRightMost(bool right_most) { right_most_ = right_most;
00093     }
00094     inline int ID () const { return id_; }
00095     inline void SetID(int id) { id_ = id; }
00096     inline int PtCnt () const { return pt_cnt_; }
00097     // Add a new pt
00098     bool Add(int x, int y);
00099     // Merge two connected components in-place
00100     bool Merge(ConComp *con_comp);
00101     // Shifts the co-ordinates of all points by the specified x & y deltas
00102     void Shift(int dx, int dy);
00103     // segments a concomp based on pixel density histogram local minima
00104     ConComp **Segment(int max_hist_wnd, int *concomp_cnt);
00105     // creates the vertical pixel density histogram of the concomp
00106     int *CreateHistogram(int max_hist_wnd);
00107     // find out the seg pts by looking for local minima in the histogram
00108     int *SegmentHistogram(int *hist_array, int *seg_pt_cnt);
00109 
00110  private:
00111     int id_;
00112     bool left_most_;
00113     bool right_most_;
00114     int left_;
00115     int top_;
00116     int right_;
00117     int bottom_;
00118     ConCompPt *head_;
00119     ConCompPt *tail_;
00120     int pt_cnt_;
00121 };
00122 }
00123 
00124 #endif  // CONCOMP_H