tesseract  5.0.0
thresholder.h
Go to the documentation of this file.
1 // File: thresholder.h
3 // Description: Base API for thresholding images in tesseract.
4 // Author: Ray Smith
5 //
6 // (C) Copyright 2008, 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_THRESHOLDER_H_
20 #define TESSERACT_CCMAIN_THRESHOLDER_H_
21 
22 #include <tesseract/export.h>
23 
24 #include <vector> // for std::vector
25 
26 struct Pix;
27 
28 namespace tesseract {
29 
30 enum class ThresholdMethod {
31  Otsu, // Tesseract's legacy Otsu
32  LeptonicaOtsu, // Leptonica's Otsu
33  Sauvola, // Leptonica's Sauvola
34  Max, // Number of Thresholding methods
35 };
36 
37 class TessBaseAPI;
38 
46 public:
48  virtual ~ImageThresholder();
49 
51  virtual void Clear();
52 
54  bool IsEmpty() const;
55 
64  void SetImage(const unsigned char *imagedata, int width, int height, int bytes_per_pixel,
65  int bytes_per_line);
66 
69  void SetRectangle(int left, int top, int width, int height);
70 
75  virtual void GetImageSizes(int *left, int *top, int *width, int *height, int *imagewidth,
76  int *imageheight);
77 
79  bool IsColor() const {
80  return pix_channels_ >= 3;
81  }
82 
84  bool IsBinary() const {
85  return pix_channels_ == 0;
86  }
87 
88  int GetScaleFactor() const {
89  return scale_;
90  }
91 
92  // Set the resolution of the source image in pixels per inch.
93  // This should be called right after SetImage(), and will let us return
94  // appropriate font sizes for the text.
95  void SetSourceYResolution(int ppi) {
96  yres_ = ppi;
97  estimated_res_ = ppi;
98  }
99  int GetSourceYResolution() const {
100  return yres_;
101  }
102  int GetScaledYResolution() const {
103  return scale_ * yres_;
104  }
105  // Set the resolution of the source image in pixels per inch, as estimated
106  // by the thresholder from the text size found during thresholding.
107  // This value will be used to set internal size thresholds during recognition
108  // and will not influence the output "point size." The default value is
109  // the same as the source resolution. (yres_)
110  void SetEstimatedResolution(int ppi) {
111  estimated_res_ = ppi;
112  }
113  // Returns the estimated resolution, including any active scaling.
114  // This value will be used to set internal size thresholds during recognition.
116  return scale_ * estimated_res_;
117  }
118 
124  void SetImage(const Image pix);
125 
130  virtual bool ThresholdToPix(Image *pix);
131 
132  virtual std::tuple<bool, Image, Image, Image> Threshold(TessBaseAPI *api,
133  ThresholdMethod method);
134 
135  // Gets a pix that contains an 8 bit threshold value at each pixel. The
136  // returned pix may be an integer reduction of the binary image such that
137  // the scale factor may be inferred from the ratio of the sizes, even down
138  // to the extreme of a 1x1 pixel thresholds image.
139  // Ideally the 8 bit threshold should be the exact threshold used to generate
140  // the binary image in ThresholdToPix, but this is not a hard constraint.
141  // Returns nullptr if the input is binary. PixDestroy after use.
142  virtual Image GetPixRectThresholds();
143 
149  Image GetPixRect();
150 
151  // Get a clone/copy of the source image rectangle, reduced to greyscale,
152  // and at the same resolution as the output binary.
153  // The returned Pix must be pixDestroyed.
154  // Provided to the classifier to extract features from the greyscale image.
155  virtual Image GetPixRectGrey();
156 
157 protected:
158  // ----------------------------------------------------------------------
159  // Utility functions that may be useful components for other thresholders.
160 
162  virtual void Init();
163 
165  bool IsFullImage() const {
166  return rect_left_ == 0 && rect_top_ == 0 && rect_width_ == image_width_ &&
167  rect_height_ == image_height_;
168  }
169 
170  // Otsu thresholds the rectangle, taking the rectangle from *this.
171  void OtsuThresholdRectToPix(Image src_pix, Image *out_pix) const;
172 
176  // arrays and also the bytes per pixel in src_pix.
177  void ThresholdRectToPix(Image src_pix, int num_channels, const std::vector<int> &thresholds,
178  const std::vector <int> &hi_values, Image *pix) const;
179 
180 protected:
184 
188  int pix_wpl_;
189  // Limits of image rectangle to be processed.
190  int scale_;
191  int yres_;
197 };
198 
199 } // namespace tesseract.
200 
201 #endif // TESSERACT_CCMAIN_THRESHOLDER_H_
struct TessBaseAPI TessBaseAPI
Definition: capi.h:62
int GetScaledEstimatedResolution() const
Definition: thresholder.h:115
int pix_wpl_
Words per line of pix_.
Definition: thresholder.h:188
void SetEstimatedResolution(int ppi)
Definition: thresholder.h:110
int GetSourceYResolution() const
Definition: thresholder.h:99
bool IsFullImage() const
Return true if we are processing the full image.
Definition: thresholder.h:165
bool IsColor() const
Return true if the source image is color.
Definition: thresholder.h:79
int estimated_res_
Resolution estimate from text size.
Definition: thresholder.h:192
int GetScaledYResolution() const
Definition: thresholder.h:102
int scale_
Scale factor from original image.
Definition: thresholder.h:190
int pix_channels_
Number of 8-bit channels in pix_.
Definition: thresholder.h:187
int yres_
y pixels/inch in source image.
Definition: thresholder.h:191
int image_width_
Width of source pix_.
Definition: thresholder.h:185
bool IsBinary() const
Returns true if the source image is binary.
Definition: thresholder.h:84
int image_height_
Height of source pix_.
Definition: thresholder.h:186
void SetSourceYResolution(int ppi)
Definition: thresholder.h:95
#define TESS_API
Definition: export.h:34