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

📄 blobls.c

📁 NIST Handwriting OCR Testbed
💻 C
📖 第 1 页 / 共 2 页
字号:
      if(do_xy2s){         blobls->blobs[i]->x2 = blobls->blobs[i]->x1 + blobls->blobs[i]->w - 1;         blobls->blobs[i]->y2 = blobls->blobs[i]->y1 + blobls->blobs[i]->h - 1;      }      if(do_cxys){         blobls->blobs[i]->cx = blobls->blobs[i]->x1 + (blobls->blobs[i]->w >>1);         blobls->blobs[i]->cy = blobls->blobs[i]->y1 + (blobls->blobs[i]->h >>1);      }      if(do_as){         blobls->blobs[i]->a = blobls->blobs[i]->w * blobls->blobs[i]->h;         if(do_pixcnt)            blobls->blobs[i]->pixcnt=pixelcnt8(blobls->blobs[i]->data,blobls->blobs[i]->a);      }      else if(do_pixcnt)         blobls->blobs[i]->pixcnt=pixelcnt8(blobls->blobs[i]->data,                                            blobls->blobs[i]->w * blobls->blobs[i]->h);   }   blobls->flags |= flags;}/*******************************************************************/compute_blob_stats(blob, flags)BLOB *blob;int flags;{   int do_pixcnt;/*   if(!(flags & BLOB_XY1S) || !(flags & BLOB_WHS))      fatalerr("compute_blob_stats",               "must have at least (x1, y1) and (w, h) already in blob structure",               NULL);*/   if(flags & BLOB_XY2S){      blob->x2 = blob->x1 + blob->w - 1;      blob->y2 = blob->y1 + blob->h - 1;   }   if(flags & BLOB_CXYS){      blob->cx = blob->x1 + (blob->w >>1);      blob->cy = blob->y1 + (blob->h >>1);   }   do_pixcnt = (flags & BLOB_PIXCNTS)?1:0;   if(flags & BLOB_AS){      blob->a = blob->w * blob->h;      if(do_pixcnt)         blob->pixcnt = pixelcnt8(blob->data, blob->a);   }   else if(do_pixcnt)      blob->pixcnt = pixelcnt8(blob->data, blob->w * blob->h);}/*******************************************************************/alloc_blob(blob)BLOB **blob;{   if(((*blob) = (BLOB *)malloc(sizeof(BLOB))) == NULL)      syserr("alloc_blob", "malloc", "blob");   (*blob)->data = (unsigned char *)NULL;}/*******************************************************************/free_blob(blob)BLOB *blob;{   if(blob->data != (unsigned char *)NULL)      free(blob->data);   free(blob);}/*******************************************************************/char2bin_blobls(blobls)BLOBLS *blobls;{   unsigned char *bdata;   int i, bw, bh;   if((blobls->bpp != 8) || (blobls->b_g != BLOB_BIN))      fatalerr("char2bin_blobls",               "input image data must be binary and one byte per pixel", NULL);   /* convert blobs to binary (8 bits per byte) */   for(i = 0; i < blobls->num; i++){      char2bin_exact(&bdata, &bw, &bh,                     blobls->blobs[i]->data, blobls->blobs[i]->w, blobls->blobs[i]->h);      free(blobls->blobs[i]->data);      blobls->blobs[i]->data = bdata;      blobls->blobs[i]->w = bw;      blobls->blobs[i]->h = bh;   }   blobls->bpp = 1;}/************************************************************/find_first_max_blob_memb(blobls, blob_offset, max_i, max_v)BLOBLS *blobls;int blob_offset, *max_i, *max_v;{   int i, val;   char *bptr;   *max_i = -1;   *max_v = -1;   for(i = 0; i < blobls->num; i++){      bptr = (char *)(blobls->blobs[i]);      val = *((int *)(bptr+blob_offset));      if(val > *max_v){         *max_v = val;         *max_i = i;      }   }}/************************************************************/float compute_avr_blob_memb(blobls, blob_offset)BLOBLS *blobls;int blob_offset;{   int sum = 0;   int i, val;   char *bptr;   float avr;   for(i = 0; i < blobls->num; i++){      bptr = (char *)(blobls->blobs[i]);      val = *((int *)(bptr+blob_offset));      sum += val;   }   avr = sum / (float)blobls->num;   return(avr);}/************************************************************/prct_blobls_memb(blobls, offset, prct)BLOBLS *blobls;int offset;float prct;{   BLOBLS *sblobls;   int topn, val;   char *bptr;   sort_blobls_on_memb(&sblobls, INC, blobls, offset);   topn = max(0, sround(sblobls->num * prct)-1);   bptr = (char *)(sblobls->blobs[topn]);   val = *((int *)(bptr+offset));   free(sblobls->blobs);   free(sblobls);   return(val);}/*************************************************************************/median_blob_memb(blobls, blob_offset)BLOBLS *blobls;int blob_offset;{   int i, *list, med;   char *bptr;   malloc_int(&list, blobls->num, "find_median_blob_memb: member list");   for(i = 0; i < blobls->num; i++){      bptr = (char *)(blobls->blobs[i]);      list[i] = *((int *)(bptr+blob_offset));   }   med = int_list_median(list, blobls->num);   free(list);   return(med);}/************************************************************/sort_blobls_on_memb(sblobls, order, blobls, blob_offset)BLOBLS **sblobls, *blobls;int order, blob_offset;{   int i;   char *bptr;   if(blobls->num == 0)      return;   build_blobls(sblobls, blobls->num, blobls->flags, blobls->bpp, blobls->b_g);   with_1_INDEX_alloc(istruct, blobls->num, INTTYPE)      for(i = 0; i < blobls->num; i++){         bptr = (char *)(blobls->blobs[i]);         add_to_1_INDEX(istruct, i, *((int *)(bptr+blob_offset)));      }      multisort_1_INDEX(istruct, order);      i = 0;      foreach_in_INDEX(istruct)         append_blobls((*sblobls), blobls->blobs[this_item_int]);      endfor   end_with_INDEX_alloc(istruct)}/************************************************************/is_blob_above(ablob, bblob)BLOB *ablob, *bblob;{   /* is first blob's bottom y above second blob's top y */   if(ablob->y2 < bblob->y1)      return(TRUE);   else      return(FALSE);}/************************************************************/find_blob_nbrs(li, ri, nnbrs, blob, blobls)int *li, *ri, *nnbrs;BLOB *blob;BLOBLS *blobls;{   /* Warning: blobls should already be sorted left to right */   (*ri) = 0;   while(((*ri) < blobls->num) && (blobls->blobs[(*ri)]->cx < blob->cx))      (*ri)++;   /* if only one nbr */   if(((*ri) == 0) || ((*ri) == blobls->num)){      *nnbrs = 1;      if((*ri) == blobls->num)         (*ri)--;      (*li) = (*ri);   }   else{      (*li) = (*ri) - 1;      *nnbrs = 2;   }}/************************************************************/find_ind_blob_nbrs(li, ri, nnbrs, blob, blobls, rank)int *li, *ri, *nnbrs;BLOB *blob;BLOBLS *blobls;int *rank;{   (*ri) = 0;   while(((*ri) < blobls->num) && (blobls->blobs[rank[(*ri)]]->cx < blob->cx))      (*ri)++;   /* if only one nbr */   if(((*ri) == 0) || ((*ri) == blobls->num)){      *nnbrs = 1;      if((*ri) == blobls->num)         (*ri)--;      (*li) = (*ri);   }   else{      (*li) = (*ri) - 1;      *nnbrs = 2;   }}/************************************************************/find_first_blob_inside(blob, blobls)BLOB *blob;BLOBLS *blobls;{   int bi;   for(bi = 0; bi < blobls->num; bi++){      if(is_box_inside(blob->x1, blob->y1, blob->x2, blob->y2,                       blobls->blobs[bi]->x1, blobls->blobs[bi]->y1,                       blobls->blobs[bi]->x2, blobls->blobs[bi]->y2))         return(bi);   }   return(NOT_FOUND);}/*************************************************************************/blob_hori_overlaps(laps_i, laps_d, nlaps, alaps, blobls, bi)BLOBLS *blobls;int **laps_i, **laps_d, *nlaps, *alaps, bi;{   int i, d;   BLOB *cptr, *nptr;   *nlaps = 0;   cptr = blobls->blobs[bi];   for(i = 0; i < blobls->num; i++){      if(i != bi){         nptr = blobls->blobs[i];         if(box_overlap_hori(cptr->x1, cptr->y1, cptr->x2, cptr->y2,                             nptr->x1, nptr->y1, nptr->x2, nptr->y2)){            /* reallocate lists if necessary */            if(*nlaps >= *alaps){               (*alaps) += BLOB_CHUNKS;               realloc_int(laps_i, *alaps, "blob_hori_overlaps : realloc : laps_i");               realloc_int(laps_d, *alaps, "blob_hori_overlaps : realloc : laps_d");            }            /* compute and store overlap distance and index */            d = (cptr->w + nptr->w) -                   (max(nptr->x2, cptr->x2) - min(nptr->x1, cptr->x1) + 1);            (*laps_i)[*nlaps] = i;            (*laps_d)[*nlaps] = d;            (*nlaps)++;         }      }   }}

⌨️ 快捷键说明

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