Tesseract
3.02
|
00001 00002 // File: thresholder.h 00003 // Description: Base API for thresolding images in tesseract. 00004 // Author: Ray Smith 00005 // Created: Mon May 12 11:00:15 PDT 2008 00006 // 00007 // (C) Copyright 2008, Google Inc. 00008 // Licensed under the Apache License, Version 2.0 (the "License"); 00009 // you may not use this file except in compliance with the License. 00010 // You may obtain a copy of the License at 00011 // http://www.apache.org/licenses/LICENSE-2.0 00012 // Unless required by applicable law or agreed to in writing, software 00013 // distributed under the License is distributed on an "AS IS" BASIS, 00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 // See the License for the specific language governing permissions and 00016 // limitations under the License. 00017 // 00019 00020 #ifndef TESSERACT_CCMAIN_THRESHOLDER_H__ 00021 #define TESSERACT_CCMAIN_THRESHOLDER_H__ 00022 00023 class IMAGE; 00024 struct Pix; 00025 00026 namespace tesseract { 00027 00034 class ImageThresholder { 00035 public: 00036 ImageThresholder(); 00037 virtual ~ImageThresholder(); 00038 00040 virtual void Clear(); 00041 00043 bool IsEmpty() const; 00044 00055 void SetImage(const unsigned char* imagedata, int width, int height, 00056 int bytes_per_pixel, int bytes_per_line); 00057 00060 void SetRectangle(int left, int top, int width, int height); 00061 00066 virtual void GetImageSizes(int* left, int* top, int* width, int* height, 00067 int* imagewidth, int* imageheight); 00068 00070 bool IsColor() const { 00071 return image_bytespp_ >= 3; 00072 } 00073 00075 bool IsBinary() const { 00076 return image_bytespp_ == 0; 00077 } 00078 00079 int GetScaleFactor() const { 00080 return scale_; 00081 } 00082 00083 // Set the resolution of the source image in pixels per inch. 00084 // This should be called right after SetImage(), and will let us return 00085 // appropriate font sizes for the text. 00086 void SetSourceYResolution(int ppi) { 00087 yres_ = ppi; 00088 estimated_res_ = ppi; 00089 } 00090 int GetSourceYResolution() const { 00091 return yres_; 00092 } 00093 int GetScaledYResolution() const { 00094 return scale_ * yres_; 00095 } 00096 // Set the resolution of the source image in pixels per inch, as estimated 00097 // by the thresholder from the text size found during thresholding. 00098 // This value will be used to set internal size thresholds during recognition 00099 // and will not influence the output "point size." The default value is 00100 // the same as the source resolution. (yres_) 00101 void SetEstimatedResolution(int ppi) { 00102 estimated_res_ = ppi; 00103 } 00104 // Returns the estimated resolution, including any active scaling. 00105 // This value will be used to set internal size thresholds during recognition. 00106 int GetScaledEstimatedResolution() const { 00107 return scale_ * estimated_res_; 00108 } 00109 00118 void SetImage(const Pix* pix); 00119 00123 virtual void ThresholdToPix(Pix** pix); 00124 00130 Pix* GetPixRect(); 00131 00137 Pix* GetPixRectGrey(); 00138 00139 protected: 00140 // ---------------------------------------------------------------------- 00141 // Utility functions that may be useful components for other thresholders. 00142 00144 virtual void Init(); 00145 00147 bool IsFullImage() const { 00148 return rect_left_ == 0 && rect_top_ == 0 && 00149 rect_width_ == image_width_ && rect_height_ == image_height_; 00150 } 00151 00154 void OtsuThresholdRectToPix(const unsigned char* imagedata, 00155 int bytes_per_pixel, int bytes_per_line, 00156 Pix** pix) const; 00157 00160 void ThresholdRectToPix(const unsigned char* imagedata, 00161 int bytes_per_pixel, int bytes_per_line, 00162 const int* thresholds, const int* hi_values, 00163 Pix** pix) const; 00164 00166 void RawRectToPix(Pix** pix) const; 00167 00168 protected: 00171 Pix* pix_; 00173 const unsigned char* image_data_; //< Raw source image. 00174 00175 int image_width_; //< Width of source image/pix. 00176 int image_height_; //< Height of source image/pix. 00177 int image_bytespp_; //< Bytes per pixel of source image/pix. 00178 int image_bytespl_; //< Bytes per line of source image/pix. 00179 // Limits of image rectangle to be processed. 00180 int scale_; //< Scale factor from original image. 00181 int yres_; //< y pixels/inch in source image. 00182 int estimated_res_; //< Resolution estimate from text size. 00183 int rect_left_; 00184 int rect_top_; 00185 int rect_width_; 00186 int rect_height_; 00187 }; 00188 00189 } // namespace tesseract. 00190 00191 #endif // TESSERACT_CCMAIN_THRESHOLDER_H__