⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 topitch.cpp

📁 一OCR的相关资料。.希望对研究OCR的朋友有所帮助.
💻 CPP
📖 第 1 页 / 共 5 页
字号:
}/********************************************************************** * compute_pitch_sd * * Use a dp algorithm to fit the character cells and return the sd of * the cell size over the row. **********************************************************************/float compute_pitch_sd(                            //find fp cells                       TO_ROW *row,                //row to do                       STATS *projection,          //vertical projection                       INT16 projection_left,      //edge                       INT16 projection_right,     //edge                       float space_size,           //size of blank                       float initial_pitch,        //guess at pitch                       float &sp_sd,               //space sd                       INT16 &mid_cuts,            //no of free cuts                       ICOORDELT_LIST *row_cells,  //list of chop pts                       BOOL8 testing_on,           //inidividual words                       INT16 start,                //start of good range                       INT16 end                   //end of good range                      ) {  INT16 occupation;              //no of cells in word.                                 //blobs  BLOBNBOX_IT blob_it = row->blob_list ();  BLOBNBOX_IT start_it;          //start of word  BLOBNBOX_IT plot_it;           //for plotting  INT16 blob_count;              //no of blobs  BOX blob_box;                  //bounding box  BOX prev_box;                  //of super blob  INT32 prev_right;              //of word sync  int scale_factor;              //on scores for big words  INT32 sp_count;                //spaces  FPSEGPT_LIST seg_list;         //char cells  FPSEGPT_IT seg_it;             //iterator  INT16 segpos;                  //position of segment  INT16 cellpos;                 //previous cell boundary                                 //iterator  ICOORDELT_IT cell_it = row_cells;  ICOORDELT *cell;               //new cell  double sqsum;                  //sum of squares  double spsum;                  //of spaces  double sp_var;                 //space error  double word_sync;              //result for word  INT32 total_count;             //total blobs  if ((pitsync_linear_version & 3) > 1) {    word_sync = compute_pitch_sd2 (row, projection, projection_left,      projection_right, initial_pitch,      occupation, mid_cuts, row_cells,      testing_on, start, end);    sp_sd = occupation;    return word_sync;  }  mid_cuts = 0;  cellpos = 0;  total_count = 0;  sqsum = 0;  sp_count = 0;  spsum = 0;  prev_right = -1;  if (blob_it.empty ())    return space_size * 10;#ifndef GRAPHICS_DISABLED  if (testing_on && to_win > 0) {    blob_box = blob_it.data ()->bounding_box ();    projection->plot (to_win, projection_left,      row->intercept (), 1.0f, -1.0f, CORAL);  }#endif  start_it = blob_it;  blob_count = 0;  blob_box = box_next (&blob_it);//first blob  blob_it.mark_cycle_pt ();  do {    for (; blob_count > 0; blob_count--)      box_next(&start_it);    do {      prev_box = blob_box;      blob_count++;      blob_box = box_next (&blob_it);    }    while (!blob_it.cycled_list ()      && blob_box.left () - prev_box.right () < space_size);    plot_it = start_it;    if (pitsync_linear_version & 3)      word_sync =        check_pitch_sync2 (&start_it, blob_count, (INT16) initial_pitch, 2,        projection, projection_left, projection_right,        row->xheight * textord_projection_scale,        occupation, &seg_list, start, end);    else      word_sync =        check_pitch_sync (&start_it, blob_count, (INT16) initial_pitch, 2,        projection, &seg_list);    if (testing_on) {      tprintf ("Word ending at (%d,%d), len=%d, sync rating=%g, ",        prev_box.right (), prev_box.top (),        seg_list.length () - 1, word_sync);      seg_it.set_to_list (&seg_list);      for (seg_it.mark_cycle_pt (); !seg_it.cycled_list ();      seg_it.forward ()) {        if (seg_it.data ()->faked)          tprintf ("(F)");        tprintf ("%d, ", seg_it.data ()->position ());        //                              tprintf("C=%g, s=%g, sq=%g\n",        //                                      seg_it.data()->cost_function(),        //                                      seg_it.data()->sum(),        //                                      seg_it.data()->squares());      }      tprintf ("\n");    }#ifndef GRAPHICS_DISABLED    if (textord_show_fixed_cuts && blob_count > 0 && to_win > 0)      plot_fp_cells2(to_win, GOLDENROD, row, &seg_list);#endif    seg_it.set_to_list (&seg_list);    if (prev_right >= 0) {      sp_var = seg_it.data ()->position () - prev_right;      sp_var -= floor (sp_var / initial_pitch + 0.5) * initial_pitch;      sp_var *= sp_var;      spsum += sp_var;      sp_count++;    }    for (seg_it.mark_cycle_pt (); !seg_it.cycled_list (); seg_it.forward ()) {      segpos = seg_it.data ()->position ();      if (cell_it.empty () || segpos > cellpos + initial_pitch / 2) {                                 //big gap        while (!cell_it.empty () && segpos > cellpos + initial_pitch * 3 / 2) {          cell = new ICOORDELT (cellpos + (INT16) initial_pitch, 0);          cell_it.add_after_then_move (cell);          cellpos += (INT16) initial_pitch;        }                                 //make new one        cell = new ICOORDELT (segpos, 0);        cell_it.add_after_then_move (cell);        cellpos = segpos;      }      else if (segpos > cellpos - initial_pitch / 2) {        cell = cell_it.data ();                                 //average positions        cell->set_x ((cellpos + segpos) / 2);        cellpos = cell->x ();      }    }    seg_it.move_to_last ();    prev_right = seg_it.data ()->position ();    if (textord_pitch_scalebigwords) {      scale_factor = (seg_list.length () - 2) / 2;      if (scale_factor < 1)        scale_factor = 1;    }    else      scale_factor = 1;    sqsum += word_sync * scale_factor;    total_count += (seg_list.length () - 1) * scale_factor;    seg_list.clear ();  }  while (!blob_it.cycled_list ());  sp_sd = sp_count > 0 ? sqrt (spsum / sp_count) : 0;  return total_count > 0 ? sqrt (sqsum / total_count) : space_size * 10;}/********************************************************************** * compute_pitch_sd2 * * Use a dp algorithm to fit the character cells and return the sd of * the cell size over the row. **********************************************************************/float compute_pitch_sd2(                            //find fp cells                        TO_ROW *row,                //row to do                        STATS *projection,          //vertical projection                        INT16 projection_left,      //edge                        INT16 projection_right,     //edge                        float initial_pitch,        //guess at pitch                        INT16 &occupation,          //no of occupied cells                        INT16 &mid_cuts,            //no of free cuts                        ICOORDELT_LIST *row_cells,  //list of chop pts                        BOOL8 testing_on,           //inidividual words                        INT16 start,                //start of good range                        INT16 end                   //end of good range                       ) {                                 //blobs  BLOBNBOX_IT blob_it = row->blob_list ();  BLOBNBOX_IT plot_it;  INT16 blob_count;              //no of blobs  BOX blob_box;                  //bounding box  FPSEGPT_LIST seg_list;         //char cells  FPSEGPT_IT seg_it;             //iterator  INT16 segpos;                  //position of segment                                 //iterator  ICOORDELT_IT cell_it = row_cells;  ICOORDELT *cell;               //new cell  double word_sync;              //result for word  mid_cuts = 0;  if (blob_it.empty ()) {    occupation = 0;    return initial_pitch * 10;  }#ifndef GRAPHICS_DISABLED  if (testing_on && to_win > 0) {    projection->plot (to_win, projection_left,      row->intercept (), 1.0f, -1.0f, CORAL);  }#endif  blob_count = 0;  blob_it.mark_cycle_pt ();  do {                                 //first blob    blob_box = box_next (&blob_it);    blob_count++;  }  while (!blob_it.cycled_list ());  plot_it = blob_it;  word_sync = check_pitch_sync2 (&blob_it, blob_count, (INT16) initial_pitch,    2, projection, projection_left,    projection_right,    row->xheight * textord_projection_scale,    occupation, &seg_list, start, end);  if (testing_on) {    tprintf ("Row ending at (%d,%d), len=%d, sync rating=%g, ",      blob_box.right (), blob_box.top (),      seg_list.length () - 1, word_sync);    seg_it.set_to_list (&seg_list);    for (seg_it.mark_cycle_pt (); !seg_it.cycled_list (); seg_it.forward ()) {      if (seg_it.data ()->faked)        tprintf ("(F)");      tprintf ("%d, ", seg_it.data ()->position ());      //                              tprintf("C=%g, s=%g, sq=%g\n",      //                                      seg_it.data()->cost_function(),      //                                      seg_it.data()->sum(),      //                                      seg_it.data()->squares());    }    tprintf ("\n");  }#ifndef GRAPHICS_DISABLED  if (textord_show_fixed_cuts && blob_count > 0 && to_win > 0)    plot_fp_cells2(to_win, GOLDENROD, row, &seg_list);#endif  seg_it.set_to_list (&seg_list);  for (seg_it.mark_cycle_pt (); !seg_it.cycled_list (); seg_it.forward ()) {    segpos = seg_it.data ()->position ();                                 //make new one    cell = new ICOORDELT (segpos, 0);    cell_it.add_after_then_move (cell);    if (seg_it.at_last ())      mid_cuts = seg_it.data ()->cheap_cuts ();  }  seg_list.clear ();  return occupation > 0 ? sqrt (word_sync / occupation) : initial_pitch * 10;}/********************************************************************** * print_pitch_sd * * Use a dp algorithm to fit the character cells and return the sd of * the cell size over the row. **********************************************************************/void print_pitch_sd(                        //find fp cells                    TO_ROW *row,            //row to do                    STATS *projection,      //vertical projection                    INT16 projection_left,  //edges //size of blank                    INT16 projection_right,                    float space_size,                    float initial_pitch     //guess at pitch                   ) {  const char *res2;              //pitch result  INT16 occupation;              //used cells  float sp_sd;                   //space sd                                 //blobs  BLOBNBOX_IT blob_it = row->blob_list ();  BLOBNBOX_IT start_it;          //start of word  BLOBNBOX_IT row_start;         //start of row  INT16 blob_count;              //no of blobs  INT16 total_blob_count;        //total blobs in line  BOX blob_box;                  //bounding box  BOX prev_box;                  //of super blob  INT32 prev_right;              //of word sync  int scale_factor;              //on scores for big words  INT32 sp_count;                //spaces  FPSEGPT_LIST seg_list;         //char cells  FPSEGPT_IT seg_it;             //iterator  double sqsum;                  //sum of squares  double spsum;                  //of spaces  double sp_var;                 //space error  double word_sync;              //result for word  double total_count;            //total cuts  if (blob_it.empty ())    return;  row_start = blob_it;  total_blob_count = 0;  total_count = 0;  sqsum = 0;  sp_count = 0;  spsum = 0;  prev_right = -1;  blob_it = row_start;  start_it = blob_it;  blob_count = 0;  blob_box = box_next (&blob_it);//first blob  blob_it.mark_cycle_pt ();  do {    for (; blob_count > 0; blob_count--)      box_next(&start_it);    do {      prev_box = blob_box;      blob_count++;      blob_box = box_next (&blob_it);    }    while (!blob_it.cycled_list ()      && blob_box.left () - prev_box.right () < space_size);    word_sync =      check_pitch_sync2 (&start_it, blob_count, (INT16) initial_pitch, 2,      projection, projection_left, projection_right,      row->xheight * textord_projection_scale,      occupation, &seg_list, 0, 0);    total_blob_count += blob_count;    seg_it.set_to_list (&seg_list);    if (prev_right >= 0) {      sp_var = seg_it.data ()->position () - prev_right;      sp_var -= floor (sp_var / initial_pitch + 0.5) * initial_pitch;      sp_var *= sp_var;      spsum += sp_var;      sp_count++;    }    seg_it.move_to_last ();    prev_right = seg_it.data ()->position ();    if (textord_pitch_scalebigwords) {      scale_factor = (seg_list.length () - 2) / 2;      if (scale_factor < 1)        scale_factor = 1;    }    else      scale_factor = 1;    sqsum += word_sync * scale_factor;    total_count += (seg_list.length () - 1) * scale_factor;    seg_list.clear ();  }  while (!blob_it.cycled_list ());  sp_sd = sp

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -