tesseract  v4.0.0-17-g361f3264
Open Source OCR Engine
thresholder.h
1 // File: thresholder.h
3 // Description: Base API for thresolding images in tesseract.
4 // Author: Ray Smith
5 // Created: Mon May 12 11:00:15 PDT 2008
6 //
7 // (C) Copyright 2008, 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_THRESHOLDER_H_
21 #define TESSERACT_CCMAIN_THRESHOLDER_H_
22 
23 #include "platform.h"
24 #include "publictypes.h"
25 
26 struct Pix;
27 
28 namespace tesseract {
29 
36 class TESS_API ImageThresholder {
37  public:
39  virtual ~ImageThresholder();
40 
42  virtual void Clear();
43 
45  bool IsEmpty() const;
46 
55  void SetImage(const unsigned char* imagedata, int width, int height,
56  int bytes_per_pixel, int bytes_per_line);
57 
60  void SetRectangle(int left, int top, int width, int height);
61 
66  virtual void GetImageSizes(int* left, int* top, int* width, int* height,
67  int* imagewidth, int* imageheight);
68 
70  bool IsColor() const {
71  return pix_channels_ >= 3;
72  }
73 
75  bool IsBinary() const {
76  return pix_channels_ == 0;
77  }
78 
79  int GetScaleFactor() const {
80  return scale_;
81  }
82 
83  // Set the resolution of the source image in pixels per inch.
84  // This should be called right after SetImage(), and will let us return
85  // appropriate font sizes for the text.
86  void SetSourceYResolution(int ppi) {
87  yres_ = ppi;
88  estimated_res_ = ppi;
89  }
90  int GetSourceYResolution() const {
91  return yres_;
92  }
93  int GetScaledYResolution() const {
94  return scale_ * yres_;
95  }
96  // Set the resolution of the source image in pixels per inch, as estimated
97  // by the thresholder from the text size found during thresholding.
98  // This value will be used to set internal size thresholds during recognition
99  // and will not influence the output "point size." The default value is
100  // the same as the source resolution. (yres_)
101  void SetEstimatedResolution(int ppi) {
102  estimated_res_ = ppi;
103  }
104  // Returns the estimated resolution, including any active scaling.
105  // This value will be used to set internal size thresholds during recognition.
107  return scale_ * estimated_res_;
108  }
109 
115  void SetImage(const Pix* pix);
116 
121  virtual bool ThresholdToPix(PageSegMode pageseg_mode, Pix** pix);
122 
123  // Gets a pix that contains an 8 bit threshold value at each pixel. The
124  // returned pix may be an integer reduction of the binary image such that
125  // the scale factor may be inferred from the ratio of the sizes, even down
126  // to the extreme of a 1x1 pixel thresholds image.
127  // Ideally the 8 bit threshold should be the exact threshold used to generate
128  // the binary image in ThresholdToPix, but this is not a hard constraint.
129  // Returns nullptr if the input is binary. PixDestroy after use.
130  virtual Pix* GetPixRectThresholds();
131 
137  Pix* GetPixRect();
138 
139  // Get a clone/copy of the source image rectangle, reduced to greyscale,
140  // and at the same resolution as the output binary.
141  // The returned Pix must be pixDestroyed.
142  // Provided to the classifier to extract features from the greyscale image.
143  virtual Pix* GetPixRectGrey();
144 
145  protected:
146  // ----------------------------------------------------------------------
147  // Utility functions that may be useful components for other thresholders.
148 
150  virtual void Init();
151 
153  bool IsFullImage() const {
154  return rect_left_ == 0 && rect_top_ == 0 &&
155  rect_width_ == image_width_ && rect_height_ == image_height_;
156  }
157 
158  // Otsu thresholds the rectangle, taking the rectangle from *this.
159  void OtsuThresholdRectToPix(Pix* src_pix, Pix** out_pix) const;
160 
164  // arrays and also the bytes per pixel in src_pix.
165  void ThresholdRectToPix(Pix* src_pix, int num_channels,
166  const int* thresholds, const int* hi_values,
167  Pix** pix) const;
168 
169  protected:
172  Pix* pix_;
173 
174  int image_width_; //< Width of source pix_.
175  int image_height_; //< Height of source pix_.
176  int pix_channels_; //< Number of 8-bit channels in pix_.
177  int pix_wpl_; //< Words per line of pix_.
178  // Limits of image rectangle to be processed.
179  int scale_; //< Scale factor from original image.
180  int yres_; //< y pixels/inch in source image.
181  int estimated_res_; //< Resolution estimate from text size.
186 };
187 
188 } // namespace tesseract.
189 
190 #endif // TESSERACT_CCMAIN_THRESHOLDER_H_
bool IsColor() const
Return true if the source image is color.
Definition: thresholder.h:70
bool IsBinary() const
Returns true if the source image is binary.
Definition: thresholder.h:75
void SetSourceYResolution(int ppi)
Definition: thresholder.h:86
int estimated_res_
Definition: thresholder.h:181
int GetScaledEstimatedResolution() const
Definition: thresholder.h:106
int image_width_
Definition: thresholder.h:174
int GetScaledYResolution() const
Definition: thresholder.h:93
int yres_
Definition: thresholder.h:180
int image_height_
Definition: thresholder.h:175
Definition: baseapi.cpp:94
int pix_wpl_
Definition: thresholder.h:177
int rect_top_
Definition: thresholder.h:183
int pix_channels_
Definition: thresholder.h:176
void SetEstimatedResolution(int ppi)
Definition: thresholder.h:101
int rect_height_
Definition: thresholder.h:185
int scale_
Definition: thresholder.h:179
int GetSourceYResolution() const
Definition: thresholder.h:90
bool IsFullImage() const
Return true if we are processing the full image.
Definition: thresholder.h:153
int rect_width_
Definition: thresholder.h:184
int GetScaleFactor() const
Definition: thresholder.h:79
int rect_left_
Definition: thresholder.h:182
Pix * pix_
Definition: thresholder.h:172
Definition: thresholder.h:36
PageSegMode
Definition: publictypes.h:163