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