/src/tesseract/src/textord/devanagari_processing.h
Line | Count | Source |
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 "image.h" // for Image |
17 | | #include "ocrblock.h" |
18 | | #include "params.h" |
19 | | |
20 | | namespace tesseract { |
21 | | |
22 | | extern INT_VAR_H(devanagari_split_debuglevel); |
23 | | |
24 | | extern BOOL_VAR_H(devanagari_split_debugimage); |
25 | | |
26 | | class TBOX; |
27 | | class DebugPixa; |
28 | | |
29 | | class PixelHistogram { |
30 | | public: |
31 | 0 | PixelHistogram() { |
32 | 0 | hist_ = nullptr; |
33 | 0 | length_ = 0; |
34 | 0 | } |
35 | | |
36 | 0 | ~PixelHistogram() { |
37 | 0 | Clear(); |
38 | 0 | } |
39 | | |
40 | 0 | void Clear() { |
41 | 0 | delete[] hist_; |
42 | 0 | length_ = 0; |
43 | 0 | } |
44 | | |
45 | 0 | int *hist() const { |
46 | 0 | return hist_; |
47 | 0 | } |
48 | | |
49 | 0 | int length() const { |
50 | 0 | return length_; |
51 | 0 | } |
52 | | |
53 | | // Methods to construct histograms from images. These clear any existing data. |
54 | | void ConstructVerticalCountHist(Image pix); |
55 | | void ConstructHorizontalCountHist(Image pix); |
56 | | |
57 | | // This method returns the global-maxima for the histogram. The frequency of |
58 | | // the global maxima is returned in count, if specified. |
59 | | int GetHistogramMaximum(int *count) const; |
60 | | |
61 | | private: |
62 | | int *hist_; |
63 | | int length_; |
64 | | }; |
65 | | |
66 | | class ShiroRekhaSplitter { |
67 | | public: |
68 | | enum SplitStrategy { |
69 | | NO_SPLIT = 0, // No splitting is performed for the phase. |
70 | | MINIMAL_SPLIT, // Blobs are split minimally. |
71 | | MAXIMAL_SPLIT // Blobs are split maximally. |
72 | | }; |
73 | | |
74 | | ShiroRekhaSplitter(); |
75 | | virtual ~ShiroRekhaSplitter(); |
76 | | |
77 | | // Top-level method to perform splitting based on current settings. |
78 | | // Returns true if a split was actually performed. |
79 | | // If split_for_pageseg is true, the pageseg_split_strategy_ is used for |
80 | | // splitting. If false, the ocr_split_strategy_ is used. |
81 | | bool Split(bool split_for_pageseg, DebugPixa *pixa_debug); |
82 | | |
83 | | // Clears the memory held by this object. |
84 | | void Clear(); |
85 | | |
86 | | // Refreshes the words in the segmentation block list by using blobs in the |
87 | | // input blob list. |
88 | | // The segmentation block list must be set. |
89 | | void RefreshSegmentationWithNewBlobs(C_BLOB_LIST *new_blobs); |
90 | | |
91 | | // Returns true if the split strategies for pageseg and ocr are different. |
92 | 14.9k | bool HasDifferentSplitStrategies() const { |
93 | 14.9k | return pageseg_split_strategy_ != ocr_split_strategy_; |
94 | 14.9k | } |
95 | | |
96 | | // This only keeps a copy of the block list pointer. At split call, the list |
97 | | // object should still be alive. This block list is used as a golden |
98 | | // segmentation when performing splitting. |
99 | 14.9k | void set_segmentation_block_list(BLOCK_LIST *block_list) { |
100 | 14.9k | segmentation_block_list_ = block_list; |
101 | 14.9k | } |
102 | | |
103 | | static const int kUnspecifiedXheight = -1; |
104 | | |
105 | 0 | void set_global_xheight(int xheight) { |
106 | 0 | global_xheight_ = xheight; |
107 | 0 | } |
108 | | |
109 | 0 | void set_perform_close(bool perform) { |
110 | 0 | perform_close_ = perform; |
111 | 0 | } |
112 | | |
113 | | // Returns the image obtained from shiro-rekha splitting. The returned object |
114 | | // is owned by this class. Callers may want to clone the returned pix to keep |
115 | | // it alive beyond the life of ShiroRekhaSplitter object. |
116 | 0 | Image splitted_image() { |
117 | 0 | return splitted_image_; |
118 | 0 | } |
119 | | |
120 | | // On setting the input image, a clone of it is owned by this class. |
121 | | void set_orig_pix(Image pix); |
122 | | |
123 | | // Returns the input image provided to the object. This object is owned by |
124 | | // this class. Callers may want to clone the returned pix to work with it. |
125 | 29.8k | Image orig_pix() { |
126 | 29.8k | return orig_pix_; |
127 | 29.8k | } |
128 | | |
129 | 0 | SplitStrategy ocr_split_strategy() const { |
130 | 0 | return ocr_split_strategy_; |
131 | 0 | } |
132 | | |
133 | 14.9k | void set_ocr_split_strategy(SplitStrategy strategy) { |
134 | 14.9k | ocr_split_strategy_ = strategy; |
135 | 14.9k | } |
136 | | |
137 | 0 | SplitStrategy pageseg_split_strategy() const { |
138 | 0 | return pageseg_split_strategy_; |
139 | 0 | } |
140 | | |
141 | 14.9k | void set_pageseg_split_strategy(SplitStrategy strategy) { |
142 | 14.9k | pageseg_split_strategy_ = strategy; |
143 | 14.9k | } |
144 | | |
145 | 0 | BLOCK_LIST *segmentation_block_list() { |
146 | 0 | return segmentation_block_list_; |
147 | 0 | } |
148 | | |
149 | | // This method returns the computed mode-height of blobs in the pix. |
150 | | // It also prunes very small blobs from calculation. Could be used to provide |
151 | | // a global xheight estimate for images which have the same point-size text. |
152 | | static int GetModeHeight(Image pix); |
153 | | |
154 | | private: |
155 | | // Method to perform a close operation on the input image. The xheight |
156 | | // estimate decides the size of sel used. |
157 | | static void PerformClose(Image pix, int xheight_estimate); |
158 | | |
159 | | // This method resolves the cc bbox to a particular row and returns the row's |
160 | | // xheight. This uses block_list_ if available, else just returns the |
161 | | // global_xheight_ estimate currently set in the object. |
162 | | int GetXheightForCC(Box *cc_bbox); |
163 | | |
164 | | // Returns a list of regions (boxes) which should be cleared in the original |
165 | | // image so as to perform shiro-rekha splitting. Pix is assumed to carry one |
166 | | // (or less) word only. Xheight measure could be the global estimate, the row |
167 | | // estimate, or unspecified. If unspecified, over splitting may occur, since a |
168 | | // conservative estimate of stroke width along with an associated multiplier |
169 | | // is used in its place. It is advisable to have a specified xheight when |
170 | | // splitting for classification/training. |
171 | | void SplitWordShiroRekha(SplitStrategy split_strategy, Image pix, int xheight, int word_left, |
172 | | int word_top, Boxa *regions_to_clear); |
173 | | |
174 | | // Returns a new box object for the corresponding TBOX, based on the original |
175 | | // image's coordinate system. |
176 | | Box *GetBoxForTBOX(const TBOX &tbox) const; |
177 | | |
178 | | // This method returns y-extents of the shiro-rekha computed from the input |
179 | | // word image. |
180 | | static void GetShiroRekhaYExtents(Image word_pix, int *shirorekha_top, int *shirorekha_bottom, |
181 | | int *shirorekha_ylevel); |
182 | | |
183 | | Image orig_pix_; // Just a clone of the input image passed. |
184 | | Image splitted_image_; // Image produced after the last splitting round. The |
185 | | // object is owned by this class. |
186 | | SplitStrategy pageseg_split_strategy_; |
187 | | SplitStrategy ocr_split_strategy_; |
188 | | Image debug_image_; |
189 | | // This block list is used as a golden segmentation when performing splitting. |
190 | | BLOCK_LIST *segmentation_block_list_; |
191 | | int global_xheight_; |
192 | | bool perform_close_; // Whether a morphological close operation should be |
193 | | // performed before CCs are run through splitting. |
194 | | }; |
195 | | |
196 | | } // namespace tesseract. |
197 | | |
198 | | #endif // TESSERACT_TEXTORD_DEVNAGARI_PROCESSING_H_ |