tesseract  4.00.00dev
equationdetect.h
Go to the documentation of this file.
1 // File: equationdetect.h
3 // Description: The equation detection class that inherits equationdetectbase.
4 // Author: Zongyi (Joe) Liu (joeliu@google.com)
5 // Created: Fri Aug 31 11:13:01 PST 2011
6 //
7 // (C) Copyright 2011, 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 //
19 
20 #ifndef TESSERACT_CCMAIN_EQUATIONDETECT_H_
21 #define TESSERACT_CCMAIN_EQUATIONDETECT_H_
22 
23 #include "blobbox.h"
24 #include "equationdetectbase.h"
25 #include "genericvector.h"
26 #include "tesseractclass.h"
27 #include "unichar.h"
28 
29 class BLOBNBOX;
30 class BLOB_CHOICE;
31 class BLOB_CHOICE_LIST;
32 class TO_BLOCK_LIST;
33 class TBOX;
34 class UNICHARSET;
35 
36 namespace tesseract {
37 
38 class Tesseract;
39 class ColPartition;
40 class ColPartitionGrid;
41 class ColPartitionSet;
42 
44  public:
45  EquationDetect(const char* equ_datapath,
46  const char* equ_language);
48 
49  enum IndentType {
55  };
56 
57  // Reset the lang_tesseract_ pointer. This function should be called before we
58  // do any detector work.
59  void SetLangTesseract(Tesseract* lang_tesseract);
60 
61  // Iterate over the blobs inside to_block, and set the blobs that we want to
62  // process to BSTT_NONE. (By default, they should be BSTT_SKIP). The function
63  // returns 0 upon success.
64  int LabelSpecialText(TO_BLOCK* to_block);
65 
66  // Find possible equation partitions from part_grid. Should be called
67  // after the special_text_type of blobs are set.
68  // It returns 0 upon success.
69  int FindEquationParts(ColPartitionGrid* part_grid,
70  ColPartitionSet** best_columns);
71 
72  // Reset the resolution of the processing image. TEST only function.
73  void SetResolution(const int resolution);
74 
75  protected:
76  // Identify the special text type for one blob, and update its field. When
77  // height_th is set (> 0), we will label the blob as BSTT_NONE if its height
78  // is less than height_th.
79  void IdentifySpecialText(BLOBNBOX *blob, const int height_th);
80 
81  // Estimate the type for one unichar.
83  const UNICHARSET& unicharset, const UNICHAR_ID id) const;
84 
85  // Compute special text type for each blobs in part_grid_.
86  void IdentifySpecialText();
87 
88  // Identify blobs that we want to skip during special blob type
89  // classification.
91 
92  // The ColPartitions in part_grid_ maybe over-segmented, particularly in the
93  // block equation regions. So we like to identify these partitions and merge
94  // them before we do the searching.
95  void MergePartsByLocation();
96 
97  // Staring from the seed center, we do radius search. And for partitions that
98  // have large overlaps with seed, we remove them from part_grid_ and add into
99  // parts_overlap. Note: this function may update the part_grid_, so if the
100  // caller is also running ColPartitionGridSearch, use the RepositionIterator
101  // to continue.
102  void SearchByOverlap(ColPartition* seed,
103  GenericVector<ColPartition*>* parts_overlap);
104 
105  // Insert part back into part_grid_, after it absorbs some other parts.
107 
108  // Identify the colparitions in part_grid_, label them as PT_EQUATION, and
109  // save them into cp_seeds_.
110  void IdentifySeedParts();
111 
112  // Check the blobs count for a seed region candidate.
113  bool CheckSeedBlobsCount(ColPartition* part);
114 
115  // Compute the foreground pixel density for a tbox area.
116  float ComputeForegroundDensity(const TBOX& tbox);
117 
118  // Check if part from seed2 label: with low math density and left indented. We
119  // are using two checks:
120  // 1. If its left is aligned with any coordinates in indented_texts_left,
121  // which we assume have been sorted.
122  // 2. If its foreground density is over foreground_density_th.
123  bool CheckForSeed2(
124  const GenericVector<int>& indented_texts_left,
125  const float foreground_density_th,
126  ColPartition* part);
127 
128  // Count the number of values in sorted_vec that is close to val, used to
129  // check if a partition is aligned with text partitions.
130  int CountAlignment(
131  const GenericVector<int>& sorted_vec, const int val) const;
132 
133  // Check for a seed candidate using the foreground pixel density. And we
134  // return true if the density is below a certain threshold, because characters
135  // in equation regions usually are apart with more white spaces.
136  bool CheckSeedFgDensity(const float density_th, ColPartition* part);
137 
138  // A light version of SplitCPHor: instead of really doing the part split, we
139  // simply compute the union bounding box of each splitted part.
140  void SplitCPHorLite(ColPartition* part, GenericVector<TBOX>* splitted_boxes);
141 
142  // Split the part (horizontally), and save the splitted result into
143  // parts_splitted. Note that it is caller's responsibility to release the
144  // memory owns by parts_splitted. On the other hand, the part is unchanged
145  // during this process and still owns the blobs, so do NOT call DeleteBoxes
146  // when freeing the colpartitions in parts_splitted.
147  void SplitCPHor(ColPartition* part,
148  GenericVector<ColPartition*>* parts_splitted);
149 
150  // Check the density for a seed candidate (part) using its math density and
151  // italic density, returns true if the check passed.
152  bool CheckSeedDensity(const float math_density_high,
153  const float math_density_low,
154  const ColPartition* part) const;
155 
156  // Check if part is indented.
158 
159  // Identify inline partitions from cp_seeds_, and re-label them.
160  void IdentifyInlineParts();
161 
162  // Comute the super bounding box for all colpartitions inside part_grid_.
163  void ComputeCPsSuperBBox();
164 
165  // Identify inline partitions from cp_seeds_ using the horizontal search.
167 
168  // Estimate the line spacing between two text partitions. Returns -1 if not
169  // enough data.
171 
172  // Identify inline partitions from cp_seeds_ using vertical search.
173  void IdentifyInlinePartsVertical(const bool top_to_bottom,
174  const int textPartsLineSpacing);
175 
176  // Check if part is an inline equation zone. This should be called after we
177  // identified the seed regions.
178  bool IsInline(const bool search_bottom,
179  const int textPartsLineSpacing,
180  ColPartition* part);
181 
182  // For a given seed partition, we search the part_grid_ and see if there is
183  // any partition can be merged with it. It returns true if the seed has been
184  // expanded.
185  bool ExpandSeed(ColPartition* seed);
186 
187  // Starting from the seed position, we search the part_grid_
188  // horizontally/vertically, find all parititions that can be
189  // merged with seed, remove them from part_grid_, and put them into
190  // parts_to_merge.
191  void ExpandSeedHorizontal(const bool search_left,
192  ColPartition* seed,
193  GenericVector<ColPartition*>* parts_to_merge);
194  void ExpandSeedVertical(const bool search_bottom,
195  ColPartition* seed,
196  GenericVector<ColPartition*>* parts_to_merge);
197 
198  // Check if a part_box is the small neighbor of seed_box.
199  bool IsNearSmallNeighbor(const TBOX& seed_box,
200  const TBOX& part_box) const;
201 
202  // Perform the density check for part, which we assume is nearing a seed
203  // partition. It returns true if the check passed.
204  bool CheckSeedNeighborDensity(const ColPartition* part) const;
205 
206  // After identify the math blocks, we do one more scanning on all text
207  // partitions, and check if any of them is the satellite of:
208  // math blocks: here a p is the satellite of q if:
209  // 1. q is the nearest vertical neighbor of p, and
210  // 2. y_gap(p, q) is less than a threshold, and
211  // 3. x_overlap(p, q) is over a threshold.
212  // Note that p can be the satellites of two blocks: its top neighbor and
213  // bottom neighbor.
215 
216  // Check if part is the satellite of one/two math blocks. If it is, we return
217  // true, and save the blocks into math_blocks.
219  ColPartition* part, GenericVector<ColPartition*>* math_blocks);
220 
221  // Search the nearest neighbor of part in one vertical direction as defined in
222  // search_bottom. It returns the neighbor found that major x overlap with it,
223  // or NULL when not found.
224  ColPartition* SearchNNVertical(const bool search_bottom,
225  const ColPartition* part);
226 
227  // Check if the neighbor with vertical distance of y_gap is a near and math
228  // block partition.
229  bool IsNearMathNeighbor(const int y_gap, const ColPartition *neighbor) const;
230 
231  // Generate the tiff file name for output/debug file.
232  void GetOutputTiffName(const char* name, STRING* image_name) const;
233 
234  // Debugger function that renders ColPartitions on the input image, where:
235  // parts labeled as PT_EQUATION will be painted in red, PT_INLINE_EQUATION
236  // will be painted in green, and other parts will be painted in blue.
237  void PaintColParts(const STRING& outfile) const;
238 
239  // Debugger function that renders the blobs in part_grid_ over the input
240  // image.
241  void PaintSpecialTexts(const STRING& outfile) const;
242 
243  // Debugger function that print the math blobs density values for a
244  // ColPartition object.
245  void PrintSpecialBlobsDensity(const ColPartition* part) const;
246 
247  // The tesseract engine intialized from equation training data.
249 
250  // The tesseract engine used for OCR. This pointer is passed in by the caller,
251  // so do NOT destroy it in this class.
253 
254  // The ColPartitionGrid that we are processing. This pointer is passed in from
255  // the caller, so do NOT destroy it in the class.
257 
258  // A simple array of pointers to the best assigned column division at
259  // each grid y coordinate. This pointer is passed in from the caller, so do
260  // NOT destroy it in the class.
262 
263  // The super bounding box of all cps in the part_grid_.
265 
266  // The seed ColPartition for equation region.
268 
269  // The resolution (dpi) of the processing image.
271 
272  // The number of pages we have processed.
274 };
275 
276 } // namespace tesseract
277 
278 #endif // TESSERACT_CCMAIN_EQUATIONDETECT_H_
EquationDetect(const char *equ_datapath, const char *equ_language)
ColPartitionGrid * part_grid_
void GetOutputTiffName(const char *name, STRING *image_name) const
bool CheckSeedFgDensity(const float density_th, ColPartition *part)
void IdentifyBlobsToSkip(ColPartition *part)
bool IsMathBlockSatellite(ColPartition *part, GenericVector< ColPartition *> *math_blocks)
float ComputeForegroundDensity(const TBOX &tbox)
int UNICHAR_ID
Definition: unichar.h:33
GenericVector< ColPartition * > cp_seeds_
bool IsNearMathNeighbor(const int y_gap, const ColPartition *neighbor) const
BlobSpecialTextType EstimateTypeForUnichar(const UNICHARSET &unicharset, const UNICHAR_ID id) const
void SetResolution(const int resolution)
ColPartitionSet ** best_columns_
int CountAlignment(const GenericVector< int > &sorted_vec, const int val) const
bool IsNearSmallNeighbor(const TBOX &seed_box, const TBOX &part_box) const
void PaintColParts(const STRING &outfile) const
int LabelSpecialText(TO_BLOCK *to_block)
bool CheckSeedBlobsCount(ColPartition *part)
bool ExpandSeed(ColPartition *seed)
void PrintSpecialBlobsDensity(const ColPartition *part) const
void SplitCPHorLite(ColPartition *part, GenericVector< TBOX > *splitted_boxes)
void ExpandSeedHorizontal(const bool search_left, ColPartition *seed, GenericVector< ColPartition *> *parts_to_merge)
bool CheckSeedNeighborDensity(const ColPartition *part) const
void IdentifyInlinePartsVertical(const bool top_to_bottom, const int textPartsLineSpacing)
int FindEquationParts(ColPartitionGrid *part_grid, ColPartitionSet **best_columns)
IndentType IsIndented(ColPartition *part)
void SearchByOverlap(ColPartition *seed, GenericVector< ColPartition *> *parts_overlap)
Definition: strngs.h:45
bool IsInline(const bool search_bottom, const int textPartsLineSpacing, ColPartition *part)
void PaintSpecialTexts(const STRING &outfile) const
void SplitCPHor(ColPartition *part, GenericVector< ColPartition *> *parts_splitted)
BlobSpecialTextType
Definition: blobbox.h:81
Definition: rect.h:30
bool CheckSeedDensity(const float math_density_high, const float math_density_low, const ColPartition *part) const
ColPartition * SearchNNVertical(const bool search_bottom, const ColPartition *part)
void InsertPartAfterAbsorb(ColPartition *part)
void ExpandSeedVertical(const bool search_bottom, ColPartition *seed, GenericVector< ColPartition *> *parts_to_merge)
void SetLangTesseract(Tesseract *lang_tesseract)
bool CheckForSeed2(const GenericVector< int > &indented_texts_left, const float foreground_density_th, ColPartition *part)