tesseract  3.03
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)
virtual Pix * GetPixRectThresholds ()
Pix * GetPixRect ()
virtual 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 (Pix *src_pix, Pix **out_pix) const
void ThresholdRectToPix (Pix *src_pix, int num_channels, const int *thresholds, const int *hi_values, Pix **pix) const

Protected Attributes

Pix * pix_
int image_width_
int image_height_
int pix_channels_
int pix_wpl_
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 36 of file thresholder.h.


Constructor & Destructor Documentation

Definition at line 32 of file thresholder.cpp.

  : pix_(NULL),
    image_width_(0), image_height_(0),
    pix_channels_(0), pix_wpl_(0),
    scale_(1), yres_(300), estimated_res_(300) {
  SetRectangle(0, 0, 0, 0);
}

Definition at line 40 of file thresholder.cpp.

                                    {
  Clear();
}

Member Function Documentation

Destroy the Pix if there is one, freeing memory.

Definition at line 45 of file thresholder.cpp.

                             {
  pixDestroy(&pix_);
}
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 132 of file thresholder.cpp.

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

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 217 of file thresholder.cpp.

                                  {
  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;
  }
}

Definition at line 234 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;
}

Definition at line 190 of file thresholder.cpp.

                                            {
  if (IsBinary()) return NULL;
  Pix* pix_grey = GetPixRectGrey();
  int width = pixGetWidth(pix_grey);
  int height = pixGetHeight(pix_grey);
  int* thresholds;
  int* hi_values;
  OtsuThreshold(pix_grey, 0, 0, width, height, &thresholds, &hi_values);
  pixDestroy(&pix_grey);
  Pix* pix_thresholds = pixCreate(width, height, 8);
  int threshold = thresholds[0] > 0 ? thresholds[0] : 128;
  pixSetAllArbitrary(pix_thresholds, threshold);
  delete [] thresholds;
  delete [] hi_values;
  return pix_thresholds;
}

Definition at line 106 of file thresholder.h.

                                           {
    return scale_ * estimated_res_;
  }

Definition at line 93 of file thresholder.h.

                                   {
    return scale_ * yres_;
  }

Definition at line 79 of file thresholder.h.

                             {
    return scale_;
  }

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 208 of file thresholder.cpp.

bool tesseract::ImageThresholder::IsBinary ( ) const [inline]

Returns true if the source image is binary.

Definition at line 75 of file thresholder.h.

                        {
    return pix_channels_ == 0;
  }
bool tesseract::ImageThresholder::IsColor ( ) const [inline]

Return true if the source image is color.

Definition at line 70 of file thresholder.h.

                       {
    return pix_channels_ >= 3;
  }

Return true if no image has been set.

Definition at line 50 of file thresholder.cpp.

                                     {
  return pix_ == NULL;
}
bool tesseract::ImageThresholder::IsFullImage ( ) const [inline, protected]

Return true if we are processing the full image.

Definition at line 152 of file thresholder.h.

void tesseract::ImageThresholder::OtsuThresholdRectToPix ( Pix *  src_pix,
Pix **  out_pix 
) const [protected]

Definition at line 247 of file thresholder.cpp.

                                                                   {
  PERF_COUNT_START("OtsuThresholdRectToPix")
  int* thresholds;
  int* hi_values;

  int num_channels = OtsuThreshold(src_pix, rect_left_, rect_top_, rect_width_,
                                   rect_height_, &thresholds, &hi_values);
  // only use opencl if compiled w/ OpenCL and selected device is opencl
#ifdef USE_OPENCL
  OpenclDevice od;
  if ((num_channels == 4 || num_channels == 1) &&
      od.selectedDeviceIsOpenCL() && rect_top_ == 0 && rect_left_ == 0 ) {
    od.ThresholdRectToPixOCL((const unsigned char*)pixGetData(src_pix),
                             num_channels, pixGetWpl(src_pix) * 4,
                             thresholds, hi_values, out_pix /*pix_OCL*/,
                             rect_height_, rect_width_, rect_top_, rect_left_);
  } else {
#endif
    ThresholdRectToPix(src_pix, num_channels, thresholds, hi_values, out_pix);
#ifdef USE_OPENCL
  }
#endif
  delete [] thresholds;
  delete [] hi_values;

  PERF_COUNT_END
}

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 all the image data, so it may be deleted immediately after this call. 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 62 of file thresholder.cpp.

                                                                         {
  int bpp = bytes_per_pixel * 8;
  if (bpp == 0) bpp = 1;
  Pix* pix = pixCreate(width, height, bpp == 24 ? 32 : bpp);
  l_uint32* data = pixGetData(pix);
  int wpl = pixGetWpl(pix);
  switch (bpp) {
  case 1:
    for (int y = 0; y < height; ++y, data += wpl, imagedata += bytes_per_line) {
      for (int x = 0; x < width; ++x) {
        if (imagedata[x / 8] & (0x80 >> (x % 8)))
          CLEAR_DATA_BIT(data, x);
        else
          SET_DATA_BIT(data, x);
      }
    }
    break;

  case 8:
    // Greyscale just copies the bytes in the right order.
    for (int y = 0; y < height; ++y, data += wpl, imagedata += bytes_per_line) {
      for (int x = 0; x < width; ++x)
        SET_DATA_BYTE(data, x, imagedata[x]);
    }
    break;

  case 24:
    // Put the colors in the correct places in the line buffer.
    for (int y = 0; y < height; ++y, imagedata += bytes_per_line) {
      for (int x = 0; x < width; ++x, ++data) {
        SET_DATA_BYTE(data, COLOR_RED, imagedata[3 * x]);
        SET_DATA_BYTE(data, COLOR_GREEN, imagedata[3 * x + 1]);
        SET_DATA_BYTE(data, COLOR_BLUE, imagedata[3 * x + 2]);
      }
    }
    break;

  case 32:
    // Maintain byte order consistency across different endianness.
    for (int y = 0; y < height; ++y, imagedata += bytes_per_line, data += wpl) {
      for (int x = 0; x < width; ++x) {
        data[x] = (imagedata[x * 4] << 24) | (imagedata[x * 4 + 1] << 16) |
                  (imagedata[x * 4 + 2] << 8) | imagedata[x * 4 + 3];
      }
    }
    break;

  default:
    tprintf("Cannot convert RAW image to Pix with bpp = %d\n", bpp);
  }
  pixSetYRes(pix, 300);
  SetImage(pix);
  pixDestroy(&pix);
}
void tesseract::ImageThresholder::SetImage ( const Pix *  pix)

Pix vs raw, which to use? Pix is the preferred input for efficiency, since raw buffers are copied. SetImage for Pix clones its input, so the source pix may be pixDestroyed immediately after, but may not go away until after the Thresholder has finished with it.

Definition at line 148 of file thresholder.cpp.

                                              {
  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_);
  pix_channels_ = depth / 8;
  pix_wpl_ = pixGetWpl(pix_);
  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 121 of file thresholder.cpp.

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

Definition at line 86 of file thresholder.h.

                                     {
    yres_ = ppi;
    estimated_res_ = ppi;
  }
void tesseract::ImageThresholder::ThresholdRectToPix ( Pix *  src_pix,
int  num_channels,
const int *  thresholds,
const int *  hi_values,
Pix **  pix 
) const [protected]

Threshold the rectangle, taking everything except the src_pix from the class, using thresholds/hi_values to the output pix. NOTE that num_channels is the size of the thresholds and hi_values

Definition at line 280 of file thresholder.cpp.

                                                           {
  PERF_COUNT_START("ThresholdRectToPix")
  *pix = pixCreate(rect_width_, rect_height_, 1);
  uinT32* pixdata = pixGetData(*pix);
  int wpl = pixGetWpl(*pix);
  int src_wpl = pixGetWpl(src_pix);
  uinT32* srcdata = pixGetData(src_pix);
  for (int y = 0; y < rect_height_; ++y) {
    const uinT32* linedata = srcdata + (y + rect_top_) * src_wpl;
    uinT32* pixline = pixdata + y * wpl;
    for (int x = 0; x < rect_width_; ++x) {
      bool white_result = true;
      for (int ch = 0; ch < num_channels; ++ch) {
        int pixel = GET_DATA_BYTE(const_cast<void*>(
                                  reinterpret_cast<const void *>(linedata)),
                                  (x + rect_left_) * num_channels + ch);
        if (hi_values[ch] >= 0 &&
            (pixel > thresholds[ch]) == (hi_values[ch] == 0)) {
          white_result = false;
          break;
        }
      }
      if (white_result)
        CLEAR_DATA_BIT(pixline, x);
      else
        SET_DATA_BIT(pixline, x);
    }
  }

  PERF_COUNT_END
}
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 174 of file thresholder.cpp.

                                               {
  if (pix_channels_ == 0) {
    // We have a binary image, so it just has to be cloned.
    *pix = GetPixRect();
  } else {
    OtsuThresholdRectToPix(pix_, pix);
  }
}

Member Data Documentation

Definition at line 180 of file thresholder.h.

Definition at line 174 of file thresholder.h.

Definition at line 173 of file thresholder.h.

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.

Definition at line 175 of file thresholder.h.

Definition at line 176 of file thresholder.h.

Definition at line 184 of file thresholder.h.

Definition at line 181 of file thresholder.h.

Definition at line 182 of file thresholder.h.

Definition at line 183 of file thresholder.h.

Definition at line 178 of file thresholder.h.

Definition at line 179 of file thresholder.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines