phrasmap.c

来自「NIST Handwriting OCR Testbed」· C语言 代码 · 共 741 行 · 第 1/2 页

C
741
字号
   else{      return(TRUE);   }}/***************************************************************************/split_across_lines(bi, blobls, esw, pi_lists, pi_lens, pi_alens, nphrases,                         mcdata, w, h, xphrases, nx)BLOBLS *blobls;int bi, esw;int **pi_lists, *pi_lens, *pi_alens, nphrases;unsigned char *mcdata;int w, h;int *xphrases, nx;{   unsigned char *rcdata, *mallocate_image();   BLOB *lblob, *rblob, *sblob;   int ncuts, rw, rh;   int pi, dx, cx;   int *px, *py, pn, pa;   int newx1, newy1, nbi;   pi = 0;   rw = blobls->blobs[bi]->h;   rh = blobls->blobs[bi]->w;   /* initialize x cut point */   ncuts = nx - 1;   dx = rw / nx;   cx = dx;   /* if cx is zero then nothing to cut */   if(cx == 0){      /* this should't happen, but just in case */      /* assign blob to first phrase */      add_pi_list(&(pi_lists[xphrases[pi]]), &(pi_lens[xphrases[pi]]),                  &(pi_alens[xphrases[pi]]), bi);      return;   }   /* rotate original blob image onto its side for cutting */   /* creating a new blob */   rcdata = mallocate_image(rw, rh, 8);   grayscale_rotate_n90(blobls->blobs[bi]->data, rcdata,                        blobls->blobs[bi]->w, blobls->blobs[bi]->h);   build_blob(&rblob, blobls->flags,              rcdata, blobls->blobs[bi]->x1, blobls->blobs[bi]->y1, rw, rh);   /* initially set working blob to rotated blob */   sblob = rblob;   /* cut the blob along path starting at cx */   find_cut_path(&px, &py, &pn, &pa, sblob->data, cx, esw, sblob->w, sblob->h);   split_blob_along_path(&lblob, &rblob, px, py, pn, sblob);   free(px);   free(py);   /* if left blob cut ... */   if(lblob != (BLOB *)NULL){      /* rotate cut piece back */      rw = lblob->h;      rh = lblob->w;      rcdata = mallocate_image(rw, rh, 8);      grayscale_rotate_90(lblob->data, rcdata, lblob->w, lblob->h);      free_blob(lblob);      /* replace current blob image with left cut piece */      blobls->blobs[bi]->x1 = sblob->x1 + sblob->h - lblob->y1 - lblob->h;      blobls->blobs[bi]->y1 = sblob->y1 + lblob->x1;      blobls->blobs[bi]->w = rw;      blobls->blobs[bi]->h = rh;      free(blobls->blobs[bi]->data);      blobls->blobs[bi]->data = rcdata;      compute_blob_stats(blobls->blobs[bi], blobls->flags);      /* add modified blob index to first phrase */      add_pi_list(&(pi_lists[xphrases[pi]]), &(pi_lens[xphrases[pi]]),                  &(pi_alens[xphrases[pi]]), bi);   }   /* if left not cut, then skip current phrase split */   else{      /* advance cx to next split */      if(rblob != (BLOB *)NULL)         cx += dx;   }   /* if right cut piece exists, then continue to make cuts */   for(pi = 1; (pi < ncuts) && (rblob != (BLOB *)NULL); pi++){      /* don't let cx run off remaining right image */      cx = min(cx, rblob->w-1);      /* if cx is zero then nothing to cut */      if(cx <= 0){         free_blob(sblob);         /* assign right to current phrase */         add_pi_list(&(pi_lists[xphrases[pi]]), &(pi_lens[xphrases[pi]]),                     &(pi_alens[xphrases[pi]]), bi);         break;      }      /* update right blob and set to working blob for next cut */      newx1 = sblob->x1 + sblob->h - rblob->y1 - rblob->h;      newy1 = sblob->y1 + rblob->x1;      rblob->x1 = newx1;      rblob->y1 = newy1;      free_blob(sblob);      sblob = rblob;      /* cut working blob along path starting at cx */      find_cut_path(&px, &py, &pn, &pa, sblob->data, cx, esw, sblob->w, sblob->h);      split_blob_along_path(&lblob, &rblob, px, py, pn, sblob);      free(px);      free(py);      if(lblob != (BLOB *)NULL){         /* rotate cut piece back */         rw = lblob->h;         rh = lblob->w;         rcdata = mallocate_image(rw, rh, 8);         grayscale_rotate_90(lblob->data, rcdata, lblob->w, lblob->h);         /* replace left blob image with rotated cut piece */         newx1 = sblob->x1 + sblob->h - lblob->y1 - lblob->h;         newy1 = sblob->y1 + lblob->x1;         lblob->x1 = newx1;         lblob->y1 = newy1;         lblob->w = rw;         lblob->h = rh;         free(lblob->data);         lblob->data = rcdata;         compute_blob_stats(lblob, blobls->flags);         /* add new blob to blob list */         nbi = blobls->num;         append_blobls(blobls, lblob);         /* add new blob index to current phrase */         add_pi_list(&(pi_lists[xphrases[pi]]), &(pi_lens[xphrases[pi]]),                     &(pi_alens[xphrases[pi]]), nbi);      }      /* if left not cut, then skip current phrase split */      /* and advance cx to next split */      else{         if(rblob != (BLOB *)NULL)            cx += dx;      }   }   /* if final cut piece exists ... */   if(rblob != (BLOB *)NULL){      pi = ncuts;      /* rotate last cut piece back */      rw = rblob->h;      rh = rblob->w;      rcdata = mallocate_image(rw, rh, 8);      grayscale_rotate_90(rblob->data, rcdata, rblob->w, rblob->h);      /* replace blob image with rotated cut piece */      newx1 = sblob->x1 + sblob->h - rblob->y1 - rblob->h;      newy1 = sblob->y1 + rblob->x1;      rblob->x1 = newx1;      rblob->y1 = newy1;      rblob->w = rw;      rblob->h = rh;      free(rblob->data);      rblob->data = rcdata;      compute_blob_stats(rblob, blobls->flags);      /* add new blob to blob list */      nbi = blobls->num;      append_blobls(blobls, rblob);      /* add new blob index to current phrase */      add_pi_list(&(pi_lists[xphrases[pi]]), &(pi_lens[xphrases[pi]]),                     &(pi_alens[xphrases[pi]]), nbi);   }   /* free up final working blob */   free_blob(sblob);}/***************************************************************************/find_closest_line_in_map(blob, mcdata, w, h)BLOB *blob;unsigned char *mcdata;int w, h;{   int y, up, down, ufound, dfound, umap, dmap;   unsigned char *smptr, *mptr;   smptr = mcdata + (blob->cy * w) + blob->cx;   if(*smptr)      return(*smptr);   /* search upwards */   ufound = FALSE;   for(y = blob->cy-1, up = 1, mptr = smptr-w; y >= 0; y--, up++, mptr-=w){      if(*mptr){         ufound = TRUE;         umap = *mptr;         break;      }    }   /* search downwards */   dfound = FALSE;   for(y = blob->cy+1, down = 1, mptr = smptr+w; y < h; y++, down++, mptr+=w){      if(*mptr){         dfound = TRUE;         dmap = *mptr;         break;      }    }   if(!ufound && !dfound)      return(NOT_FOUND);   if(ufound && dfound){      if(up < down)         return(umap);      else         return(dmap);   }   else if (ufound)      return(umap);   else      return(dmap);}/***************************************************************************/hist_blob_colors(color_hist, hnum, blob, mcdata, w, h)int *color_hist, hnum;BLOB *blob;unsigned char *mcdata;int w, h;{   int x, y;   unsigned char *bptr, *smptr, *mptr;   bptr = blob->data;   smptr = mcdata + (blob->y1 * w) + blob->x1;   for(y = 0; y < blob->h; y++){      mptr = smptr;      for(x = 0; x < blob->w; x++){         if(*bptr && (*mptr != 0))            color_hist[*mptr]++;         bptr++;         mptr++;      }      smptr += w;   }}/***************************************************************************/draw_phrase_map(odata, w, h, blobls, pi_lists, pi_lens, nphrases)unsigned char **odata;int w, h;BLOBLS *blobls;int **pi_lists, *pi_lens, nphrases;{   int i, j, k, *tys, *bys, max_len, max_i;   int lx, tly, rx, try, *txlist, *tylist, tlen, talen;   int bly, bry, *bxlist, *bylist, blen, balen;   int pix, last, x;   unsigned char *allocate_image();   if(nphrases >= 256)      fatalerr("draw_phrase_map", "# of phrases exceeds 8-bit pixel value for map",               NULL);   *odata = allocate_image(w, h, 8);   find_first_max_forward(pi_lens, 0, nphrases, &max_i, &max_len);   malloc_int(&tys, max_len, "draw_phrase_map : tys");   malloc_int(&bys, max_len, "draw_phrase_map : bys");   talen = 0;   balen = 0;   for(i = 0; i < nphrases; i++){      pix = i+1;      for(j = 0; j < pi_lens[i]; j++){         tys[j] = blobls->blobs[(pi_lists[i])[j]]->y1;         bys[j] = blobls->blobs[(pi_lists[i])[j]]->y2;      }      three_smooth(tys, pi_lens[i]);      three_smooth(bys, pi_lens[i]);      tly = tys[0];      bly = bys[0];      lx = blobls->blobs[(pi_lists[i])[0]]->cx;      drawvertline8(*odata, w, h, lx, tly, lx, bly, pix);      for(j = 1; j < pi_lens[i]; j++){         /* interpolate top line segment */         try = tys[j];         rx = blobls->blobs[(pi_lists[i])[j]]->cx;         dx_line_alloc(lx, tly, rx, try, &txlist, &tylist, &tlen, &talen);         /* interpolate bottom line segment */         bry = bys[j];         dx_line_alloc(lx, bly, rx, bry, &bxlist, &bylist, &blen, &balen);         if(tlen != blen){            fatalerr("draw_phrase_map", "length of interpolated segments not equal",                     NULL);         }         for(k = 1; k < tlen; k++)            drawvertline8(*odata, w, h, txlist[k], tylist[k],                          bxlist[k], bylist[k], pix);         tly = try;         bly = bry;         lx = rx;      }      tly = tys[0];      bly = bys[0];      lx = blobls->blobs[(pi_lists[i])[0]]->cx - 1;      for(x = lx; x >= 0; x--){         if(!tstndrawvertline8(*odata, w, h, x, tly, x, bly, pix))            break;      }      last = pi_lens[i]-1;      try = tys[last];      bry = bys[last];      rx = blobls->blobs[(pi_lists[i])[last]]->cx + 1;      for(x = rx; x < w; x++){         if(!tstndrawvertline8(*odata, w, h, x, try, x, bry, pix))            break;      }   }   free(tys);   free(bys);   if(talen != 0){      free(txlist);      free(tylist);   }   if(balen != 0){      free(bxlist);      free(bylist);   }}/***************************************************************************/short_phrases_to_problems(problems, nprob, aprob, pi_lists, pi_lens, nphrases)int **problems, *nprob, *aprob;int **pi_lists, *pi_lens, *nphrases;{   int i, j, aincr;   aincr = max(TOO_SHORT, PHRASE_LEN_CHUNKS);   i = 0;   while(i < *nphrases){      if(pi_lens[i] <= TOO_SHORT){         if(((*nprob)+TOO_SHORT) >= (*aprob)){            (*aprob) += aincr;            realloc_int(problems, *aprob, "short_phrases_to_problems : problems");         }         for(j = 0; j < pi_lens[i]; j++){            (*problems)[*nprob] = (pi_lists[i])[j];            (*nprob)++;         }         remove_from_pi_lists(i, pi_lists, pi_lens, (*nphrases));         (*nphrases)--;      }      else         i++;   }}

⌨️ 快捷键说明

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