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

📄 oldbasel.cpp

📁 一OCR的相关资料。.希望对研究OCR的朋友有所帮助.
💻 CPP
📖 第 1 页 / 共 5 页
字号:
  - MAXOVERLAP * (rightedge - leftedge)) {    *baseline = *spline;         /*copy it */    x = (leftedge + rightedge) / 2.0;    shift = ICOORD (0, (INT16) (gradient * x + c - spline->y (x)));    baseline->move (shift);  }}/********************************************************************** * partition_line * * Partition a row of blobs into different groups of continuous * y position. jumplimit specifies the max allowable limit on a jump * before a new partition is started. * The return value is the biggest partition **********************************************************************/intpartition_line (                 //partition blobsBOX blobcoords[],                //bounding boxesint blobcount,                   /*no of blobs on row */int *numparts,                   /*number of partitions */char partids[],                  /*partition no of each blob */int partsizes[],                 /*no in each partition */QSPLINE * spline,                /*curve to fit to */float jumplimit,                 /*allowed delta change */float ydiffs[]                   /*diff from spline */) {  register int blobindex;        /*no along text line */  int bestpart;                  /*best new partition */  int biggestpart;               /*part with most members */  float diff;                    /*difference from line */  int startx;                    /*index of start blob */  float partdiffs[MAXPARTS];     /*step between parts */  for (bestpart = 0; bestpart < MAXPARTS; bestpart++)    partsizes[bestpart] = 0;     /*zero them all */  startx = get_ydiffs (blobcoords, blobcount, spline, ydiffs);  *numparts = 1;                 /*1 partition */  bestpart = -1;                 /*first point */  for (blobindex = startx; blobindex < blobcount; blobindex++) {  /*do each blob in row */    diff = ydiffs[blobindex];    /*diff from line */    if (textord_oldbl_debug) {      tprintf ("%d(%d,%d), ", blobindex,        blobcoords[blobindex].left (),        blobcoords[blobindex].bottom ());    }    bestpart =      choose_partition(diff, partdiffs, bestpart, jumplimit, numparts);                                 /*record partition */    partids[blobindex] = bestpart;    partsizes[bestpart]++;       /*another in it */  }  bestpart = -1;                 /*first point */  partsizes[0]--;                /*doing 1st pt again */                                 /*do each blob in row */  for (blobindex = startx; blobindex >= 0; blobindex--) {    diff = ydiffs[blobindex];    /*diff from line */    if (textord_oldbl_debug) {      tprintf ("%d(%d,%d), ", blobindex,        blobcoords[blobindex].left (),        blobcoords[blobindex].bottom ());    }    bestpart =      choose_partition(diff, partdiffs, bestpart, jumplimit, numparts);                                 /*record partition */    partids[blobindex] = bestpart;    partsizes[bestpart]++;       /*another in it */  }  for (biggestpart = 0, bestpart = 1; bestpart < *numparts; bestpart++)    if (partsizes[bestpart] >= partsizes[biggestpart])      biggestpart = bestpart;    /*new biggest */  if (textord_oldbl_merge_parts)    merge_oldbl_parts(blobcoords,                      blobcount,                      partids,                      partsizes,                      biggestpart,                      jumplimit);  return biggestpart;            /*biggest partition */}/********************************************************************** * merge_oldbl_parts * * For any adjacent group of blobs in a different part, put them in the * main part if they fit closely to neighbours in the main part. **********************************************************************/voidmerge_oldbl_parts (              //partition blobsBOX blobcoords[],                //bounding boxesint blobcount,                   /*no of blobs on row */char partids[],                  /*partition no of each blob */int partsizes[],                 /*no in each partition */int biggestpart,                 //major partitionfloat jumplimit                  /*allowed delta change */) {  BOOL8 found_one;               //found a bestpart blob  BOOL8 close_one;               //found was close enough  register int blobindex;        /*no along text line */  int prevpart;                  //previous iteration  int runlength;                 //no in this part  float diff;                    /*difference from line */  int startx;                    /*index of start blob */  int test_blob;                 //another index  FCOORD coord;                  //blob coordinate  float m, c;                    //fitted line  QLSQ stats;                    //line stuff  prevpart = biggestpart;  runlength = 0;  startx = 0;  for (blobindex = 0; blobindex < blobcount; blobindex++) {    if (partids[blobindex] != prevpart) {      //                      tprintf("Partition change at (%d,%d) from %d to %d after run of %d\n",      //                              blobcoords[blobindex].left(),blobcoords[blobindex].bottom(),      //                              prevpart,partids[blobindex],runlength);      if (prevpart != biggestpart && runlength > MAXBADRUN) {        stats.clear ();        for (test_blob = startx; test_blob < blobindex; test_blob++) {          coord = FCOORD ((blobcoords[test_blob].left ()            + blobcoords[test_blob].right ()) / 2.0,            blobcoords[test_blob].bottom ());          stats.add (coord.x (), coord.y ());        }        stats.fit (1);        m = stats.get_b ();        c = stats.get_c ();        if (textord_oldbl_debug)          tprintf ("Fitted line y=%g x + %g\n", m, c);        found_one = FALSE;        close_one = FALSE;        for (test_blob = 1; !found_one          && (startx - test_blob >= 0        || blobindex + test_blob <= blobcount); test_blob++) {          if (startx - test_blob >= 0          && partids[startx - test_blob] == biggestpart) {            found_one = TRUE;            coord = FCOORD ((blobcoords[startx - test_blob].left ()              + blobcoords[startx -              test_blob].right ()) /              2.0,              blobcoords[startx -              test_blob].bottom ());            diff = m * coord.x () + c - coord.y ();            if (textord_oldbl_debug)              tprintf                ("Diff of common blob to suspect part=%g at (%g,%g)\n",                diff, coord.x (), coord.y ());            if (diff < jumplimit && -diff < jumplimit)              close_one = TRUE;          }          if (blobindex + test_blob <= blobcount          && partids[blobindex + test_blob - 1] == biggestpart) {            found_one = TRUE;            coord =              FCOORD ((blobcoords[blobindex + test_blob - 1].              left () + blobcoords[blobindex + test_blob -              1].right ()) / 2.0,              blobcoords[blobindex + test_blob -              1].bottom ());            diff = m * coord.x () + c - coord.y ();            if (textord_oldbl_debug)              tprintf                ("Diff of common blob to suspect part=%g at (%g,%g)\n",                diff, coord.x (), coord.y ());            if (diff < jumplimit && -diff < jumplimit)              close_one = TRUE;          }        }        if (close_one) {          if (textord_oldbl_debug)            tprintf              ("Merged %d blobs back into part %d from %d starting at (%d,%d)\n",              runlength, biggestpart, prevpart,              blobcoords[startx].left (),              blobcoords[startx].bottom ());                                 //switch sides          partsizes[prevpart] -= runlength;          for (test_blob = startx; test_blob < blobindex; test_blob++)            partids[test_blob] = biggestpart;        }      }      prevpart = partids[blobindex];      runlength = 1;      startx = blobindex;    }    else      runlength++;  }}/********************************************************************** * get_ydiffs * * Get the differences between the blobs and the spline, * putting them in ydiffs.  The return value is the index * of the blob in the middle of the "best behaved" region **********************************************************************/intget_ydiffs (                     //evaluate differencesBOX blobcoords[],                //bounding boxesint blobcount,                   /*no of blobs */QSPLINE * spline,                /*approximating spline */float ydiffs[]                   /*output */) {  register int blobindex;        /*current blob */  int xcentre;                   /*xcoord */  int lastx;                     /*last xcentre */  float diffsum;                 /*sum of diffs */  float diff;                    /*current difference */  float drift;                   /*sum of spline steps */  float bestsum;                 /*smallest diffsum */  int bestindex;                 /*index of bestsum */  diffsum = 0.0f;  bestindex = 0;  bestsum = (float) MAX_INT32;  drift = 0.0f;  lastx = blobcoords[0].left ();                                 /*do each blob in row */  for (blobindex = 0; blobindex < blobcount; blobindex++) {                                 /*centre of blob */    xcentre = (blobcoords[blobindex].left () + blobcoords[blobindex].right ()) >> 1;                                 //step functions in spline    drift += spline->step (lastx, xcentre);    lastx = xcentre;    diff = blobcoords[blobindex].bottom ();    diff -= spline->y (xcentre);    diff += drift;    ydiffs[blobindex] = diff;    /*store difference */    if (blobindex > 2)                                 /*remove old one */      diffsum -= ABS (ydiffs[blobindex - 3]);    diffsum += ABS (diff);       /*add new one */    if (blobindex >= 2 && diffsum < bestsum) {      bestsum = diffsum;         /*find min sum */      bestindex = blobindex - 1; /*middle of set */    }  }  return bestindex;}/********************************************************************** * choose_partition * * Choose a partition for the point and return the index. **********************************************************************/intchoose_partition (               //select partitionregister float diff,             /*diff from spline */float partdiffs[],               /*diff on all parts */int lastpart,                    /*last assigned partition */float jumplimit,                 /*new part threshold */int *partcount                   /*no of partitions */) {  register int partition;        /*partition no */  int bestpart;                  /*best new partition */  float bestdelta;               /*best gap from a part */  static float drift;            /*drift from spline */  float delta;                   /*diff from part */  static float lastdelta;        /*previous delta */  if (lastpart < 0) {    partdiffs[0] = diff;    lastpart = 0;                /*first point */    drift = 0.0f;    lastdelta = 0.0f;  }                                 /*adjusted diff from part */  delta = diff - partdiffs[lastpart] - drift;  if (textord_oldbl_debug) {    tprintf ("Diff=%.2f, Delta=%.3f, Drift=%.3f, ", diff, delta, drift);  }  if (ABS (delta) > jumplimit / 2) {                                 /*delta on part 0 */    bestdelta = diff - partdiffs[0] - drift;    bestpart = 0;                /*0 best so far */    for (partition = 1; partition < *partcount; partition++) {      delta = diff - partdiffs[partition] - drift;      if (ABS (delta) < ABS (bestdelta)) {        bestdelta = delta;        bestpart = partition;    /*part with nearest jump */      }    }    delta = bestdelta;                                 /*too far away */    if (ABS (bestdelta) > jumplimit    && *partcount < MAXPARTS) {  /*and spare part left */      bestpart = (*partcount)++; /*best was new one */                                 /*start new one */      partdiffs[bestpart] = diff - drift;      delta = 0.0f;    }  }  else {    bestpart = lastpart;         /*best was last one */  }  if (bestpart == lastpart    && (ABS (delta - lastdelta) < jumplimit / 2    || ABS (delta) < jumplimit / 2))                                 /*smooth the drift */    drift = (3 * drift + delta) / 3;  lastdelta = delta;  if (textord_oldbl_debug) {    tprintf ("P=%d\n", bestpart);  }  return bestpart;}///*merge_partitions(partids,partcount,blobcount,bestpart) discards funny looking//partitions and gives all the rest partid 0*/////merge_partitions(partids,partcount,blobcount,bestpart)//register char              *partids;                     /*partition numbers*///int                        partcount;                    /*no of partitions*///int                        blobcount;                    /*no of blobs*///int                        bestpart;                     /*best partition*///{//   register int            blobindex;                    /*no along text line*///   int                     runlength;                    /*run of same partition*///   int                     bestrun;                      /*biggest runlength*/////   bestrun=0;                                            /*no runs yet*///   runlength=1;//   for (blobindex=1;blobindex<blobcount;blobindex++)//   {  if (partids[blobindex]!=partids[blobindex-1])

⌨️ 快捷键说明

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