Tesseract 3.02

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- Class List
- Class Hierarchy
- Class Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Protected Member Functions | Protected Attributes
tesseract::ImageThresholder Class Reference

`#include <thresholder.h>`

List of all members.

## Public Member Functions

| ImageThresholder ()
virtual | ~ImageThresholder ()
virtual void | Clear ()
| Destroy the Pix if there is one, freeing memory.
bool | IsEmpty () const
| Return true if no image has been set.
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.
bool | IsBinary () const
| Returns true if the source image is binary.
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.
bool | IsFullImage () const
| Return true if we are processing the full image.
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.

## Protected Attributes

Pix * | pix_
const unsigned char * | image_data_
| Exactly one of pix_ and image_data_ is not NULL.
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.

: pix_(NULL),
image_data_(NULL),
image_width_(0), image_height_(0),
image_bytespp_(0), image_bytespl_(0),
scale_(1), yres_(300), estimated_res_(300) {
SetRectangle(0, 0, 0, 0);
}

tesseract::ImageThresholder::~ImageThresholder | ( |  | ) |

virtual

Definition at line 40 of file thresholder.cpp.

{
Clear();
}

## 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.

{
if (pix_ != NULL) {
pixDestroy(&pix_);
pix_ = NULL;
}
image_data_ = NULL;
}

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.

{
*left = rect_left_;
*top = rect_top_;
*width = rect_width_;
*height = rect_height_;
*imagewidth = image_width_;
*imageheight = image_height_;
}

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.

{
if (pix_ != NULL) {
if (IsFullImage()) {
// Just clone the whole thing.
return pixClone(pix_);
} else {
// Crop to the given rectangle.
Box* box = boxCreate(rect_left_, rect_top_, rect_width_, rect_height_);
Pix* cropped = pixClipRectangle(pix_, box, NULL);
boxDestroy(&box);
return cropped;
}
}
// The input is raw, so we have to make a copy of it.
Pix* raw_pix;
RawRectToPix(&raw_pix);
return raw_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.

{
Pix* pix = GetPixRect(); // May have to be reduced to grey.
int depth = pixGetDepth(pix);
if (depth != 8) {
Pix* result = depth < 8 ? pixConvertTo8(pix, false)
: pixConvertRGBToLuminance(pix);
pixDestroy(&pix);
return result;
}
return pix;
}

int tesseract::ImageThresholder::GetScaledEstimatedResolution | ( |  | ) | const

inline

Definition at line 106 of file thresholder.h.

{
return scale_ * estimated_res_;
}

int tesseract::ImageThresholder::GetScaledYResolution | ( |  | ) | const

inline

Definition at line 93 of file thresholder.h.

{
return scale_ * yres_;
}

int tesseract::ImageThresholder::GetScaleFactor | ( |  | ) | const

inline

Definition at line 79 of file thresholder.h.

{
return scale_;
}

int tesseract::ImageThresholder::GetSourceYResolution | ( |  | ) | const

inline

Definition at line 90 of file thresholder.h.

{
return yres_;
}

void tesseract::ImageThresholder::Init | ( |  | ) |

protected virtual

Common initialization shared between SetImage methods.

Definition at line 173 of file thresholder.cpp.

{
SetRectangle(0, 0, image_width_, image_height_);
}

bool tesseract::ImageThresholder::IsBinary | ( |  | ) | const

inline

Returns true if the source image is binary.

Definition at line 75 of file thresholder.h.

{
return image_bytespp_ == 0;
}

bool tesseract::ImageThresholder::IsColor | ( |  | ) | const

inline

Return true if the source image is color.

Definition at line 70 of file thresholder.h.

{
return image_bytespp_ >= 3;
}

bool tesseract::ImageThresholder::IsEmpty | ( |  | ) | const

Return true if no image has been set.

Definition at line 54 of file thresholder.cpp.

{
if (pix_ != NULL)
return false;
return image_data_ == NULL;
}

bool tesseract::ImageThresholder::IsFullImage | ( |  | ) | const

inline protected

Return true if we are processing the full image.

Definition at line 147 of file thresholder.h.

{
return rect_left_ == 0 && rect_top_ == 0 &&
rect_width_ == image_width_ && rect_height_ == image_height_;
}

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.

{
int* thresholds;
int* hi_values;
OtsuThreshold(imagedata, bytes_per_pixel, bytes_per_line,
rect_left_, rect_top_, rect_width_, rect_height_,
&thresholds, &hi_values);
// Threshold the image to the given IMAGE.
ThresholdRectToPix(imagedata, bytes_per_pixel, bytes_per_line,
thresholds, hi_values, pix);
delete [] thresholds;
delete [] hi_values;
}

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.

{
if (image_bytespp_ < 4) {
// Go via a tesseract image structure (doesn't copy the data)
// and use ToPix.
IMAGE image;
int bits_per_pixel = image_bytespp_ * 8;
if (image_bytespp_ == 0)
bits_per_pixel = 1;
image.capture(const_cast<uinT8*>(image_data_),
image_width_, rect_top_ + rect_height_, bits_per_pixel);
if (IsFullImage()) {
*pix = image.ToPix();
} else {
IMAGE rect;
rect.create(rect_width_, rect_height_, bits_per_pixel);
// The capture chopped the image off at top+height, so copy
// the rectangle with y = 0 to get a rectangle of height
// starting at the bottom, since copy_sub_image uses bottom-up coords.
copy_sub_image(&image, rect_left_, 0, rect_width_, rect_height_,
&rect, 0, 0, true);
*pix = rect.ToPix();
}
} else {
*pix = pixCreate(rect_width_, rect_height_, 32);
uinT32* data = pixGetData(*pix);
int wpl = pixGetWpl(*pix);
const uinT8* imagedata = image_data_ + rect_top_ * image_bytespl_ +
rect_left_ * image_bytespp_;
for (int y = 0; y < rect_height_; ++y) {
const uinT8* linedata = imagedata;
uinT32* line = data + y * wpl;
for (int x = 0; x < rect_width_; ++x) {
line[x] = (linedata[0] << 24) | (linedata[1] << 16) |
(linedata[2] << 8) | linedata[3];
linedata += 4;
}
imagedata += image_bytespl_;
}
}
}

void tesseract::ImageThresholder::SetEstimatedResolution | ( | int | ppi | ) |

inline

Definition at line 101 of file thresholder.h.

{
estimated_res_ = ppi;
}

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.

{
if (pix_ != NULL)
pixDestroy(&pix_);
pix_ = NULL;
image_data_ = imagedata;
image_width_ = width;
image_height_ = height;
image_bytespp_ = bytes_per_pixel;
image_bytespl_ = bytes_per_line;
scale_ = 1;
estimated_res_ = yres_ = 300;
Init();
}

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.

{
image_data_ = NULL;
if (pix_ != NULL)
pixDestroy(&pix_);
Pix* src = const_cast<Pix*>(pix);
int depth;
pixGetDimensions(src, &image_width_, &image_height_, &depth);
// Convert the image as necessary so it is one of binary, plain RGB, or
// 8 bit with no colormap.
if (depth > 1 && depth < 8) {
pix_ = pixConvertTo8(src, false);
} else if (pixGetColormap(src)) {
pix_ = pixRemoveColormap(src, REMOVE_CMAP_BASED_ON_SRC);
} else {
pix_ = pixClone(src);
}
depth = pixGetDepth(pix_);
image_bytespp_ = depth / 8;
image_bytespl_ = pixGetWpl(pix_) * sizeof(l_uint32);
scale_ = 1;
estimated_res_ = yres_ = pixGetYRes(src);
Init();
}

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.

{
rect_left_ = left;
rect_top_ = top;
rect_width_ = width;
rect_height_ = height;
}

void tesseract::ImageThresholder::SetSourceYResolution | ( | int | ppi | ) |

inline

Definition at line 86 of file thresholder.h.

{
yres_ = ppi;
estimated_res_ = ppi;
}

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.

{
*pix = pixCreate(rect_width_, rect_height_, 1);
uinT32* pixdata = pixGetData(*pix);
int wpl = pixGetWpl(*pix);
const unsigned char* srcdata = imagedata + rect_top_* bytes_per_line +
rect_left_ * bytes_per_pixel;
for (int y = 0; y < rect_height_; ++y) {
const uinT8* linedata = srcdata;
uinT32* pixline = pixdata + y * wpl;
for (int x = 0; x < rect_width_; ++x, linedata += bytes_per_pixel) {
bool white_result = true;
for (int ch = 0; ch < bytes_per_pixel; ++ch) {
if (hi_values[ch] >= 0 &&
(linedata[ch] > thresholds[ch]) == (hi_values[ch] == 0)) {
white_result = false;
break;
}
}
if (white_result)
CLEAR_DATA_BIT(pixline, x);
else
SET_DATA_BIT(pixline, x);
}
srcdata += bytes_per_line;
}
}

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.

{
if (pix_ != NULL) {
if (image_bytespp_ == 0) {
// We have a binary image, so it just has to be cloned.
*pix = GetPixRect();
} else {
if (image_bytespp_ == 4) {
// Color data can just be passed direct.
const uinT32* data = pixGetData(pix_);
OtsuThresholdRectToPix(reinterpret_cast<const uinT8*>(data),
image_bytespp_, image_bytespl_, pix);
} else {
// Convert 8-bit to IMAGE and then pass its
// buffer to the raw interface to complete the conversion.
IMAGE temp_image;
temp_image.FromPix(pix_);
OtsuThresholdRectToPix(temp_image.get_buffer(),
image_bytespp_,
COMPUTE_IMAGE_XDIM(temp_image.get_xsize(),
temp_image.get_bpp()),
pix);
}
}
return;
}
if (image_bytespp_ > 0) {
// Threshold grey or color.
OtsuThresholdRectToPix(image_data_, image_bytespp_, image_bytespl_, pix);
} else {
RawRectToPix(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:

- /mnt/data/src/tesseract-ocr/ccmain/thresholder.h
- /mnt/data/src/tesseract-ocr/ccmain/thresholder.cpp