tesseract v5.3.3.20231005
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, DebugPixa *pixa_debug)
 
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)
 
Image splitted_image ()
 
void set_orig_pix (Image pix)
 
Image 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 ()
 

Static Public Member Functions

static int GetModeHeight (Image pix)
 

Static Public Attributes

static const int kUnspecifiedXheight = -1
 

Detailed Description

Definition at line 70 of file devanagari_processing.h.

Member Enumeration Documentation

◆ SplitStrategy

Enumerator
NO_SPLIT 
MINIMAL_SPLIT 
MAXIMAL_SPLIT 

Definition at line 72 of file devanagari_processing.h.

72 {
73 NO_SPLIT = 0, // No splitting is performed for the phase.
74 MINIMAL_SPLIT, // Blobs are split minimally.
75 MAXIMAL_SPLIT // Blobs are split maximally.
76 };

Constructor & Destructor Documentation

◆ ShiroRekhaSplitter()

tesseract::ShiroRekhaSplitter::ShiroRekhaSplitter ( )

Definition at line 41 of file devanagari_processing.cpp.

41 {
42 orig_pix_ = nullptr;
43 segmentation_block_list_ = nullptr;
44 splitted_image_ = nullptr;
45 global_xheight_ = kUnspecifiedXheight;
46 perform_close_ = false;
47 debug_image_ = nullptr;
48 pageseg_split_strategy_ = NO_SPLIT;
49 ocr_split_strategy_ = NO_SPLIT;
50}

◆ ~ShiroRekhaSplitter()

tesseract::ShiroRekhaSplitter::~ShiroRekhaSplitter ( )
virtual

Definition at line 52 of file devanagari_processing.cpp.

Member Function Documentation

◆ Clear()

void tesseract::ShiroRekhaSplitter::Clear ( )

Definition at line 56 of file devanagari_processing.cpp.

56 {
57 orig_pix_.destroy();
58 splitted_image_.destroy();
59 pageseg_split_strategy_ = NO_SPLIT;
60 ocr_split_strategy_ = NO_SPLIT;
61 debug_image_.destroy();
62 segmentation_block_list_ = nullptr;
63 global_xheight_ = kUnspecifiedXheight;
64 perform_close_ = false;
65}
void destroy()
Definition: image.cpp:32

◆ GetModeHeight()

int tesseract::ShiroRekhaSplitter::GetModeHeight ( Image  pix)
static

Definition at line 392 of file devanagari_processing.cpp.

392 {
393 Boxa *boxa = pixConnComp(pix, nullptr, 8);
394 STATS heights(0, pixGetHeight(pix) - 1);
395 heights.clear();
396 for (int i = 0; i < boxaGetCount(boxa); ++i) {
397 Box *box = boxaGetBox(boxa, i, L_CLONE);
398 l_int32 x, y, w, h;
399 boxGetGeometry(box, &x, &y, &w, &h);
400 if (h >= 3 || w >= 3) {
401 heights.add(h, 1);
402 }
403 boxDestroy(&box);
404 }
405 boxaDestroy(&boxa);
406 return heights.mode();
407}
const double y

◆ HasDifferentSplitStrategies()

bool tesseract::ShiroRekhaSplitter::HasDifferentSplitStrategies ( ) const
inline

Definition at line 96 of file devanagari_processing.h.

96 {
97 return pageseg_split_strategy_ != ocr_split_strategy_;
98 }

◆ ocr_split_strategy()

SplitStrategy tesseract::ShiroRekhaSplitter::ocr_split_strategy ( ) const
inline

Definition at line 133 of file devanagari_processing.h.

133 {
134 return ocr_split_strategy_;
135 }

◆ orig_pix()

Image tesseract::ShiroRekhaSplitter::orig_pix ( )
inline

Definition at line 129 of file devanagari_processing.h.

129 {
130 return orig_pix_;
131 }

◆ pageseg_split_strategy()

SplitStrategy tesseract::ShiroRekhaSplitter::pageseg_split_strategy ( ) const
inline

Definition at line 141 of file devanagari_processing.h.

141 {
142 return pageseg_split_strategy_;
143 }

◆ RefreshSegmentationWithNewBlobs()

void tesseract::ShiroRekhaSplitter::RefreshSegmentationWithNewBlobs ( C_BLOB_LIST *  new_blobs)

Definition at line 342 of file devanagari_processing.cpp.

342 {
343 // The segmentation block list must have been specified.
344 ASSERT_HOST(segmentation_block_list_);
346 tprintf("Before refreshing blobs:\n");
347 PrintSegmentationStats(segmentation_block_list_);
348 tprintf("New Blobs found: %d\n", new_blobs->length());
349 }
350
351 C_BLOB_LIST not_found_blobs;
353 segmentation_block_list_, new_blobs,
354 ((devanagari_split_debugimage && debug_image_) ? &not_found_blobs : nullptr));
355
357 tprintf("After refreshing blobs:\n");
358 PrintSegmentationStats(segmentation_block_list_);
359 }
360 if (devanagari_split_debugimage && debug_image_) {
361 // Plot out the original blobs for which no match was found in the new
362 // all_blobs list.
363 C_BLOB_IT not_found_it(&not_found_blobs);
364 for (not_found_it.mark_cycle_pt(); !not_found_it.cycled_list(); not_found_it.forward()) {
365 C_BLOB *not_found = not_found_it.data();
366 TBOX not_found_box = not_found->bounding_box();
367 Box *box_to_plot = GetBoxForTBOX(not_found_box);
368 pixRenderBoxArb(debug_image_, box_to_plot, 1, 255, 0, 255);
369 boxDestroy(&box_to_plot);
370 }
371
372 // Plot out the blobs unused from all blobs.
373 C_BLOB_IT all_blobs_it(new_blobs);
374 for (all_blobs_it.mark_cycle_pt(); !all_blobs_it.cycled_list(); all_blobs_it.forward()) {
375 C_BLOB *a_blob = all_blobs_it.data();
376 Box *box_to_plot = GetBoxForTBOX(a_blob->bounding_box());
377 pixRenderBoxArb(debug_image_, box_to_plot, 3, 0, 127, 0);
378 boxDestroy(&box_to_plot);
379 }
380 }
381}
#define ASSERT_HOST(x)
Definition: errcode.h:54
@ TBOX
bool devanagari_split_debugimage
void tprintf(const char *format,...)
Definition: tprintf.cpp:41
void RefreshWordBlobsFromNewBlobs(BLOCK_LIST *block_list, C_BLOB_LIST *new_blobs, C_BLOB_LIST *not_found_blobs)
Definition: ocrblock.cpp:474
void PrintSegmentationStats(BLOCK_LIST *block_list)
Definition: ocrblock.cpp:407

◆ segmentation_block_list()

BLOCK_LIST * tesseract::ShiroRekhaSplitter::segmentation_block_list ( )
inline

Definition at line 149 of file devanagari_processing.h.

149 {
150 return segmentation_block_list_;
151 }

◆ set_global_xheight()

void tesseract::ShiroRekhaSplitter::set_global_xheight ( int  xheight)
inline

Definition at line 109 of file devanagari_processing.h.

109 {
110 global_xheight_ = xheight;
111 }

◆ set_ocr_split_strategy()

void tesseract::ShiroRekhaSplitter::set_ocr_split_strategy ( SplitStrategy  strategy)
inline

Definition at line 137 of file devanagari_processing.h.

137 {
138 ocr_split_strategy_ = strategy;
139 }

◆ set_orig_pix()

void tesseract::ShiroRekhaSplitter::set_orig_pix ( Image  pix)

Definition at line 68 of file devanagari_processing.cpp.

68 {
69 if (orig_pix_) {
70 orig_pix_.destroy();
71 }
72 orig_pix_ = pix.clone();
73}
Image clone() const
Definition: image.cpp:24

◆ set_pageseg_split_strategy()

void tesseract::ShiroRekhaSplitter::set_pageseg_split_strategy ( SplitStrategy  strategy)
inline

Definition at line 145 of file devanagari_processing.h.

145 {
146 pageseg_split_strategy_ = strategy;
147 }

◆ set_perform_close()

void tesseract::ShiroRekhaSplitter::set_perform_close ( bool  perform)
inline

Definition at line 113 of file devanagari_processing.h.

113 {
114 perform_close_ = perform;
115 }

◆ set_segmentation_block_list()

void tesseract::ShiroRekhaSplitter::set_segmentation_block_list ( BLOCK_LIST *  block_list)
inline

Definition at line 103 of file devanagari_processing.h.

103 {
104 segmentation_block_list_ = block_list;
105 }

◆ Split()

bool tesseract::ShiroRekhaSplitter::Split ( bool  split_for_pageseg,
DebugPixa pixa_debug 
)

Definition at line 80 of file devanagari_processing.cpp.

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

◆ splitted_image()

Image tesseract::ShiroRekhaSplitter::splitted_image ( )
inline

Definition at line 120 of file devanagari_processing.h.

120 {
121 return splitted_image_;
122 }

Member Data Documentation

◆ kUnspecifiedXheight

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

Definition at line 107 of file devanagari_processing.h.


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