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

#include <devanagari_processing.h>

Public Types

enum  SplitStrategy { NO_SPLIT = 0, MINIMAL_SPLIT, MAXIMAL_SPLIT }
 

Public Member Functions

 ShiroRekhaSplitter ()
 
virtual ~ShiroRekhaSplitter ()
 
bool Split (bool split_for_pageseg)
 
void Clear ()
 
void RefreshSegmentationWithNewBlobs (C_BLOB_LIST *new_blobs)
 
bool HasDifferentSplitStrategies () const
 
void set_segmentation_block_list (BLOCK_LIST *block_list)
 
void set_global_xheight (int xheight)
 
void set_perform_close (bool perform)
 
Pix * splitted_image ()
 
void set_orig_pix (Pix *pix)
 
Pix * orig_pix ()
 
SplitStrategy ocr_split_strategy () const
 
void set_ocr_split_strategy (SplitStrategy strategy)
 
SplitStrategy pageseg_split_strategy () const
 
void set_pageseg_split_strategy (SplitStrategy strategy)
 
BLOCK_LIST * segmentation_block_list ()
 
void DumpDebugImage (const char *filename) const
 

Static Public Member Functions

static int GetModeHeight (Pix *pix)
 

Static Public Attributes

static const int kUnspecifiedXheight = -1
 

Detailed Description

Definition at line 66 of file devanagari_processing.h.

Member Enumeration Documentation

Enumerator
NO_SPLIT 
MINIMAL_SPLIT 
MAXIMAL_SPLIT 

Definition at line 68 of file devanagari_processing.h.

68  {
69  NO_SPLIT = 0, // No splitting is performed for the phase.
70  MINIMAL_SPLIT, // Blobs are split minimally.
71  MAXIMAL_SPLIT // Blobs are split maximally.
72  };

Constructor & Destructor Documentation

tesseract::ShiroRekhaSplitter::ShiroRekhaSplitter ( )

Definition at line 37 of file devanagari_processing.cpp.

37  {
38  orig_pix_ = NULL;
39  segmentation_block_list_ = NULL;
40  splitted_image_ = NULL;
41  global_xheight_ = kUnspecifiedXheight;
42  perform_close_ = false;
43  debug_image_ = NULL;
44  pageseg_split_strategy_ = NO_SPLIT;
45  ocr_split_strategy_ = NO_SPLIT;
46 }
#define NULL
Definition: host.h:144
tesseract::ShiroRekhaSplitter::~ShiroRekhaSplitter ( )
virtual

Definition at line 48 of file devanagari_processing.cpp.

48  {
49  Clear();
50 }

Member Function Documentation

void tesseract::ShiroRekhaSplitter::Clear ( )

Definition at line 52 of file devanagari_processing.cpp.

52  {
53  pixDestroy(&orig_pix_);
54  pixDestroy(&splitted_image_);
55  pageseg_split_strategy_ = NO_SPLIT;
56  ocr_split_strategy_ = NO_SPLIT;
57  pixDestroy(&debug_image_);
58  segmentation_block_list_ = NULL;
59  global_xheight_ = kUnspecifiedXheight;
60  perform_close_ = false;
61 }
#define NULL
Definition: host.h:144
void tesseract::ShiroRekhaSplitter::DumpDebugImage ( const char *  filename) const

Definition at line 64 of file devanagari_processing.cpp.

64  {
65  pixWrite(filename, debug_image_, IFF_PNG);
66 }
int tesseract::ShiroRekhaSplitter::GetModeHeight ( Pix *  pix)
static

Definition at line 409 of file devanagari_processing.cpp.

409  {
410  Boxa* boxa = pixConnComp(pix, NULL, 8);
411  STATS heights(0, pixGetHeight(pix));
412  heights.clear();
413  for (int i = 0; i < boxaGetCount(boxa); ++i) {
414  Box* box = boxaGetBox(boxa, i, L_CLONE);
415  if (box->h >= 3 || box->w >= 3) {
416  heights.add(box->h, 1);
417  }
418  boxDestroy(&box);
419  }
420  boxaDestroy(&boxa);
421  return heights.mode();
422 }
#define NULL
Definition: host.h:144
Definition: statistc.h:29
bool tesseract::ShiroRekhaSplitter::HasDifferentSplitStrategies ( ) const
inline

Definition at line 92 of file devanagari_processing.h.

92  {
93  return pageseg_split_strategy_ != ocr_split_strategy_;
94  }
SplitStrategy tesseract::ShiroRekhaSplitter::ocr_split_strategy ( ) const
inline

Definition at line 129 of file devanagari_processing.h.

129  {
130  return ocr_split_strategy_;
131  }
Pix* tesseract::ShiroRekhaSplitter::orig_pix ( )
inline

Definition at line 125 of file devanagari_processing.h.

125  {
126  return orig_pix_;
127  }
SplitStrategy tesseract::ShiroRekhaSplitter::pageseg_split_strategy ( ) const
inline

Definition at line 137 of file devanagari_processing.h.

137  {
138  return pageseg_split_strategy_;
139  }
void tesseract::ShiroRekhaSplitter::RefreshSegmentationWithNewBlobs ( C_BLOB_LIST *  new_blobs)

Definition at line 355 of file devanagari_processing.cpp.

356  {
357  // The segmentation block list must have been specified.
358  ASSERT_HOST(segmentation_block_list_);
359  if (devanagari_split_debuglevel > 0) {
360  tprintf("Before refreshing blobs:\n");
361  PrintSegmentationStats(segmentation_block_list_);
362  tprintf("New Blobs found: %d\n", new_blobs->length());
363  }
364 
365  C_BLOB_LIST not_found_blobs;
366  RefreshWordBlobsFromNewBlobs(segmentation_block_list_,
367  new_blobs,
368  ((devanagari_split_debugimage && debug_image_) ?
369  &not_found_blobs : NULL));
370 
371  if (devanagari_split_debuglevel > 0) {
372  tprintf("After refreshing blobs:\n");
373  PrintSegmentationStats(segmentation_block_list_);
374  }
375  if (devanagari_split_debugimage && debug_image_) {
376  // Plot out the original blobs for which no match was found in the new
377  // all_blobs list.
378  C_BLOB_IT not_found_it(&not_found_blobs);
379  for (not_found_it.mark_cycle_pt(); !not_found_it.cycled_list();
380  not_found_it.forward()) {
381  C_BLOB* not_found = not_found_it.data();
382  TBOX not_found_box = not_found->bounding_box();
383  Box* box_to_plot = GetBoxForTBOX(not_found_box);
384  pixRenderBoxArb(debug_image_, box_to_plot, 1, 255, 0, 255);
385  boxDestroy(&box_to_plot);
386  }
387 
388  // Plot out the blobs unused from all blobs.
389  C_BLOB_IT all_blobs_it(new_blobs);
390  for (all_blobs_it.mark_cycle_pt(); !all_blobs_it.cycled_list();
391  all_blobs_it.forward()) {
392  C_BLOB* a_blob = all_blobs_it.data();
393  Box* box_to_plot = GetBoxForTBOX(a_blob->bounding_box());
394  pixRenderBoxArb(debug_image_, box_to_plot, 3, 0, 127, 0);
395  boxDestroy(&box_to_plot);
396  }
397  }
398 }
void PrintSegmentationStats(BLOCK_LIST *block_list)
Definition: ocrblock.cpp:400
#define NULL
Definition: host.h:144
Definition: rect.h:29
int devanagari_split_debuglevel
bool devanagari_split_debugimage
void RefreshWordBlobsFromNewBlobs(BLOCK_LIST *block_list, C_BLOB_LIST *new_blobs, C_BLOB_LIST *not_found_blobs)
Definition: ocrblock.cpp:468
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:41
TBOX bounding_box()
Definition: stepblob.cpp:192
#define ASSERT_HOST(x)
Definition: errcode.h:84
BLOCK_LIST* tesseract::ShiroRekhaSplitter::segmentation_block_list ( )
inline

Definition at line 145 of file devanagari_processing.h.

145  {
146  return segmentation_block_list_;
147  }
void tesseract::ShiroRekhaSplitter::set_global_xheight ( int  xheight)
inline

Definition at line 105 of file devanagari_processing.h.

105  {
106  global_xheight_ = xheight;
107  }
void tesseract::ShiroRekhaSplitter::set_ocr_split_strategy ( SplitStrategy  strategy)
inline

Definition at line 133 of file devanagari_processing.h.

133  {
134  ocr_split_strategy_ = strategy;
135  }
void tesseract::ShiroRekhaSplitter::set_orig_pix ( Pix *  pix)

Definition at line 69 of file devanagari_processing.cpp.

69  {
70  if (orig_pix_) {
71  pixDestroy(&orig_pix_);
72  }
73  orig_pix_ = pixClone(pix);
74 }
void tesseract::ShiroRekhaSplitter::set_pageseg_split_strategy ( SplitStrategy  strategy)
inline

Definition at line 141 of file devanagari_processing.h.

141  {
142  pageseg_split_strategy_ = strategy;
143  }
void tesseract::ShiroRekhaSplitter::set_perform_close ( bool  perform)
inline

Definition at line 109 of file devanagari_processing.h.

109  {
110  perform_close_ = perform;
111  }
void tesseract::ShiroRekhaSplitter::set_segmentation_block_list ( BLOCK_LIST *  block_list)
inline

Definition at line 99 of file devanagari_processing.h.

99  {
100  segmentation_block_list_ = block_list;
101  }
bool tesseract::ShiroRekhaSplitter::Split ( bool  split_for_pageseg)

Definition at line 81 of file devanagari_processing.cpp.

81  {
82  SplitStrategy split_strategy = split_for_pageseg ? pageseg_split_strategy_ :
83  ocr_split_strategy_;
84  if (split_strategy == NO_SPLIT) {
85  return false; // Nothing to do.
86  }
87  ASSERT_HOST(split_strategy == MINIMAL_SPLIT ||
88  split_strategy == MAXIMAL_SPLIT);
89  ASSERT_HOST(orig_pix_);
91  tprintf("Splitting shiro-rekha ...\n");
92  tprintf("Split strategy = %s\n",
93  split_strategy == MINIMAL_SPLIT ? "Minimal" : "Maximal");
94  tprintf("Initial pageseg available = %s\n",
95  segmentation_block_list_ ? "yes" : "no");
96  }
97  // Create a copy of original image to store the splitting output.
98  pixDestroy(&splitted_image_);
99  splitted_image_ = pixCopy(NULL, orig_pix_);
100 
101  // Initialize debug image if required.
103  pixDestroy(&debug_image_);
104  debug_image_ = pixConvertTo32(orig_pix_);
105  }
106 
107  // Determine all connected components in the input image. A close operation
108  // may be required prior to this, depending on the current settings.
109  Pix* pix_for_ccs = pixClone(orig_pix_);
110  if (perform_close_ && global_xheight_ != kUnspecifiedXheight &&
111  !segmentation_block_list_) {
112  if (devanagari_split_debuglevel > 0) {
113  tprintf("Performing a global close operation..\n");
114  }
115  // A global measure is available for xheight, but no local information
116  // exists.
117  pixDestroy(&pix_for_ccs);
118  pix_for_ccs = pixCopy(NULL, orig_pix_);
119  PerformClose(pix_for_ccs, global_xheight_);
120  }
121  Pixa* ccs;
122  Boxa* tmp_boxa = pixConnComp(pix_for_ccs, &ccs, 8);
123  boxaDestroy(&tmp_boxa);
124  pixDestroy(&pix_for_ccs);
125 
126  // Iterate over all connected components. Get their bounding boxes and clip
127  // out the image regions corresponding to these boxes from the original image.
128  // Conditionally run splitting on each of them.
129  Boxa* regions_to_clear = boxaCreate(0);
130  for (int i = 0; i < pixaGetCount(ccs); ++i) {
131  Box* box = ccs->boxa->box[i];
132  Pix* word_pix = pixClipRectangle(orig_pix_, box, NULL);
133  ASSERT_HOST(word_pix);
134  int xheight = GetXheightForCC(box);
135  if (xheight == kUnspecifiedXheight && segmentation_block_list_ &&
137  pixRenderBoxArb(debug_image_, box, 1, 255, 0, 0);
138  }
139  // If some xheight measure is available, attempt to pre-eliminate small
140  // blobs from the shiro-rekha process. This is primarily to save the CCs
141  // corresponding to punctuation marks/small dots etc which are part of
142  // larger graphemes.
143  if (xheight == kUnspecifiedXheight ||
144  (box->w > xheight / 3 && box->h > xheight / 2)) {
145  SplitWordShiroRekha(split_strategy, word_pix, xheight,
146  box->x, box->y, regions_to_clear);
147  } else if (devanagari_split_debuglevel > 0) {
148  tprintf("CC dropped from splitting: %d,%d (%d, %d)\n",
149  box->x, box->y, box->w, box->h);
150  }
151  pixDestroy(&word_pix);
152  }
153  // Actually clear the boxes now.
154  for (int i = 0; i < boxaGetCount(regions_to_clear); ++i) {
155  Box* box = boxaGetBox(regions_to_clear, i, L_CLONE);
156  pixClearInRect(splitted_image_, box);
157  boxDestroy(&box);
158  }
159  boxaDestroy(&regions_to_clear);
160  pixaDestroy(&ccs);
162  DumpDebugImage(split_for_pageseg ? "pageseg_split_debug.png" :
163  "ocr_split_debug.png");
164  }
165  return true;
166 }
#define NULL
Definition: host.h:144
int devanagari_split_debuglevel
bool devanagari_split_debugimage
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:41
void DumpDebugImage(const char *filename) const
#define ASSERT_HOST(x)
Definition: errcode.h:84
Pix* tesseract::ShiroRekhaSplitter::splitted_image ( )
inline

Definition at line 116 of file devanagari_processing.h.

116  {
117  return splitted_image_;
118  }

Member Data Documentation

const int tesseract::ShiroRekhaSplitter::kUnspecifiedXheight = -1
static

Definition at line 103 of file devanagari_processing.h.


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