1
/**********************************************************************
3
* Description: Simple class to associate a Tesseract classification unit with
4
* its bounding box so that the boxes can be rotated as the image
5
* is rotated for degradation. Also includes routines to output
6
* the character-tagged boxes to a boxfile.
8
* Created: Mon Nov 18 2013
10
* (C) Copyright 2013, Google Inc.
11
* Licensed under the Apache License, Version 2.0 (the "License");
12
* you may not use this file except in compliance with the License.
13
* You may obtain a copy of the License at
14
* http://www.apache.org/licenses/LICENSE-2.0
15
* Unless required by applicable law or agreed to in writing, software
16
* distributed under the License is distributed on an "AS IS" BASIS,
17
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
* See the License for the specific language governing permissions and
19
* limitations under the License.
21
**********************************************************************/
31
BoxChar::BoxChar(const char* utf8_str, int len) : ch_(utf8_str, len) {
39
void BoxChar::AddBox(int x, int y, int width, int height) {
40
box_ = boxCreate(x, y, width, height);
44
void BoxChar::TranslateBoxes(int xshift, int yshift,
45
vector<BoxChar*>* boxes) {
46
for (int i = 0; i < boxes->size(); ++i) {
47
BOX* box = (*boxes)[i]->box_;
55
// Rotate the boxes in [start_box, end_box) by the given rotation.
56
// The rotation is in radians clockwise about the given center.
58
void BoxChar::RotateBoxes(float rotation,
63
vector<BoxChar*>* boxes) {
64
Boxa* orig = boxaCreate(0);
65
for (int i = start_box; i < end_box; ++i) {
66
BOX* box = (*boxes)[i]->box_;
67
if (box) boxaAddBox(orig, box, L_CLONE);
69
Boxa* rotated = boxaRotate(orig, xcenter, ycenter, rotation);
71
for (int i = start_box, box_ind = 0; i < end_box; ++i) {
72
if ((*boxes)[i]->box_) {
73
boxDestroy(&((*boxes)[i]->box_));
74
(*boxes)[i]->box_ = boxaGetBox(rotated, box_ind++, L_CLONE);
77
boxaDestroy(&rotated);
81
void BoxChar::WriteTesseractBoxFile(const string& filename, int height,
82
const vector<BoxChar*>& boxes) {
84
const int kMaxLineLength = 1024;
85
char buffer[kMaxLineLength];
86
for (int i = 0; i < boxes.size(); ++i) {
87
if (boxes[i]->box_ != NULL) {
88
int nbytes = snprintf(buffer, kMaxLineLength,
89
"%s %d %d %d %d %d\n",
90
boxes[i]->ch_.c_str(),
92
height - boxes[i]->box_->y - boxes[i]->box_->h,
93
boxes[i]->box_->x + boxes[i]->box_->w,
94
height - boxes[i]->box_->y,
96
output.append(buffer, nbytes);
99
File::WriteStringToFileOrDie(output, filename);
101
} // namespace tesseract