All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 65 of file devanagari_processing.h.

Member Enumeration Documentation

Enumerator
NO_SPLIT 
MINIMAL_SPLIT 
MAXIMAL_SPLIT 

Definition at line 67 of file devanagari_processing.h.

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

Constructor & Destructor Documentation

tesseract::ShiroRekhaSplitter::ShiroRekhaSplitter ( )

Definition at line 40 of file devanagari_processing.cpp.

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

Definition at line 51 of file devanagari_processing.cpp.

51  {
52  Clear();
53 }

Member Function Documentation

void tesseract::ShiroRekhaSplitter::Clear ( )

Definition at line 55 of file devanagari_processing.cpp.

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

Definition at line 67 of file devanagari_processing.cpp.

67  {
68  pixWrite(filename, debug_image_, IFF_PNG);
69 }
int tesseract::ShiroRekhaSplitter::GetModeHeight ( Pix *  pix)
static

Definition at line 412 of file devanagari_processing.cpp.

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

Definition at line 91 of file devanagari_processing.h.

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

Definition at line 128 of file devanagari_processing.h.

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

Definition at line 124 of file devanagari_processing.h.

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

Definition at line 136 of file devanagari_processing.h.

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

Definition at line 358 of file devanagari_processing.cpp.

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

Definition at line 144 of file devanagari_processing.h.

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

Definition at line 104 of file devanagari_processing.h.

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

Definition at line 132 of file devanagari_processing.h.

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

Definition at line 72 of file devanagari_processing.cpp.

72  {
73  if (orig_pix_) {
74  pixDestroy(&orig_pix_);
75  }
76  orig_pix_ = pixClone(pix);
77 }
void tesseract::ShiroRekhaSplitter::set_pageseg_split_strategy ( SplitStrategy  strategy)
inline

Definition at line 140 of file devanagari_processing.h.

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

Definition at line 108 of file devanagari_processing.h.

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

Definition at line 98 of file devanagari_processing.h.

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

Definition at line 84 of file devanagari_processing.cpp.

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

Definition at line 115 of file devanagari_processing.h.

115  {
116  return splitted_image_;
117  }

Member Data Documentation

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

Definition at line 102 of file devanagari_processing.h.


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