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