tesseract 3.04.01

training/boxchar.h

Go to the documentation of this file.
00001 /**********************************************************************
00002  * File:        boxchar.h
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 #ifndef TESSERACT_TRAINING_BOXCHAR_H_
00024 #define TESSERACT_TRAINING_BOXCHAR_H_
00025 
00026 #include <string>
00027 #include <vector>
00028 
00029 #include "allheaders.h"  // from Leptonica
00030 
00031 #ifdef USE_STD_NAMESPACE
00032 using std::string;
00033 using std::vector;
00034 #endif
00035 
00036 struct Box;
00037 
00038 namespace tesseract {
00039 
00040 class BoxChar {
00041  public:
00042   BoxChar(const char* utf8_str, int len);
00043 
00044   ~BoxChar();
00045 
00046   // Accessors.
00047   const string& ch() const { return ch_; }
00048   const Box* box() const   { return box_; }
00049   const int& page() const  { return page_; }
00050 
00051 
00052   // Set the box_ member.
00053   void AddBox(int x, int y, int width, int height);
00054 
00055   void set_page(int page) { page_ = page; }
00056 
00057   string* mutable_ch() { return &ch_; }
00058   Box* mutable_box()   { return box_; }
00059 
00060   // Sort function for sorting by left edge of box. Note that this will not
00061   // work properly until after InsertNewlines and InsertSpaces.
00062   bool operator<(const BoxChar& other) const {
00063     if (box_ == NULL) return true;
00064     if (other.box_ == NULL) return false;
00065     return box_->x < other.box_->x;
00066   }
00067 
00068   static void TranslateBoxes(int xshift, int yshift,
00069                              vector<BoxChar*>* boxes);
00070 
00071   // Prepares for writing the boxes to a file by inserting newlines, spaces,
00072   // and re-ordering so the boxes are strictly left-to-right.
00073   static void PrepareToWrite(vector<BoxChar*>* boxes);
00074   // Inserts newline (tab) characters into the vector at newline positions.
00075   static void InsertNewlines(bool rtl_rules, bool vertical_rules,
00076                              vector<BoxChar*>* boxes);
00077   // Converts NULL boxes to space characters, with appropriate bounding boxes.
00078   static void InsertSpaces(bool rtl_rules, bool vertical_rules,
00079                            vector<BoxChar*>* boxes);
00080   // Reorders text in a right-to-left script in left-to-right order.
00081   static void ReorderRTLText(vector<BoxChar*>* boxes);
00082   // Returns true if the vector contains mostly RTL characters.
00083   static bool ContainsMostlyRTL(const vector<BoxChar*>& boxes);
00084   // Returns true if the text is mostly laid out vertically.
00085   static bool MostlyVertical(const vector<BoxChar*>& boxes);
00086 
00087   // Returns the total length of all the strings in the boxes.
00088   static int TotalByteLength(const vector<BoxChar*>& boxes);
00089 
00090   // Rotate the vector of boxes between start and end by the given rotation.
00091   // The rotation is in radians clockwise about the given center.
00092   static void RotateBoxes(float rotation,
00093                           int xcenter,
00094                           int ycenter,
00095                           int start_box,
00096                           int end_box,
00097                           vector<BoxChar*>* boxes);
00098 
00099   // Create a tesseract box file from the vector of boxes. The image height
00100   // is needed to convert to tesseract coordinates.
00101   static void WriteTesseractBoxFile(const string& name, int height,
00102                                     const vector<BoxChar*>& boxes);
00103 
00104  private:
00105   string ch_;
00106   Box* box_;
00107   int page_;
00108 };
00109 
00110 // Sort predicate to sort a vector of BoxChar*.
00111 struct BoxCharPtrSort {
00112   bool operator()(const BoxChar* box1, const BoxChar* box2) const {
00113     return *box1 < *box2;
00114   }
00115 };
00116 
00117 }  // namespace tesseract
00118 
00119 #endif  // TESSERACT_TRAINING_BOXCHAR_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines