tesseract  3.03
/usr/local/google/home/jbreiden/tesseract-ocr-read-only/training/boxchar.cpp
Go to the documentation of this file.
00001 /**********************************************************************
00002  * File:        boxchar.cpp
00003  * Description: Simple class to associate a Tesseract classification unit with
00004  *              its bounding box so that the boxes can be rotated as the image
00005  *              is rotated for degradation.  Also includes routines to output
00006  *              the character-tagged boxes to a boxfile.
00007  * Author:      Ray Smith
00008  * Created:     Mon Nov 18 2013
00009  *
00010  * (C) Copyright 2013, Google Inc.
00011  * Licensed under the Apache License, Version 2.0 (the "License");
00012  * you may not use this file except in compliance with the License.
00013  * You may obtain a copy of the License at
00014  * http://www.apache.org/licenses/LICENSE-2.0
00015  * Unless required by applicable law or agreed to in writing, software
00016  * distributed under the License is distributed on an "AS IS" BASIS,
00017  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00018  * See the License for the specific language governing permissions and
00019  * limitations under the License.
00020  *
00021  **********************************************************************/
00022 
00023 #include "boxchar.h"
00024 
00025 #include <stddef.h>
00026 
00027 #include "fileio.h"
00028 
00029 namespace tesseract {
00030 
00031 BoxChar::BoxChar(const char* utf8_str, int len) : ch_(utf8_str, len) {
00032   box_ = NULL;
00033 }
00034 
00035 BoxChar::~BoxChar() {
00036   boxDestroy(&box_);
00037 }
00038 
00039 void BoxChar::AddBox(int x, int y, int width, int height) {
00040   box_ = boxCreate(x, y, width, height);
00041 }
00042 
00043 /* static */
00044 void BoxChar::TranslateBoxes(int xshift, int yshift,
00045                              vector<BoxChar*>* boxes) {
00046   for (int i = 0; i < boxes->size(); ++i) {
00047     BOX* box = (*boxes)[i]->box_;
00048     if (box != NULL) {
00049       box->x += xshift;
00050       box->y += yshift;
00051     }
00052   }
00053 }
00054 
00055 // Rotate the boxes in [start_box, end_box) by the given rotation.
00056 // The rotation is in radians clockwise about the given center.
00057 /* static */
00058 void BoxChar::RotateBoxes(float rotation,
00059                           int xcenter,
00060                           int ycenter,
00061                           int start_box,
00062                           int end_box,
00063                           vector<BoxChar*>* boxes) {
00064   Boxa* orig = boxaCreate(0);
00065   for (int i = start_box; i < end_box; ++i) {
00066     BOX* box = (*boxes)[i]->box_;
00067     if (box) boxaAddBox(orig, box, L_CLONE);
00068   }
00069   Boxa* rotated = boxaRotate(orig, xcenter, ycenter, rotation);
00070   boxaDestroy(&orig);
00071   for (int i = start_box, box_ind = 0; i < end_box; ++i) {
00072     if ((*boxes)[i]->box_) {
00073       boxDestroy(&((*boxes)[i]->box_));
00074       (*boxes)[i]->box_ = boxaGetBox(rotated, box_ind++, L_CLONE);
00075     }
00076   }
00077   boxaDestroy(&rotated);
00078 }
00079 
00080 /* static */
00081 void BoxChar::WriteTesseractBoxFile(const string& filename, int height,
00082                                     const vector<BoxChar*>& boxes) {
00083   string output;
00084   const int kMaxLineLength = 1024;
00085   char buffer[kMaxLineLength];
00086   for (int i = 0; i < boxes.size(); ++i) {
00087     if (boxes[i]->box_ != NULL) {
00088       int nbytes = snprintf(buffer, kMaxLineLength,
00089                             "%s %d %d %d %d %d\n",
00090                             boxes[i]->ch_.c_str(),
00091                             boxes[i]->box_->x,
00092                             height - boxes[i]->box_->y - boxes[i]->box_->h,
00093                             boxes[i]->box_->x + boxes[i]->box_->w,
00094                             height - boxes[i]->box_->y,
00095                             boxes[i]->page_);
00096       output.append(buffer, nbytes);
00097     }
00098   }
00099   File::WriteStringToFileOrDie(output, filename);
00100 }
00101 }  // namespace tesseract
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines