tesseract  3.05.02
devanagari_processing.h
Go to the documentation of this file.
1 // Copyright 2008 Google Inc. All Rights Reserved.
2 // Author: shobhitsaxena@google.com (Shobhit Saxena)
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #ifndef TESSERACT_TEXTORD_DEVNAGARI_PROCESSING_H_
14 #define TESSERACT_TEXTORD_DEVNAGARI_PROCESSING_H_
15 
16 #include "ocrblock.h"
17 #include "params.h"
18 
19 struct Pix;
20 struct Box;
21 struct Boxa;
22 
23 extern
25  "Debug level for split shiro-rekha process.");
26 
27 extern
29  "Whether to create a debug image for split shiro-rekha process.");
30 
31 class TBOX;
32 
33 namespace tesseract {
34 
36  public:
38  hist_ = NULL;
39  length_ = 0;
40  }
41 
43  Clear();
44  }
45 
46  void Clear() {
47  if (hist_) {
48  delete[] hist_;
49  }
50  length_ = 0;
51  }
52 
53  int* hist() const { return hist_; }
54 
55  int length() const {
56  return length_;
57  }
58 
59  // Methods to construct histograms from images. These clear any existing data.
60  void ConstructVerticalCountHist(Pix* pix);
61  void ConstructHorizontalCountHist(Pix* pix);
62 
63  // This method returns the global-maxima for the histogram. The frequency of
64  // the global maxima is returned in count, if specified.
65  int GetHistogramMaximum(int* count) const;
66 
67  private:
68  int* hist_;
69  int length_;
70 };
71 
73  public:
75  NO_SPLIT = 0, // No splitting is performed for the phase.
76  MINIMAL_SPLIT, // Blobs are split minimally.
77  MAXIMAL_SPLIT // Blobs are split maximally.
78  };
79 
81  virtual ~ShiroRekhaSplitter();
82 
83  // Top-level method to perform splitting based on current settings.
84  // Returns true if a split was actually performed.
85  // If split_for_pageseg is true, the pageseg_split_strategy_ is used for
86  // splitting. If false, the ocr_split_strategy_ is used.
87  bool Split(bool split_for_pageseg);
88 
89  // Clears the memory held by this object.
90  void Clear();
91 
92  // Refreshes the words in the segmentation block list by using blobs in the
93  // input blob list.
94  // The segmentation block list must be set.
95  void RefreshSegmentationWithNewBlobs(C_BLOB_LIST* new_blobs);
96 
97  // Returns true if the split strategies for pageseg and ocr are different.
99  return pageseg_split_strategy_ != ocr_split_strategy_;
100  }
101 
102  // This only keeps a copy of the block list pointer. At split call, the list
103  // object should still be alive. This block list is used as a golden
104  // segmentation when performing splitting.
105  void set_segmentation_block_list(BLOCK_LIST* block_list) {
106  segmentation_block_list_ = block_list;
107  }
108 
109  static const int kUnspecifiedXheight = -1;
110 
111  void set_global_xheight(int xheight) {
112  global_xheight_ = xheight;
113  }
114 
115  void set_perform_close(bool perform) {
116  perform_close_ = perform;
117  }
118 
119  // Returns the image obtained from shiro-rekha splitting. The returned object
120  // is owned by this class. Callers may want to clone the returned pix to keep
121  // it alive beyond the life of ShiroRekhaSplitter object.
122  Pix* splitted_image() {
123  return splitted_image_;
124  }
125 
126  // On setting the input image, a clone of it is owned by this class.
127  void set_orig_pix(Pix* pix);
128 
129  // Returns the input image provided to the object. This object is owned by
130  // this class. Callers may want to clone the returned pix to work with it.
131  Pix* orig_pix() {
132  return orig_pix_;
133  }
134 
136  return ocr_split_strategy_;
137  }
138 
140  ocr_split_strategy_ = strategy;
141  }
142 
144  return pageseg_split_strategy_;
145  }
146 
148  pageseg_split_strategy_ = strategy;
149  }
150 
151  BLOCK_LIST* segmentation_block_list() {
152  return segmentation_block_list_;
153  }
154 
155  // This method dumps a debug image to the specified location.
156  void DumpDebugImage(const char* filename) const;
157 
158  // This method returns the computed mode-height of blobs in the pix.
159  // It also prunes very small blobs from calculation. Could be used to provide
160  // a global xheight estimate for images which have the same point-size text.
161  static int GetModeHeight(Pix* pix);
162 
163  private:
164  // Method to perform a close operation on the input image. The xheight
165  // estimate decides the size of sel used.
166  static void PerformClose(Pix* pix, int xheight_estimate);
167 
168  // This method resolves the cc bbox to a particular row and returns the row's
169  // xheight. This uses block_list_ if available, else just returns the
170  // global_xheight_ estimate currently set in the object.
171  int GetXheightForCC(Box* cc_bbox);
172 
173  // Returns a list of regions (boxes) which should be cleared in the original
174  // image so as to perform shiro-rekha splitting. Pix is assumed to carry one
175  // (or less) word only. Xheight measure could be the global estimate, the row
176  // estimate, or unspecified. If unspecified, over splitting may occur, since a
177  // conservative estimate of stroke width along with an associated multiplier
178  // is used in its place. It is advisable to have a specified xheight when
179  // splitting for classification/training.
180  void SplitWordShiroRekha(SplitStrategy split_strategy,
181  Pix* pix,
182  int xheight,
183  int word_left,
184  int word_top,
185  Boxa* regions_to_clear);
186 
187  // Returns a new box object for the corresponding TBOX, based on the original
188  // image's coordinate system.
189  Box* GetBoxForTBOX(const TBOX& tbox) const;
190 
191  // This method returns y-extents of the shiro-rekha computed from the input
192  // word image.
193  static void GetShiroRekhaYExtents(Pix* word_pix,
194  int* shirorekha_top,
195  int* shirorekha_bottom,
196  int* shirorekha_ylevel);
197 
198  Pix* orig_pix_; // Just a clone of the input image passed.
199  Pix* splitted_image_; // Image produced after the last splitting round. The
200  // object is owned by this class.
201  SplitStrategy pageseg_split_strategy_;
202  SplitStrategy ocr_split_strategy_;
203  Pix* debug_image_;
204  // This block list is used as a golden segmentation when performing splitting.
205  BLOCK_LIST* segmentation_block_list_;
206  int global_xheight_;
207  bool perform_close_; // Whether a morphological close operation should be
208  // performed before CCs are run through splitting.
209 };
210 
211 } // namespace tesseract.
212 
213 #endif // TESSERACT_TEXTORD_DEVNAGARI_PROCESSING_H_
SplitStrategy pageseg_split_strategy() const
int count(LIST var_list)
Definition: oldlist.cpp:103
bool devanagari_split_debugimage
void set_ocr_split_strategy(SplitStrategy strategy)
void set_pageseg_split_strategy(SplitStrategy strategy)
#define INT_VAR_H(name, val, comment)
Definition: params.h:265
void set_segmentation_block_list(BLOCK_LIST *block_list)
int devanagari_split_debuglevel
#define BOOL_VAR_H(name, val, comment)
Definition: params.h:268
int GetHistogramMaximum(int *count) const
void RefreshSegmentationWithNewBlobs(C_BLOB_LIST *new_blobs)
SplitStrategy ocr_split_strategy() const
Definition: rect.h:30
void DumpDebugImage(const char *filename) const
bool Split(bool split_for_pageseg)