Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tesseract::ImageThresholder Class Reference

#include <thresholder.h>

Public Member Functions

 ImageThresholder ()
 
virtual ~ImageThresholder ()
 
virtual void Clear ()
 Destroy the Pix if there is one, freeing memory. More...
 
bool IsEmpty () const
 Return true if no image has been set. More...
 
void SetImage (const unsigned char *imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line)
 
void SetRectangle (int left, int top, int width, int height)
 
virtual void GetImageSizes (int *left, int *top, int *width, int *height, int *imagewidth, int *imageheight)
 
bool IsColor () const
 Return true if the source image is color. More...
 
bool IsBinary () const
 Returns true if the source image is binary. More...
 
int GetScaleFactor () const
 
void SetSourceYResolution (int ppi)
 
int GetSourceYResolution () const
 
int GetScaledYResolution () const
 
void SetEstimatedResolution (int ppi)
 
int GetScaledEstimatedResolution () const
 
void SetImage (const Pix *pix)
 
virtual void ThresholdToPix (Pix **pix)
 
Pix * GetPixRect ()
 
Pix * GetPixRectGrey ()
 

Protected Member Functions

virtual void Init ()
 Common initialization shared between SetImage methods. More...
 
bool IsFullImage () const
 Return true if we are processing the full image. More...
 
void OtsuThresholdRectToPix (const unsigned char *imagedata, int bytes_per_pixel, int bytes_per_line, Pix **pix) const
 
void ThresholdRectToPix (const unsigned char *imagedata, int bytes_per_pixel, int bytes_per_line, const int *thresholds, const int *hi_values, Pix **pix) const
 
void RawRectToPix (Pix **pix) const
 Copy the raw image rectangle, taking all data from the class, to the Pix. More...
 

Protected Attributes

Pix * pix_
 
const unsigned char * image_data_
 Exactly one of pix_ and image_data_ is not NULL. More...
 
int image_width_
 
int image_height_
 
int image_bytespp_
 
int image_bytespl_
 
int scale_
 
int yres_
 
int estimated_res_
 
int rect_left_
 
int rect_top_
 
int rect_width_
 
int rect_height_
 

Detailed Description

Base class for all tesseract image thresholding classes. Specific classes can add new thresholding methods by overriding ThresholdToPix. Each instance deals with a single image, but the design is intended to be useful for multiple calls to SetRectangle and ThresholdTo* if desired.

Definition at line 34 of file thresholder.h.

Constructor & Destructor Documentation

tesseract::ImageThresholder::ImageThresholder ( )

Definition at line 31 of file thresholder.cpp.

32  : pix_(NULL),
36  scale_(1), yres_(300), estimated_res_(300) {
37  SetRectangle(0, 0, 0, 0);
38 }
const unsigned char * image_data_
Exactly one of pix_ and image_data_ is not NULL.
Definition: thresholder.h:173
#define NULL
Definition: host.h:144
void SetRectangle(int left, int top, int width, int height)
Definition: thresholder.cpp:88
tesseract::ImageThresholder::~ImageThresholder ( )
virtual

Definition at line 40 of file thresholder.cpp.

40  {
41  Clear();
42 }
virtual void Clear()
Destroy the Pix if there is one, freeing memory.
Definition: thresholder.cpp:45

Member Function Documentation

void tesseract::ImageThresholder::Clear ( )
virtual

Destroy the Pix if there is one, freeing memory.

Definition at line 45 of file thresholder.cpp.

45  {
46  if (pix_ != NULL) {
47  pixDestroy(&pix_);
48  pix_ = NULL;
49  }
50  image_data_ = NULL;
51 }
const unsigned char * image_data_
Exactly one of pix_ and image_data_ is not NULL.
Definition: thresholder.h:173
#define NULL
Definition: host.h:144
void tesseract::ImageThresholder::GetImageSizes ( int *  left,
int *  top,
int *  width,
int *  height,
int *  imagewidth,
int *  imageheight 
)
virtual

Get enough parameters to be able to rebuild bounding boxes in the original image (not just within the rectangle). Left and top are enough with top-down coordinates, but the height of the rectangle and the image are needed for bottom-up.

Definition at line 99 of file thresholder.cpp.

101  {
102  *left = rect_left_;
103  *top = rect_top_;
104  *width = rect_width_;
105  *height = rect_height_;
106  *imagewidth = image_width_;
107  *imageheight = image_height_;
108 }
Pix * tesseract::ImageThresholder::GetPixRect ( )

Get a clone/copy of the source image rectangle. The returned Pix must be pixDestroyed. This function will be used in the future by the page layout analysis, and the layout analysis that uses it will only be available with Leptonica, so there is no raw equivalent.

Definition at line 182 of file thresholder.cpp.

182  {
183  if (pix_ != NULL) {
184  if (IsFullImage()) {
185  // Just clone the whole thing.
186  return pixClone(pix_);
187  } else {
188  // Crop to the given rectangle.
189  Box* box = boxCreate(rect_left_, rect_top_, rect_width_, rect_height_);
190  Pix* cropped = pixClipRectangle(pix_, box, NULL);
191  boxDestroy(&box);
192  return cropped;
193  }
194  }
195  // The input is raw, so we have to make a copy of it.
196  Pix* raw_pix;
197  RawRectToPix(&raw_pix);
198  return raw_pix;
199 }
#define NULL
Definition: host.h:144
bool IsFullImage() const
Return true if we are processing the full image.
Definition: thresholder.h:147
void RawRectToPix(Pix **pix) const
Copy the raw image rectangle, taking all data from the class, to the Pix.
Pix * tesseract::ImageThresholder::GetPixRectGrey ( )

Get a clone/copy of the source image rectangle, reduced to greyscale. The returned Pix must be pixDestroyed. This function will be used in the future by the page layout analysis, and the layout analysis that uses it will only be available with Leptonica, so there is no raw equivalent.

Definition at line 206 of file thresholder.cpp.

206  {
207  Pix* pix = GetPixRect(); // May have to be reduced to grey.
208  int depth = pixGetDepth(pix);
209  if (depth != 8) {
210  Pix* result = depth < 8 ? pixConvertTo8(pix, false)
211  : pixConvertRGBToLuminance(pix);
212  pixDestroy(&pix);
213  return result;
214  }
215  return pix;
216 }
int tesseract::ImageThresholder::GetScaledEstimatedResolution ( ) const
inline

Definition at line 106 of file thresholder.h.

106  {
107  return scale_ * estimated_res_;
108  }
int tesseract::ImageThresholder::GetScaledYResolution ( ) const
inline

Definition at line 93 of file thresholder.h.

93  {
94  return scale_ * yres_;
95  }
int tesseract::ImageThresholder::GetScaleFactor ( ) const
inline

Definition at line 79 of file thresholder.h.

79  {
80  return scale_;
81  }
int tesseract::ImageThresholder::GetSourceYResolution ( ) const
inline

Definition at line 90 of file thresholder.h.

90  {
91  return yres_;
92  }
void tesseract::ImageThresholder::Init ( )
protectedvirtual

Common initialization shared between SetImage methods.

Definition at line 173 of file thresholder.cpp.

173  {
175 }
void SetRectangle(int left, int top, int width, int height)
Definition: thresholder.cpp:88
bool tesseract::ImageThresholder::IsBinary ( ) const
inline

Returns true if the source image is binary.

Definition at line 75 of file thresholder.h.

75  {
76  return image_bytespp_ == 0;
77  }
bool tesseract::ImageThresholder::IsColor ( ) const
inline

Return true if the source image is color.

Definition at line 70 of file thresholder.h.

70  {
71  return image_bytespp_ >= 3;
72  }
bool tesseract::ImageThresholder::IsEmpty ( ) const

Return true if no image has been set.

Definition at line 54 of file thresholder.cpp.

54  {
55  if (pix_ != NULL)
56  return false;
57  return image_data_ == NULL;
58 }
const unsigned char * image_data_
Exactly one of pix_ and image_data_ is not NULL.
Definition: thresholder.h:173
#define NULL
Definition: host.h:144
bool tesseract::ImageThresholder::IsFullImage ( ) const
inlineprotected

Return true if we are processing the full image.

Definition at line 147 of file thresholder.h.

void tesseract::ImageThresholder::OtsuThresholdRectToPix ( const unsigned char *  imagedata,
int  bytes_per_pixel,
int  bytes_per_line,
Pix **  pix 
) const
protected

Otsu threshold the rectangle, taking everything except the image buffer pointer from the class, to the output Pix.

Definition at line 220 of file thresholder.cpp.

223  {
224  int* thresholds;
225  int* hi_values;
226  OtsuThreshold(imagedata, bytes_per_pixel, bytes_per_line,
228  &thresholds, &hi_values);
229 
230  // Threshold the image to the given IMAGE.
231  ThresholdRectToPix(imagedata, bytes_per_pixel, bytes_per_line,
232  thresholds, hi_values, pix);
233  delete [] thresholds;
234  delete [] hi_values;
235 }
void ThresholdRectToPix(const unsigned char *imagedata, int bytes_per_pixel, int bytes_per_line, const int *thresholds, const int *hi_values, Pix **pix) const
void OtsuThreshold(const unsigned char *imagedata, int bytes_per_pixel, int bytes_per_line, int left, int top, int width, int height, int **thresholds, int **hi_values)
Definition: otsuthr.cpp:32
void tesseract::ImageThresholder::RawRectToPix ( Pix **  pix) const
protected

Copy the raw image rectangle, taking all data from the class, to the Pix.

Definition at line 272 of file thresholder.cpp.

272  {
273  if (image_bytespp_ < 4) {
274  // Go via a tesseract image structure (doesn't copy the data)
275  // and use ToPix.
276  IMAGE image;
277  int bits_per_pixel = image_bytespp_ * 8;
278  if (image_bytespp_ == 0)
279  bits_per_pixel = 1;
280  image.capture(const_cast<uinT8*>(image_data_),
281  image_width_, rect_top_ + rect_height_, bits_per_pixel);
282  if (IsFullImage()) {
283  *pix = image.ToPix();
284  } else {
285  IMAGE rect;
286  rect.create(rect_width_, rect_height_, bits_per_pixel);
287  // The capture chopped the image off at top+height, so copy
288  // the rectangle with y = 0 to get a rectangle of height
289  // starting at the bottom, since copy_sub_image uses bottom-up coords.
291  &rect, 0, 0, true);
292  *pix = rect.ToPix();
293  }
294  } else {
295  *pix = pixCreate(rect_width_, rect_height_, 32);
296  uinT32* data = pixGetData(*pix);
297  int wpl = pixGetWpl(*pix);
298  const uinT8* imagedata = image_data_ + rect_top_ * image_bytespl_ +
300  for (int y = 0; y < rect_height_; ++y) {
301  const uinT8* linedata = imagedata;
302  uinT32* line = data + y * wpl;
303  for (int x = 0; x < rect_width_; ++x) {
304  line[x] = (linedata[0] << 24) | (linedata[1] << 16) |
305  (linedata[2] << 8) | linedata[3];
306  linedata += 4;
307  }
308  imagedata += image_bytespl_;
309  }
310  }
311 }
const unsigned char * image_data_
Exactly one of pix_ and image_data_ is not NULL.
Definition: thresholder.h:173
inT8 capture(uinT8 *pixels, inT32 x, inT32 y, inT8 bits_per_pixel)
Definition: imgs.cpp:170
DLLSYM void copy_sub_image(IMAGE *source, inT32 xstart, inT32 ystart, inT32 xext, inT32 yext, IMAGE *dest, inT32 xdest, inT32 ydest, BOOL8 adjust_grey)
Definition: imgs.cpp:270
bool IsFullImage() const
Return true if we are processing the full image.
Definition: thresholder.h:147
unsigned char uinT8
Definition: host.h:99
Definition: img.h:51
unsigned int uinT32
Definition: host.h:103
inT8 create(inT32 x, inT32 y, inT8 bits_per_pixel)
Definition: imgs.cpp:121
Pix * ToPix()
Definition: imgs.cpp:1501
void tesseract::ImageThresholder::SetEstimatedResolution ( int  ppi)
inline

Definition at line 101 of file thresholder.h.

101  {
102  estimated_res_ = ppi;
103  }
void tesseract::ImageThresholder::SetImage ( const unsigned char *  imagedata,
int  width,
int  height,
int  bytes_per_pixel,
int  bytes_per_line 
)

SetImage makes a copy of only the metadata, not the underlying image buffer. It promises to treat the source as read-only in either case, but in return assumes that the Pix or image buffer remain valid throughout the life of the ImageThresholder. Greyscale of 8 and color of 24 or 32 bits per pixel may be given. Palette color images will not work properly and must be converted to 24 bit. Binary images of 1 bit per pixel may also be given but they must be byte packed with the MSB of the first byte being the first pixel, and a one pixel is WHITE. For binary images set bytes_per_pixel=0.

Definition at line 70 of file thresholder.cpp.

72  {
73  if (pix_ != NULL)
74  pixDestroy(&pix_);
75  pix_ = NULL;
76  image_data_ = imagedata;
77  image_width_ = width;
78  image_height_ = height;
79  image_bytespp_ = bytes_per_pixel;
80  image_bytespl_ = bytes_per_line;
81  scale_ = 1;
82  estimated_res_ = yres_ = 300;
83  Init();
84 }
const unsigned char * image_data_
Exactly one of pix_ and image_data_ is not NULL.
Definition: thresholder.h:173
#define NULL
Definition: host.h:144
virtual void Init()
Common initialization shared between SetImage methods.
void tesseract::ImageThresholder::SetImage ( const Pix *  pix)

Pix vs raw, which to use? Implementations should provide the ability to source and target Pix where possible. A future version of Tesseract may choose to use Pix as its internal representation and discard IMAGE altogether. Because of that, an implementation that sources and targets Pix may end up with less copies than an implementation that does not. NOTE: Opposite to SetImage for raw images, SetImage for Pix clones its input, so the source pix may be pixDestroyed immediately after.

Definition at line 112 of file thresholder.cpp.

112  {
113  image_data_ = NULL;
114  if (pix_ != NULL)
115  pixDestroy(&pix_);
116  Pix* src = const_cast<Pix*>(pix);
117  int depth;
118  pixGetDimensions(src, &image_width_, &image_height_, &depth);
119  // Convert the image as necessary so it is one of binary, plain RGB, or
120  // 8 bit with no colormap.
121  if (depth > 1 && depth < 8) {
122  pix_ = pixConvertTo8(src, false);
123  } else if (pixGetColormap(src)) {
124  pix_ = pixRemoveColormap(src, REMOVE_CMAP_BASED_ON_SRC);
125  } else {
126  pix_ = pixClone(src);
127  }
128  depth = pixGetDepth(pix_);
129  image_bytespp_ = depth / 8;
130  image_bytespl_ = pixGetWpl(pix_) * sizeof(l_uint32);
131  scale_ = 1;
132  estimated_res_ = yres_ = pixGetYRes(src);
133  Init();
134 }
const unsigned char * image_data_
Exactly one of pix_ and image_data_ is not NULL.
Definition: thresholder.h:173
#define NULL
Definition: host.h:144
virtual void Init()
Common initialization shared between SetImage methods.
void tesseract::ImageThresholder::SetRectangle ( int  left,
int  top,
int  width,
int  height 
)

Store the coordinates of the rectangle to process for later use. Doesn't actually do any thresholding.

Definition at line 88 of file thresholder.cpp.

88  {
89  rect_left_ = left;
90  rect_top_ = top;
91  rect_width_ = width;
92  rect_height_ = height;
93 }
void tesseract::ImageThresholder::SetSourceYResolution ( int  ppi)
inline

Definition at line 86 of file thresholder.h.

86  {
87  yres_ = ppi;
88  estimated_res_ = ppi;
89  }
void tesseract::ImageThresholder::ThresholdRectToPix ( const unsigned char *  imagedata,
int  bytes_per_pixel,
int  bytes_per_line,
const int *  thresholds,
const int *  hi_values,
Pix **  pix 
) const
protected

Threshold the rectangle, taking everything except the image buffer pointer from the class, using thresholds/hi_values to the output IMAGE.

Definition at line 239 of file thresholder.cpp.

244  {
245  *pix = pixCreate(rect_width_, rect_height_, 1);
246  uinT32* pixdata = pixGetData(*pix);
247  int wpl = pixGetWpl(*pix);
248  const unsigned char* srcdata = imagedata + rect_top_* bytes_per_line +
249  rect_left_ * bytes_per_pixel;
250  for (int y = 0; y < rect_height_; ++y) {
251  const uinT8* linedata = srcdata;
252  uinT32* pixline = pixdata + y * wpl;
253  for (int x = 0; x < rect_width_; ++x, linedata += bytes_per_pixel) {
254  bool white_result = true;
255  for (int ch = 0; ch < bytes_per_pixel; ++ch) {
256  if (hi_values[ch] >= 0 &&
257  (linedata[ch] > thresholds[ch]) == (hi_values[ch] == 0)) {
258  white_result = false;
259  break;
260  }
261  }
262  if (white_result)
263  CLEAR_DATA_BIT(pixline, x);
264  else
265  SET_DATA_BIT(pixline, x);
266  }
267  srcdata += bytes_per_line;
268  }
269 }
unsigned char uinT8
Definition: host.h:99
unsigned int uinT32
Definition: host.h:103
void tesseract::ImageThresholder::ThresholdToPix ( Pix **  pix)
virtual

Threshold the source image as efficiently as possible to the output Pix. Creates a Pix and sets pix to point to the resulting pointer. Caller must use pixDestroy to free the created Pix.

Definition at line 139 of file thresholder.cpp.

139  {
140  if (pix_ != NULL) {
141  if (image_bytespp_ == 0) {
142  // We have a binary image, so it just has to be cloned.
143  *pix = GetPixRect();
144  } else {
145  if (image_bytespp_ == 4) {
146  // Color data can just be passed direct.
147  const uinT32* data = pixGetData(pix_);
148  OtsuThresholdRectToPix(reinterpret_cast<const uinT8*>(data),
150  } else {
151  // Convert 8-bit to IMAGE and then pass its
152  // buffer to the raw interface to complete the conversion.
153  IMAGE temp_image;
154  temp_image.FromPix(pix_);
155  OtsuThresholdRectToPix(temp_image.get_buffer(),
157  COMPUTE_IMAGE_XDIM(temp_image.get_xsize(),
158  temp_image.get_bpp()),
159  pix);
160  }
161  }
162  return;
163  }
164  if (image_bytespp_ > 0) {
165  // Threshold grey or color.
167  } else {
168  RawRectToPix(pix);
169  }
170 }
uinT8 * get_buffer()
Definition: img.h:132
void OtsuThresholdRectToPix(const unsigned char *imagedata, int bytes_per_pixel, int bytes_per_line, Pix **pix) const
const unsigned char * image_data_
Exactly one of pix_ and image_data_ is not NULL.
Definition: thresholder.h:173
#define NULL
Definition: host.h:144
void FromPix(const Pix *src_pix)
Definition: imgs.cpp:1561
#define COMPUTE_IMAGE_XDIM(xsize, bpp)
Definition: img.h:33
inT8 get_bpp()
Definition: img.h:106
inT32 get_xsize()
Definition: img.h:92
Definition: img.h:51
unsigned int uinT32
Definition: host.h:103
void RawRectToPix(Pix **pix) const
Copy the raw image rectangle, taking all data from the class, to the Pix.

Member Data Documentation

int tesseract::ImageThresholder::estimated_res_
protected

Definition at line 182 of file thresholder.h.

int tesseract::ImageThresholder::image_bytespl_
protected

Definition at line 178 of file thresholder.h.

int tesseract::ImageThresholder::image_bytespp_
protected

Definition at line 177 of file thresholder.h.

const unsigned char* tesseract::ImageThresholder::image_data_
protected

Exactly one of pix_ and image_data_ is not NULL.

Definition at line 173 of file thresholder.h.

int tesseract::ImageThresholder::image_height_
protected

Definition at line 176 of file thresholder.h.

int tesseract::ImageThresholder::image_width_
protected

Definition at line 175 of file thresholder.h.

Pix* tesseract::ImageThresholder::pix_
protected

Clone or other copy of the source Pix. The pix will always be PixDestroy()ed on destruction of the class.

Definition at line 171 of file thresholder.h.

int tesseract::ImageThresholder::rect_height_
protected

Definition at line 186 of file thresholder.h.

int tesseract::ImageThresholder::rect_left_
protected

Definition at line 183 of file thresholder.h.

int tesseract::ImageThresholder::rect_top_
protected

Definition at line 184 of file thresholder.h.

int tesseract::ImageThresholder::rect_width_
protected

Definition at line 185 of file thresholder.h.

int tesseract::ImageThresholder::scale_
protected

Definition at line 180 of file thresholder.h.

int tesseract::ImageThresholder::yres_
protected

Definition at line 181 of file thresholder.h.


The documentation for this class was generated from the following files: