Back to index

texmacs  1.0.7.15
recognize_handwriting.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : recognize_handwriting.cpp
00004 * DESCRIPTION: Recognition of handwriting
00005 * COPYRIGHT  : (C) 2012  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #include "handwriting.hpp"
00013 
00014 /******************************************************************************
00015 * Recognize one glyph
00016 ******************************************************************************/
00017 
00018 void
00019 recognize_glyph_one (contours gl, int& level, string& best, double& best_rec) {
00020   array<tree>   disc1;
00021   array<double> cont1;
00022   invariants (gl, 1, disc1, cont1);
00023   array<tree>   disc2;
00024   array<double> cont2;
00025   invariants (gl, 2, disc2, cont2);
00026 
00027   best= "";
00028   best_rec= -100.0;
00029   int    best_i= -1;
00030   for (int i=0; i<N(learned_names); i++)
00031     if (N(learned_glyphs[i]) == N(gl) && disc1 == learned_disc1[i]) {
00032       string        name= learned_names[i];
00033       array<double> cont= learned_cont1[i];
00034       double        dist= l2_norm (cont - cont1) / sqrt (N(cont1));
00035       double        rec = 1.0 - dist;
00036       if (rec > best_rec) { best_rec= rec; best= name; best_i= i; }
00037       //cout << name << ": " << 100.0 * rec << "%\n";
00038     }
00039   if (best != "") {
00040     //cout << "disc= " << disc1 << "\n";
00041     //cout << "cont= " << cont1 << "\n";
00042     level= 1;
00043     return;
00044   }
00045 
00046   for (int i=0; i<N(learned_names); i++)
00047     if (N(learned_glyphs[i]) == N(gl) && disc2 == learned_disc2[i]) {
00048       string        name= learned_names[i];
00049       array<double> cont= learned_cont2[i];
00050       double        dist= l2_norm (cont - cont2) / sqrt (N(cont2));
00051       double        rec = 1.0 - dist;
00052       if (rec > best_rec) { best_rec= rec; best= name; }
00053       //cout << name << ": " << 100.0 * rec << "%\n";
00054     }
00055   level= 2;
00056 }
00057 
00058 /******************************************************************************
00059 * Recognize several glyphs
00060 ******************************************************************************/
00061 
00062 bool
00063 attached (poly_line pl1, poly_line pl2) {
00064   point p1= inf (pl1), q1= sup (pl1);
00065   point p2= inf (pl2), q2= sup (pl2);
00066   //cout << "<< " << p1 << ", " << q1 << "\n";
00067   //cout << ">> " << p2 << ", " << q2 << "\n";
00068   if (p2[1] > q1[1]) return true;
00069   if (p2[0] > q1[0]) return false;
00070   return true;
00071 }
00072 
00073 string
00074 recognize_glyph (contours gl) {
00075   string r;
00076   for (int i=0; i<N(gl); ) {
00077     int eat= 1;
00078     while (i+eat < N(gl) && attached (gl[i+eat-1], gl[i+eat])) eat++;
00079     int    lev= 3;
00080     string subr= "";
00081     double rec= -100.0;
00082     recognize_glyph_one (range (gl, i, i+eat), lev, subr, rec);
00083     r << subr;
00084     i += eat;
00085     //cout << "Add " << eat << "\n";
00086   }
00087   return r;
00088 }