tesseract  5.0.0-alpha-619-ge9db
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 //
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 <tesseract/genericvector.h> // for GenericVector
25 #include "tesseractclass.h" // for Tesseract
26 #include <tesseract/unichar.h> // for UNICHAR_ID
27 
28 class TBOX;
29 class UNICHARSET;
30 
31 namespace tesseract {
32 
33 class Tesseract;
34 class ColPartition;
35 class ColPartitionGrid;
36 class ColPartitionSet;
37 
39  public:
40  EquationDetect(const char* equ_datapath,
41  const char* equ_language);
42  ~EquationDetect() override;
43 
44  enum IndentType {
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.
64  int FindEquationParts(ColPartitionGrid* part_grid,
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_.
81  void IdentifySpecialText();
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.
90  void MergePartsByLocation();
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.
97  void SearchByOverlap(ColPartition* seed,
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.
108  bool CheckSeedBlobsCount(ColPartition* part);
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_
tesseract::EquationDetect::ExpandSeedHorizontal
void ExpandSeedHorizontal(const bool search_left, ColPartition *seed, GenericVector< ColPartition * > *parts_to_merge)
Definition: equationdetect.cpp:1127
tesseract::EquationDetect::EstimateTextPartLineSpacing
int EstimateTextPartLineSpacing()
Definition: equationdetect.cpp:867
tesseract::EquationDetect::IsInline
bool IsInline(const bool search_bottom, const int textPartsLineSpacing, ColPartition *part)
Definition: equationdetect.cpp:936
tesseract::EquationDetect::GetOutputTiffName
void GetOutputTiffName(const char *name, STRING *image_name) const
Definition: equationdetect.cpp:1456
tesseract::EquationDetect
Definition: equationdetect.h:38
tesseractclass.h
tesseract::EquationDetect::IdentifySeedParts
void IdentifySeedParts()
Definition: equationdetect.cpp:539
tesseract::EquationDetect::IdentifyInlineParts
void IdentifyInlineParts()
Definition: equationdetect.cpp:783
blobbox.h
tesseract::EquationDetect::CheckForSeed2
bool CheckForSeed2(const GenericVector< int > &indented_texts_left, const float foreground_density_th, ColPartition *part)
Definition: equationdetect.cpp:737
tesseract::Tesseract
Definition: tesseractclass.h:172
BlobSpecialTextType
BlobSpecialTextType
Definition: blobbox.h:95
tesseract::EquationDetect::IsNearMathNeighbor
bool IsNearMathNeighbor(const int y_gap, const ColPartition *neighbor) const
Definition: equationdetect.cpp:1447
tesseract::EquationDetect::resolution_
int resolution_
Definition: equationdetect.h:265
tesseract::EquationDetect::IdentifyInlinePartsVertical
void IdentifyInlinePartsVertical(const bool top_to_bottom, const int textPartsLineSpacing)
Definition: equationdetect.cpp:907
tesseract::EquationDetect::LEFT_INDENT
Definition: equationdetect.h:46
tesseract::EquationDetect::SetLangTesseract
void SetLangTesseract(Tesseract *lang_tesseract)
Definition: equationdetect.cpp:123
STRING
Definition: strngs.h:45
tesseract::EquationDetect::ExpandSeedVertical
void ExpandSeedVertical(const bool search_bottom, ColPartition *seed, GenericVector< ColPartition * > *parts_to_merge)
Definition: equationdetect.cpp:1183
TO_BLOCK
Definition: blobbox.h:691
tesseract::EquationDetect::SplitCPHor
void SplitCPHor(ColPartition *part, GenericVector< ColPartition * > *parts_splitted)
Definition: equationdetect.cpp:647
tesseract::ColPartitionSet
Definition: colpartitionset.h:39
tesseract::EquationDetect::INDENT_TYPE_COUNT
Definition: equationdetect.h:49
equationdetectbase.h
tesseract::EquationDetect::page_count_
int page_count_
Definition: equationdetect.h:268
BLOBNBOX
Definition: blobbox.h:142
tesseract::ColPartition
Definition: colpartition.h:67
tesseract::EquationDetect::EquationDetect
EquationDetect(const char *equ_datapath, const char *equ_language)
Definition: equationdetect.cpp:102
genericvector.h
tesseract::EquationDetect::IdentifyInlinePartsHorizontal
void IdentifyInlinePartsHorizontal()
Definition: equationdetect.cpp:802
tesseract::EquationDetect::NO_INDENT
Definition: equationdetect.h:45
tesseract::EquationDetect::EstimateTypeForUnichar
BlobSpecialTextType EstimateTypeForUnichar(const UNICHARSET &unicharset, const UNICHAR_ID id) const
Definition: equationdetect.cpp:224
tesseract::EquationDetect::IsNearSmallNeighbor
bool IsNearSmallNeighbor(const TBOX &seed_box, const TBOX &part_box) const
Definition: equationdetect.cpp:1270
tesseract::EquationDetect::SplitCPHorLite
void SplitCPHorLite(ColPartition *part, GenericVector< TBOX > *splitted_boxes)
Definition: equationdetect.cpp:697
tesseract::EquationDetect::CheckSeedFgDensity
bool CheckSeedFgDensity(const float density_th, ColPartition *part)
Definition: equationdetect.cpp:625
tesseract::EquationDetect::ExpandSeed
bool ExpandSeed(ColPartition *seed)
Definition: equationdetect.cpp:1084
tesseract::EquationDetect::IdentifySpecialText
void IdentifySpecialText()
Definition: equationdetect.cpp:258
tesseract::EquationDetect::CountAlignment
int CountAlignment(const GenericVector< int > &sorted_vec, const int val) const
Definition: equationdetect.cpp:759
UNICHARSET
Definition: unicharset.h:145
tesseract::EquationDetect::best_columns_
ColPartitionSet ** best_columns_
Definition: equationdetect.h:256
tesseract::EquationDetect::FindEquationParts
int FindEquationParts(ColPartitionGrid *part_grid, ColPartitionSet **best_columns) override
Definition: equationdetect.cpp:358
tesseract::EquationDetect::IsIndented
IndentType IsIndented(ColPartition *part)
Definition: equationdetect.cpp:1020
tesseract::EquationDetect::MergePartsByLocation
void MergePartsByLocation()
Definition: equationdetect.cpp:424
tesseract::EquationDetect::cps_super_bbox_
TBOX * cps_super_bbox_
Definition: equationdetect.h:259
tesseract::EquationDetect::SearchByOverlap
void SearchByOverlap(ColPartition *seed, GenericVector< ColPartition * > *parts_overlap)
Definition: equationdetect.cpp:463
tesseract::EquationDetect::RIGHT_INDENT
Definition: equationdetect.h:47
tesseract
Definition: baseapi.h:65
tesseract::EquationDetect::ComputeForegroundDensity
float ComputeForegroundDensity(const TBOX &tbox)
Definition: equationdetect.cpp:611
tesseract::EquationDetect::lang_tesseract_
Tesseract * lang_tesseract_
Definition: equationdetect.h:247
tesseract::EquationDetect::CheckSeedBlobsCount
bool CheckSeedBlobsCount(ColPartition *part)
Definition: equationdetect.cpp:983
tesseract::EquationDetect::IndentType
IndentType
Definition: equationdetect.h:44
tesseract::EquationDetect::PaintColParts
void PaintColParts(const STRING &outfile) const
Definition: equationdetect.cpp:1481
tesseract::EquationDetect::PaintSpecialTexts
void PaintSpecialTexts(const STRING &outfile) const
Definition: equationdetect.cpp:1464
UNICHAR_ID
int UNICHAR_ID
Definition: unichar.h:36
tesseract::EquationDetect::PrintSpecialBlobsDensity
void PrintSpecialBlobsDensity(const ColPartition *part) const
Definition: equationdetect.cpp:1504
tesseract::EquationDetect::ProcessMathBlockSatelliteParts
void ProcessMathBlockSatelliteParts()
Definition: equationdetect.cpp:1309
GenericVector
Definition: baseapi.h:40
tesseract::EquationDetect::ComputeCPsSuperBBox
void ComputeCPsSuperBBox()
Definition: equationdetect.cpp:791
tesseract::EquationDetect::LabelSpecialText
int LabelSpecialText(TO_BLOCK *to_block) override
Definition: equationdetect.cpp:131
tesseract::EquationDetectBase
Definition: equationdetectbase.h:32
tesseract::EquationDetect::part_grid_
ColPartitionGrid * part_grid_
Definition: equationdetect.h:251
tesseract::EquationDetect::equ_tesseract_
Tesseract equ_tesseract_
Definition: equationdetect.h:243
tesseract::EquationDetect::BOTH_INDENT
Definition: equationdetect.h:48
tesseract::EquationDetect::cp_seeds_
GenericVector< ColPartition * > cp_seeds_
Definition: equationdetect.h:262
unichar.h
tesseract::ColPartitionGrid
Definition: colpartitiongrid.h:32
tesseract::EquationDetect::SearchNNVertical
ColPartition * SearchNNVertical(const bool search_bottom, const ColPartition *part)
Definition: equationdetect.cpp:1412
tesseract::EquationDetect::InsertPartAfterAbsorb
void InsertPartAfterAbsorb(ColPartition *part)
Definition: equationdetect.cpp:512
tesseract::EquationDetect::~EquationDetect
~EquationDetect() override
Definition: equationdetect.cpp:121
tesseract::EquationDetect::SetResolution
void SetResolution(const int resolution)
Definition: equationdetect.cpp:127
tesseract::EquationDetect::IsMathBlockSatellite
bool IsMathBlockSatellite(ColPartition *part, GenericVector< ColPartition * > *math_blocks)
Definition: equationdetect.cpp:1358
tesseract::EquationDetect::CheckSeedDensity
bool CheckSeedDensity(const float math_density_high, const float math_density_low, const ColPartition *part) const
Definition: equationdetect.cpp:1001
tesseract::EquationDetect::CheckSeedNeighborDensity
bool CheckSeedNeighborDensity(const ColPartition *part) const
Definition: equationdetect.cpp:1292
tesseract::EquationDetect::IdentifyBlobsToSkip
void IdentifyBlobsToSkip(ColPartition *part)
Definition: equationdetect.cpp:310
TBOX
Definition: rect.h:33