tesseract v5.3.3.20231005
tesseract::BoxWord Class Reference

#include <boxword.h>

Public Member Functions

 BoxWord ()
 
 BoxWord (const BoxWord &src)
 
 ~BoxWord ()=default
 
BoxWordoperator= (const BoxWord &src)
 
void CopyFrom (const BoxWord &src)
 
void ClipToOriginalWord (const BLOCK *block, WERD *original_word)
 
void MergeBoxes (unsigned start, unsigned end)
 
void InsertBox (unsigned index, const TBOX &box)
 
void ChangeBox (unsigned index, const TBOX &box)
 
void DeleteBox (unsigned index)
 
void DeleteAllBoxes ()
 
void ProcessMatchedBlobs (const TWERD &other, const std::function< void(int)> &cb) const
 
const TBOXbounding_box () const
 
unsigned length () const
 
const TBOXBlobBox (unsigned index) const
 

Static Public Member Functions

static BoxWordCopyFromNormalized (TWERD *tessword)
 

Detailed Description

Definition at line 34 of file boxword.h.

Constructor & Destructor Documentation

◆ BoxWord() [1/2]

tesseract::BoxWord::BoxWord ( )

Definition at line 33 of file boxword.cpp.

33: length_(0) {}

◆ BoxWord() [2/2]

tesseract::BoxWord::BoxWord ( const BoxWord src)
explicit

Definition at line 35 of file boxword.cpp.

35 {
36 CopyFrom(src);
37}
void CopyFrom(const BoxWord &src)
Definition: boxword.cpp:44

◆ ~BoxWord()

tesseract::BoxWord::~BoxWord ( )
default

Member Function Documentation

◆ BlobBox()

const TBOX & tesseract::BoxWord::BlobBox ( unsigned  index) const
inline

Definition at line 84 of file boxword.h.

84 {
85 return boxes_[index];
86 }

◆ bounding_box()

const TBOX & tesseract::BoxWord::bounding_box ( ) const
inline

Definition at line 78 of file boxword.h.

78 {
79 return bbox_;
80 }

◆ ChangeBox()

void tesseract::BoxWord::ChangeBox ( unsigned  index,
const TBOX box 
)

Definition at line 169 of file boxword.cpp.

169 {
170 boxes_[index] = box;
171 ComputeBoundingBox();
172}

◆ ClipToOriginalWord()

void tesseract::BoxWord::ClipToOriginalWord ( const BLOCK block,
WERD original_word 
)

Definition at line 92 of file boxword.cpp.

92 {
93 for (unsigned i = 0; i < length_; ++i) {
94 TBOX box = boxes_[i];
95 // Expand by a single pixel, as the poly approximation error is 1 pixel.
96 box =
97 TBOX(box.left() - 1, box.bottom() - 1, box.right() + 1, box.top() + 1);
98 // Now find the original box that matches.
99 TBOX original_box;
100 C_BLOB_IT b_it(original_word->cblob_list());
101 for (b_it.mark_cycle_pt(); !b_it.cycled_list(); b_it.forward()) {
102 TBOX blob_box = b_it.data()->bounding_box();
103 if (block != nullptr) {
104 blob_box.rotate(block->re_rotation());
105 }
106 if (blob_box.major_overlap(box)) {
107 original_box += blob_box;
108 }
109 }
110 if (!original_box.null_box()) {
111 if (NearlyEqual<int>(original_box.left(), box.left(),
113 box.set_left(original_box.left());
114 }
115 if (NearlyEqual<int>(original_box.right(), box.right(),
117 box.set_right(original_box.right());
118 }
119 if (NearlyEqual<int>(original_box.top(), box.top(), kBoxClipTolerance)) {
120 box.set_top(original_box.top());
121 }
122 if (NearlyEqual<int>(original_box.bottom(), box.bottom(),
124 box.set_bottom(original_box.bottom());
125 }
126 }
127 original_box = original_word->bounding_box();
128 if (block != nullptr) {
129 original_box.rotate(block->re_rotation());
130 }
131 boxes_[i] = box.intersection(original_box);
132 }
133 ComputeBoundingBox();
134}
@ TBOX
const int kBoxClipTolerance
Definition: boxword.cpp:31

◆ CopyFrom()

void tesseract::BoxWord::CopyFrom ( const BoxWord src)

Definition at line 44 of file boxword.cpp.

44 {
45 bbox_ = src.bbox_;
46 length_ = src.length_;
47 boxes_.clear();
48 boxes_.reserve(length_);
49 for (unsigned i = 0; i < length_; ++i) {
50 boxes_.push_back(src.boxes_[i]);
51 }
52}

◆ CopyFromNormalized()

BoxWord * tesseract::BoxWord::CopyFromNormalized ( TWERD tessword)
static

Definition at line 56 of file boxword.cpp.

56 {
57 auto *boxword = new BoxWord();
58 // Count the blobs.
59 boxword->length_ = tessword->NumBlobs();
60 // Allocate memory.
61 boxword->boxes_.reserve(boxword->length_);
62
63 for (unsigned b = 0; b < boxword->length_; ++b) {
64 TBLOB *tblob = tessword->blobs[b];
65 TBOX blob_box;
66 for (TESSLINE *outline = tblob->outlines; outline != nullptr;
67 outline = outline->next) {
68 EDGEPT *edgept = outline->loop;
69 // Iterate over the edges.
70 do {
71 if (!edgept->IsHidden() || !edgept->prev->IsHidden()) {
72 ICOORD pos(edgept->pos.x, edgept->pos.y);
73 TPOINT denormed;
74 tblob->denorm().DenormTransform(nullptr, edgept->pos, &denormed);
75 pos.set_x(denormed.x);
76 pos.set_y(denormed.y);
77 TBOX pt_box(pos, pos);
78 blob_box += pt_box;
79 }
80 edgept = edgept->next;
81 } while (edgept != outline->loop);
82 }
83 boxword->boxes_.push_back(blob_box);
84 }
85 boxword->ComputeBoundingBox();
86 return boxword;
87}
@ TPOINT

◆ DeleteAllBoxes()

void tesseract::BoxWord::DeleteAllBoxes ( )

Definition at line 184 of file boxword.cpp.

184 {
185 length_ = 0;
186 boxes_.clear();
187 bbox_ = TBOX();
188}

◆ DeleteBox()

void tesseract::BoxWord::DeleteBox ( unsigned  index)

Definition at line 176 of file boxword.cpp.

176 {
177 ASSERT_HOST(index < length_);
178 boxes_.erase(boxes_.begin() + index);
179 --length_;
180 ComputeBoundingBox();
181}
#define ASSERT_HOST(x)
Definition: errcode.h:54

◆ InsertBox()

void tesseract::BoxWord::InsertBox ( unsigned  index,
const TBOX box 
)

Definition at line 157 of file boxword.cpp.

157 {
158 if (index < length_) {
159 boxes_.insert(boxes_.begin() + index, box);
160 } else {
161 boxes_.push_back(box);
162 }
163 length_ = boxes_.size();
164 ComputeBoundingBox();
165}

◆ length()

unsigned tesseract::BoxWord::length ( ) const
inline

Definition at line 81 of file boxword.h.

81 {
82 return length_;
83 }

◆ MergeBoxes()

void tesseract::BoxWord::MergeBoxes ( unsigned  start,
unsigned  end 
)

Definition at line 138 of file boxword.cpp.

138 {
139 start = ClipToRange(start, 0U, length_);
140 end = ClipToRange(end, 0U, length_);
141 if (end <= start + 1) {
142 return;
143 }
144 for (unsigned i = start + 1; i < end; ++i) {
145 boxes_[start] += boxes_[i];
146 }
147 int shrinkage = end - 1 - start;
148 length_ -= shrinkage;
149 for (unsigned i = start + 1; i < length_; ++i) {
150 boxes_[i] = boxes_[i + shrinkage];
151 }
152 boxes_.resize(length_);
153}
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:105

◆ operator=()

BoxWord & tesseract::BoxWord::operator= ( const BoxWord src)

Definition at line 39 of file boxword.cpp.

39 {
40 CopyFrom(src);
41 return *this;
42}

◆ ProcessMatchedBlobs()

void tesseract::BoxWord::ProcessMatchedBlobs ( const TWERD other,
const std::function< void(int)> &  cb 
) const

Definition at line 201 of file boxword.cpp.

202 {
203 for (unsigned i = 0; i < length_ && i < other.NumBlobs(); ++i) {
204 TBOX blob_box = other.blobs[i]->bounding_box();
205 if (blob_box == boxes_[i]) {
206 cb(i);
207 }
208 }
209}

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