tesseract  4.0.0-1-g2a2b
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 "allheaders.h"
17 #include "debugpixa.h"
18 #include "ocrblock.h"
19 #include "params.h"
20 
21 struct Pix;
22 struct Box;
23 struct Boxa;
24 
25 extern
27  "Debug level for split shiro-rekha process.");
28 
29 extern
31  "Whether to create a debug image for split shiro-rekha process.");
32 
33 class TBOX;
34 
35 namespace tesseract {
36 
38  public:
40  hist_ = nullptr;
41  length_ = 0;
42  }
43 
45  Clear();
46  }
47 
48  void Clear() {
49  delete[] hist_;
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, DebugPixa* pixa_debug);
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 returns the computed mode-height of blobs in the pix.
156  // It also prunes very small blobs from calculation. Could be used to provide
157  // a global xheight estimate for images which have the same point-size text.
158  static int GetModeHeight(Pix* pix);
159 
160  private:
161  // Method to perform a close operation on the input image. The xheight
162  // estimate decides the size of sel used.
163  static void PerformClose(Pix* pix, int xheight_estimate);
164 
165  // This method resolves the cc bbox to a particular row and returns the row's
166  // xheight. This uses block_list_ if available, else just returns the
167  // global_xheight_ estimate currently set in the object.
168  int GetXheightForCC(Box* cc_bbox);
169 
170  // Returns a list of regions (boxes) which should be cleared in the original
171  // image so as to perform shiro-rekha splitting. Pix is assumed to carry one
172  // (or less) word only. Xheight measure could be the global estimate, the row
173  // estimate, or unspecified. If unspecified, over splitting may occur, since a
174  // conservative estimate of stroke width along with an associated multiplier
175  // is used in its place. It is advisable to have a specified xheight when
176  // splitting for classification/training.
177  void SplitWordShiroRekha(SplitStrategy split_strategy,
178  Pix* pix,
179  int xheight,
180  int word_left,
181  int word_top,
182  Boxa* regions_to_clear);
183 
184  // Returns a new box object for the corresponding TBOX, based on the original
185  // image's coordinate system.
186  Box* GetBoxForTBOX(const TBOX& tbox) const;
187 
188  // This method returns y-extents of the shiro-rekha computed from the input
189  // word image.
190  static void GetShiroRekhaYExtents(Pix* word_pix,
191  int* shirorekha_top,
192  int* shirorekha_bottom,
193  int* shirorekha_ylevel);
194 
195  Pix* orig_pix_; // Just a clone of the input image passed.
196  Pix* splitted_image_; // Image produced after the last splitting round. The
197  // object is owned by this class.
198  SplitStrategy pageseg_split_strategy_;
199  SplitStrategy ocr_split_strategy_;
200  Pix* debug_image_;
201  // This block list is used as a golden segmentation when performing splitting.
202  BLOCK_LIST* segmentation_block_list_;
203  int global_xheight_;
204  bool perform_close_; // Whether a morphological close operation should be
205  // performed before CCs are run through splitting.
206 };
207 
208 } // namespace tesseract.
209 
210 #endif // TESSERACT_TEXTORD_DEVNAGARI_PROCESSING_H_
#define INT_VAR_H(name, val, comment)
Definition: params.h:264
int devanagari_split_debuglevel
#define BOOL_VAR_H(name, val, comment)
Definition: params.h:267
int count(LIST var_list)
Definition: oldlist.cpp:98
void RefreshSegmentationWithNewBlobs(C_BLOB_LIST *new_blobs)
Definition: rect.h:34
SplitStrategy ocr_split_strategy() const
int GetHistogramMaximum(int *count) const
SplitStrategy pageseg_split_strategy() const
void set_pageseg_split_strategy(SplitStrategy strategy)
bool Split(bool split_for_pageseg, DebugPixa *pixa_debug)
void set_ocr_split_strategy(SplitStrategy strategy)
bool devanagari_split_debugimage
void set_segmentation_block_list(BLOCK_LIST *block_list)