tesseract  5.0.0-alpha-619-ge9db
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 bool ThresholdToPix (PageSegMode pageseg_mode, Pix **pix)
 Returns false on error. More...
 
virtual Pix * GetPixRectThresholds ()
 
Pix * GetPixRect ()
 
virtual 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 (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_
 Width of source pix_. More...
 
int image_height_
 Height of source pix_. More...
 
int pix_channels_
 Number of 8-bit channels in pix_. More...
 
int pix_wpl_
 Words per line of pix_. More...
 
int scale_
 Scale factor from original image. More...
 
int yres_
 y pixels/inch in source image. More...
 
int estimated_res_
 Resolution estimate from text size. More...
 
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 35 of file thresholder.h.

Constructor & Destructor Documentation

◆ ImageThresholder()

tesseract::ImageThresholder::ImageThresholder ( )

Definition at line 35 of file thresholder.cpp.

36  : pix_(nullptr),
38  pix_channels_(0), pix_wpl_(0),
39  scale_(1), yres_(300), estimated_res_(300) {
40  SetRectangle(0, 0, 0, 0);
41 }

◆ ~ImageThresholder()

tesseract::ImageThresholder::~ImageThresholder ( )
virtual

Definition at line 43 of file thresholder.cpp.

43  {
44  Clear();
45 }

Member Function Documentation

◆ Clear()

void tesseract::ImageThresholder::Clear ( )
virtual

Destroy the Pix if there is one, freeing memory.

Definition at line 48 of file thresholder.cpp.

48  {
49  pixDestroy(&pix_);
50 }

◆ GetImageSizes()

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

136  {
137  *left = rect_left_;
138  *top = rect_top_;
139  *width = rect_width_;
140  *height = rect_height_;
141  *imagewidth = image_width_;
142  *imageheight = image_height_;
143 }

◆ GetPixRect()

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

236  {
237  if (IsFullImage()) {
238  // Just clone the whole thing.
239  return pixClone(pix_);
240  } else {
241  // Crop to the given rectangle.
242  Box* box = boxCreate(rect_left_, rect_top_, rect_width_, rect_height_);
243  Pix* cropped = pixClipRectangle(pix_, box, nullptr);
244  boxDestroy(&box);
245  return cropped;
246  }
247 }

◆ GetPixRectGrey()

Pix * tesseract::ImageThresholder::GetPixRectGrey ( )
virtual

Definition at line 253 of file thresholder.cpp.

253  {
254  Pix* pix = GetPixRect(); // May have to be reduced to grey.
255  int depth = pixGetDepth(pix);
256  if (depth != 8) {
257  Pix* result = depth < 8 ? pixConvertTo8(pix, false)
258  : pixConvertRGBToLuminance(pix);
259  pixDestroy(&pix);
260  return result;
261  }
262  return pix;
263 }

◆ GetPixRectThresholds()

Pix * tesseract::ImageThresholder::GetPixRectThresholds ( )
virtual

Definition at line 209 of file thresholder.cpp.

209  {
210  if (IsBinary()) return nullptr;
211  Pix* pix_grey = GetPixRectGrey();
212  int width = pixGetWidth(pix_grey);
213  int height = pixGetHeight(pix_grey);
214  int* thresholds;
215  int* hi_values;
216  OtsuThreshold(pix_grey, 0, 0, width, height, &thresholds, &hi_values);
217  pixDestroy(&pix_grey);
218  Pix* pix_thresholds = pixCreate(width, height, 8);
219  int threshold = thresholds[0] > 0 ? thresholds[0] : 128;
220  pixSetAllArbitrary(pix_thresholds, threshold);
221  delete [] thresholds;
222  delete [] hi_values;
223  return pix_thresholds;
224 }

◆ GetScaledEstimatedResolution()

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

Definition at line 105 of file thresholder.h.

105  {
106  return scale_ * estimated_res_;
107  }

◆ GetScaledYResolution()

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

Definition at line 92 of file thresholder.h.

92  {
93  return scale_ * yres_;
94  }

◆ GetScaleFactor()

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

Definition at line 78 of file thresholder.h.

78  {
79  return scale_;
80  }

◆ GetSourceYResolution()

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

Definition at line 89 of file thresholder.h.

89  {
90  return yres_;
91  }

◆ Init()

void tesseract::ImageThresholder::Init ( )
protectedvirtual

Common initialization shared between SetImage methods.

Definition at line 227 of file thresholder.cpp.

227  {
229 }

◆ IsBinary()

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

Returns true if the source image is binary.

Definition at line 74 of file thresholder.h.

74  {
75  return pix_channels_ == 0;
76  }

◆ IsColor()

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

Return true if the source image is color.

Definition at line 69 of file thresholder.h.

69  {
70  return pix_channels_ >= 3;
71  }

◆ IsEmpty()

bool tesseract::ImageThresholder::IsEmpty ( ) const

Return true if no image has been set.

Definition at line 53 of file thresholder.cpp.

53  {
54  return pix_ == nullptr;
55 }

◆ IsFullImage()

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

Return true if we are processing the full image.

Definition at line 152 of file thresholder.h.

152  {
153  return rect_left_ == 0 && rect_top_ == 0 && rect_width_ == image_width_ &&
155  }

◆ OtsuThresholdRectToPix()

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

Definition at line 266 of file thresholder.cpp.

267  {
268  int* thresholds;
269  int* hi_values;
270 
271  int num_channels = OtsuThreshold(src_pix, rect_left_, rect_top_, rect_width_,
272  rect_height_, &thresholds, &hi_values);
273  // only use opencl if compiled w/ OpenCL and selected device is opencl
274 #ifdef USE_OPENCL
275  OpenclDevice od;
276  if (num_channels == 4 &&
277  od.selectedDeviceIsOpenCL() && rect_top_ == 0 && rect_left_ == 0) {
278  od.ThresholdRectToPixOCL((unsigned char*)pixGetData(src_pix), num_channels,
279  pixGetWpl(src_pix) * 4, thresholds, hi_values,
280  out_pix /*pix_OCL*/, rect_height_, rect_width_,
282  } else {
283 #endif
284  ThresholdRectToPix(src_pix, num_channels, thresholds, hi_values, out_pix);
285 #ifdef USE_OPENCL
286  }
287 #endif
288  delete [] thresholds;
289  delete [] hi_values;
290 }

◆ SetEstimatedResolution()

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

Definition at line 100 of file thresholder.h.

100  {
101  estimated_res_ = ppi;
102  }

◆ SetImage() [1/2]

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

150  {
151  if (pix_ != nullptr)
152  pixDestroy(&pix_);
153  Pix* src = const_cast<Pix*>(pix);
154  int depth;
155  pixGetDimensions(src, &image_width_, &image_height_, &depth);
156  // Convert the image as necessary so it is one of binary, plain RGB, or
157  // 8 bit with no colormap. Guarantee that we always end up with our own copy,
158  // not just a clone of the input.
159  if (pixGetColormap(src)) {
160  Pix* tmp = pixRemoveColormap(src, REMOVE_CMAP_BASED_ON_SRC);
161  depth = pixGetDepth(tmp);
162  if (depth > 1 && depth < 8) {
163  pix_ = pixConvertTo8(tmp, false);
164  pixDestroy(&tmp);
165  } else {
166  pix_ = tmp;
167  }
168  } else if (depth > 1 && depth < 8) {
169  pix_ = pixConvertTo8(src, false);
170  } else {
171  pix_ = pixCopy(nullptr, src);
172  }
173  depth = pixGetDepth(pix_);
174  pix_channels_ = depth / 8;
175  pix_wpl_ = pixGetWpl(pix_);
176  scale_ = 1;
177  estimated_res_ = yres_ = pixGetYRes(pix_);
178  Init();
179 }

◆ SetImage() [2/2]

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

67  {
68  int bpp = bytes_per_pixel * 8;
69  if (bpp == 0) bpp = 1;
70  Pix* pix = pixCreate(width, height, bpp == 24 ? 32 : bpp);
71  l_uint32* data = pixGetData(pix);
72  int wpl = pixGetWpl(pix);
73  switch (bpp) {
74  case 1:
75  for (int y = 0; y < height; ++y, data += wpl, imagedata += bytes_per_line) {
76  for (int x = 0; x < width; ++x) {
77  if (imagedata[x / 8] & (0x80 >> (x % 8)))
78  CLEAR_DATA_BIT(data, x);
79  else
80  SET_DATA_BIT(data, x);
81  }
82  }
83  break;
84 
85  case 8:
86  // Greyscale just copies the bytes in the right order.
87  for (int y = 0; y < height; ++y, data += wpl, imagedata += bytes_per_line) {
88  for (int x = 0; x < width; ++x)
89  SET_DATA_BYTE(data, x, imagedata[x]);
90  }
91  break;
92 
93  case 24:
94  // Put the colors in the correct places in the line buffer.
95  for (int y = 0; y < height; ++y, imagedata += bytes_per_line) {
96  for (int x = 0; x < width; ++x, ++data) {
97  SET_DATA_BYTE(data, COLOR_RED, imagedata[3 * x]);
98  SET_DATA_BYTE(data, COLOR_GREEN, imagedata[3 * x + 1]);
99  SET_DATA_BYTE(data, COLOR_BLUE, imagedata[3 * x + 2]);
100  }
101  }
102  break;
103 
104  case 32:
105  // Maintain byte order consistency across different endianness.
106  for (int y = 0; y < height; ++y, imagedata += bytes_per_line, data += wpl) {
107  for (int x = 0; x < width; ++x) {
108  data[x] = (imagedata[x * 4] << 24) | (imagedata[x * 4 + 1] << 16) |
109  (imagedata[x * 4 + 2] << 8) | imagedata[x * 4 + 3];
110  }
111  }
112  break;
113 
114  default:
115  tprintf("Cannot convert RAW image to Pix with bpp = %d\n", bpp);
116  }
117  SetImage(pix);
118  pixDestroy(&pix);
119 }

◆ SetRectangle()

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

123  {
124  rect_left_ = left;
125  rect_top_ = top;
126  rect_width_ = width;
127  rect_height_ = height;
128 }

◆ SetSourceYResolution()

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

Definition at line 85 of file thresholder.h.

85  {
86  yres_ = ppi;
87  estimated_res_ = ppi;
88  }

◆ ThresholdRectToPix()

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

300  {
301  *pix = pixCreate(rect_width_, rect_height_, 1);
302  uint32_t* pixdata = pixGetData(*pix);
303  int wpl = pixGetWpl(*pix);
304  int src_wpl = pixGetWpl(src_pix);
305  uint32_t* srcdata = pixGetData(src_pix);
306  pixSetXRes(*pix, pixGetXRes(src_pix));
307  pixSetYRes(*pix, pixGetYRes(src_pix));
308  for (int y = 0; y < rect_height_; ++y) {
309  const uint32_t* linedata = srcdata + (y + rect_top_) * src_wpl;
310  uint32_t* pixline = pixdata + y * wpl;
311  for (int x = 0; x < rect_width_; ++x) {
312  bool white_result = true;
313  for (int ch = 0; ch < num_channels; ++ch) {
314  int pixel =
315  GET_DATA_BYTE(linedata, (x + rect_left_) * num_channels + ch);
316  if (hi_values[ch] >= 0 &&
317  (pixel > thresholds[ch]) == (hi_values[ch] == 0)) {
318  white_result = false;
319  break;
320  }
321  }
322  if (white_result)
323  CLEAR_DATA_BIT(pixline, x);
324  else
325  SET_DATA_BIT(pixline, x);
326  }
327  }
328 }

◆ ThresholdToPix()

bool tesseract::ImageThresholder::ThresholdToPix ( PageSegMode  pageseg_mode,
Pix **  pix 
)
virtual

Returns false on error.

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. Returns false on error.

Definition at line 185 of file thresholder.cpp.

185  {
186  if (image_width_ > INT16_MAX || image_height_ > INT16_MAX) {
187  tprintf("Image too large: (%d, %d)\n", image_width_, image_height_);
188  return false;
189  }
190  if (pix_channels_ == 0) {
191  // We have a binary image, but it still has to be copied, as this API
192  // allows the caller to modify the output.
193  Pix* original = GetPixRect();
194  *pix = pixCopy(nullptr, original);
195  pixDestroy(&original);
196  } else {
198  }
199  return true;
200 }

Member Data Documentation

◆ estimated_res_

int tesseract::ImageThresholder::estimated_res_
protected

Resolution estimate from text size.

Definition at line 179 of file thresholder.h.

◆ image_height_

int tesseract::ImageThresholder::image_height_
protected

Height of source pix_.

Definition at line 173 of file thresholder.h.

◆ image_width_

int tesseract::ImageThresholder::image_width_
protected

Width of source pix_.

Definition at line 172 of file thresholder.h.

◆ pix_

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 170 of file thresholder.h.

◆ pix_channels_

int tesseract::ImageThresholder::pix_channels_
protected

Number of 8-bit channels in pix_.

Definition at line 174 of file thresholder.h.

◆ pix_wpl_

int tesseract::ImageThresholder::pix_wpl_
protected

Words per line of pix_.

Definition at line 175 of file thresholder.h.

◆ rect_height_

int tesseract::ImageThresholder::rect_height_
protected

Definition at line 183 of file thresholder.h.

◆ rect_left_

int tesseract::ImageThresholder::rect_left_
protected

Definition at line 180 of file thresholder.h.

◆ rect_top_

int tesseract::ImageThresholder::rect_top_
protected

Definition at line 181 of file thresholder.h.

◆ rect_width_

int tesseract::ImageThresholder::rect_width_
protected

Definition at line 182 of file thresholder.h.

◆ scale_

int tesseract::ImageThresholder::scale_
protected

Scale factor from original image.

Definition at line 177 of file thresholder.h.

◆ yres_

int tesseract::ImageThresholder::yres_
protected

y pixels/inch in source image.

Definition at line 178 of file thresholder.h.


The documentation for this class was generated from the following files:
tesseract::ImageThresholder::pix_wpl_
int pix_wpl_
Words per line of pix_.
Definition: thresholder.h:175
tesseract::ImageThresholder::Init
virtual void Init()
Common initialization shared between SetImage methods.
Definition: thresholder.cpp:227
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
tesseract::ImageThresholder::ThresholdRectToPix
void ThresholdRectToPix(Pix *src_pix, int num_channels, const int *thresholds, const int *hi_values, Pix **pix) const
Definition: thresholder.cpp:296
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::rect_top_
int rect_top_
Definition: thresholder.h:181
tesseract::ImageThresholder::Clear
virtual void Clear()
Destroy the Pix if there is one, freeing memory.
Definition: thresholder.cpp:48
tesseract::ImageThresholder::pix_channels_
int pix_channels_
Number of 8-bit channels in pix_.
Definition: thresholder.h:174
tesseract::ImageThresholder::yres_
int yres_
y pixels/inch in source image.
Definition: thresholder.h:178
tesseract::ImageThresholder::GetPixRectGrey
virtual Pix * GetPixRectGrey()
Definition: thresholder.cpp:253
tesseract::OtsuThreshold
int OtsuThreshold(Pix *src_pix, int left, int top, int width, int height, int **thresholds, int **hi_values)
Definition: otsuthr.cpp:56
tesseract::ImageThresholder::SetImage
void SetImage(const unsigned char *imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line)
Definition: thresholder.cpp:65
tesseract::ImageThresholder::GetPixRect
Pix * GetPixRect()
Definition: thresholder.cpp:236
tesseract::ImageThresholder::OtsuThresholdRectToPix
void OtsuThresholdRectToPix(Pix *src_pix, Pix **out_pix) const
Definition: thresholder.cpp:266
tprintf
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:34
tesseract::ImageThresholder::scale_
int scale_
Scale factor from original image.
Definition: thresholder.h:177
tesseract::ImageThresholder::SetRectangle
void SetRectangle(int left, int top, int width, int height)
Definition: thresholder.cpp:123
tesseract::ImageThresholder::rect_width_
int rect_width_
Definition: thresholder.h:182