charsample.cpp

来自「一OCR的相关资料。.希望对研究OCR的朋友有所帮助.」· C++ 代码 · 共 699 行 · 第 1/2 页

CPP
699
字号
  ysize = 0;  ch = '\0';  nsamples = 0;  proto_data = NULL;  proto = NULL;}CHAR_PROTO::CHAR_PROTO(INT32 x_size,                       INT32 y_size,                       INT32 n_samples,                       float initial_value,                       char c) {  INT32 x;  INT32 y;  xsize = x_size;  ysize = y_size;  ch = c;  nsamples = n_samples;  ALLOC_2D_ARRAY(xsize, ysize, proto_data, proto, float);   for (y = 0; y < ysize; y++)    for (x = 0; x < xsize; x++)      proto[x][y] = initial_value;}CHAR_PROTO::CHAR_PROTO(CHAR_SAMPLE *sample) {   INT32 x;  INT32 y;  IMAGELINE imline_s;  if (sample->image () == NULL) {    xsize = 0;    ysize = 0;    ch = '\0';    nsamples = 0;    proto_data = NULL;    proto = NULL;  }  else {    ch = sample->character ();    xsize = sample->image ()->get_xsize ();    ysize = sample->image ()->get_ysize ();    nsamples = 1;    ALLOC_2D_ARRAY(xsize, ysize, proto_data, proto, float);     for (y = 0; y < ysize; y++) {      sample->image ()->fast_get_line (0, y, xsize, &imline_s);      for (x = 0; x < xsize; x++)        if (imline_s.pixels[x] == BINIM_WHITE)          proto[x][y] = 1.0;      else        proto[x][y] = -1.0;    }  }}CHAR_PROTO::~CHAR_PROTO () {  if (proto_data != NULL)    FREE_2D_ARRAY(proto_data, proto); }float CHAR_PROTO::match_sample(CHAR_SAMPLE *test_sample) {   CHAR_PROTO *test_proto;  float score;  if (test_sample->image () != NULL) {    test_proto = new CHAR_PROTO (test_sample);    if (xsize > test_proto->x_size ())      score = this->match (test_proto);    else {      demo_word = -demo_word;    // Flag different call      score = test_proto->match (this);    }  }  else    return BAD_SCORE;  delete test_proto;  return score;}float CHAR_PROTO::match(CHAR_PROTO *test_proto) {   INT32 xsize2 = test_proto->x_size ();  INT32 y_size;  INT32 y_size2;  INT32 x_offset;  INT32 y_offset;  INT32 x;  INT32 y;  CHAR_PROTO *match_proto;  float score;  float sum = 0.0;  ASSERT_HOST (xsize >= xsize2);  x_offset = (xsize - xsize2) / 2;  if (ysize < test_proto->y_size ()) {    y_size = test_proto->y_size ();    y_size2 = ysize;    y_offset = (y_size - y_size2) / 2;    match_proto = new CHAR_PROTO (xsize,      y_size,      nsamples * test_proto->n_samples (),      0, '\0');    for (y = 0; y < y_offset; y++) {      for (x = 0; x < xsize2; x++) {        match_proto->data ()[x + x_offset][y] =          test_proto->data ()[x][y] * nsamples;        sum += match_proto->data ()[x + x_offset][y];      }    }    for (y = y_offset + y_size2; y < y_size; y++) {      for (x = 0; x < xsize2; x++) {        match_proto->data ()[x + x_offset][y] =          test_proto->data ()[x][y] * nsamples;        sum += match_proto->data ()[x + x_offset][y];      }    }    for (y = y_offset; y < y_offset + y_size2; y++) {      for (x = 0; x < x_offset; x++) {        match_proto->data ()[x][y] = proto[x][y - y_offset] *          test_proto->n_samples ();        sum += match_proto->data ()[x][y];      }      for (x = x_offset + xsize2; x < xsize; x++) {        match_proto->data ()[x][y] = proto[x][y - y_offset] *          test_proto->n_samples ();        sum += match_proto->data ()[x][y];      }      for (x = x_offset; x < x_offset + xsize2; x++) {        match_proto->data ()[x][y] =          proto[x][y - y_offset] * test_proto->data ()[x - x_offset][y];        sum += match_proto->data ()[x][y];      }    }  }  else {    y_size = ysize;    y_size2 = test_proto->y_size ();    y_offset = (y_size - y_size2) / 2;    match_proto = new CHAR_PROTO (xsize,      y_size,      nsamples * test_proto->n_samples (),      0, '\0');    for (y = 0; y < y_offset; y++)    for (x = 0; x < xsize; x++) {      match_proto->data ()[x][y] =        proto[x][y] * test_proto->n_samples ();      sum += match_proto->data ()[x][y];    }    for (y = y_offset + y_size2; y < y_size; y++)    for (x = 0; x < xsize; x++) {      match_proto->data ()[x][y] =        proto[x][y] * test_proto->n_samples ();      sum += match_proto->data ()[x][y];    }    for (y = y_offset; y < y_offset + y_size2; y++) {      for (x = 0; x < x_offset; x++) {        match_proto->data ()[x][y] =          proto[x][y] * test_proto->n_samples ();        sum += match_proto->data ()[x][y];      }      for (x = x_offset + xsize2; x < xsize; x++) {        match_proto->data ()[x][y] =          proto[x][y] * test_proto->n_samples ();        sum += match_proto->data ()[x][y];      }      for (x = x_offset; x < x_offset + xsize2; x++) {        match_proto->data ()[x][y] = proto[x][y] *          test_proto->data ()[x - x_offset][y - y_offset];        sum += match_proto->data ()[x][y];      }    }  }  score = (1.0 - sum /    (xsize * y_size * nsamples * test_proto->n_samples ()));  if (tessedit_mm_debug) {    if (score < 0) {      tprintf ("Match score %f\n", score);      tprintf ("x: %d, y: %d, ns: %d, nt: %d, dx %d, dy: %d\n",        xsize, y_size, nsamples, test_proto->n_samples (),        x_offset, y_offset);      for (y = 0; y < y_size; y++) {        tprintf ("\n%d", y);        for (x = 0; x < xsize; x++)          tprintf ("\t%d", match_proto->data ()[x][y]);      }      tprintf ("\n");      fflush(debug_fp);     }  }#ifndef GRAPHICS_DISABLED  if (tessedit_display_mm) {    tprintf ("Match score %f\n", score);    display_images (this->make_image (),      test_proto->make_image (), match_proto->make_image ());  }  else if (demo_word != 0) {    if (demo_word > 0)      display_image (test_proto->make_image (), "Test sample",        300, 400, FALSE);    else      display_image (this->make_image (), "Test sample", 300, 400, FALSE);    display_image (match_proto->make_image (), "Best match",      700, 400, TRUE);  }#endif  delete match_proto;  return score;}void CHAR_PROTO::enlarge_prototype(INT32 new_xsize, INT32 new_ysize) {   float *old_proto_data = proto_data;  float **old_proto = proto;  INT32 old_xsize = xsize;  INT32 old_ysize = ysize;  INT32 x_offset;  INT32 y_offset;  INT32 x;  INT32 y;  ASSERT_HOST (new_xsize >= xsize && new_ysize >= ysize);  xsize = new_xsize;  ysize = new_ysize;  ALLOC_2D_ARRAY(xsize, ysize, proto_data, proto, float);   x_offset = (xsize - old_xsize) / 2;  y_offset = (ysize - old_ysize) / 2;  for (y = 0; y < y_offset; y++)    for (x = 0; x < xsize; x++)      proto[x][y] = nsamples;  for (y = y_offset + old_ysize; y < ysize; y++)    for (x = 0; x < xsize; x++)      proto[x][y] = nsamples;  for (y = y_offset; y < y_offset + old_ysize; y++) {    for (x = 0; x < x_offset; x++)      proto[x][y] = nsamples;    for (x = x_offset + old_xsize; x < xsize; x++)      proto[x][y] = nsamples;    for (x = x_offset; x < x_offset + old_xsize; x++)      proto[x][y] = old_proto[x - x_offset][y - y_offset];  }  FREE_2D_ARRAY(old_proto_data, old_proto); }void CHAR_PROTO::add_sample(CHAR_SAMPLE *sample) {   INT32 x_offset;  INT32 y_offset;  INT32 x;  INT32 y;  IMAGELINE imline_s;  INT32 sample_xsize = sample->image ()->get_xsize ();  INT32 sample_ysize = sample->image ()->get_ysize ();  x_offset = (xsize - sample_xsize) / 2;  y_offset = (ysize - sample_ysize) / 2;  ASSERT_HOST (x_offset >= 0 && y_offset >= 0);  for (y = 0; y < y_offset; y++)    for (x = 0; x < xsize; x++)      proto[x][y]++;             // Treat pixels outside the  // range as white  for (y = y_offset + sample_ysize; y < ysize; y++)    for (x = 0; x < xsize; x++)      proto[x][y]++;  for (y = y_offset; y < y_offset + sample_ysize; y++) {    sample->image ()->fast_get_line (0,      y - y_offset, sample_xsize, &imline_s);    for (x = x_offset; x < x_offset + sample_xsize; x++) {      if (imline_s.pixels[x - x_offset] == BINIM_WHITE)        proto[x][y]++;      else        proto[x][y]--;    }    for (x = 0; x < x_offset; x++)      proto[x][y]++;    for (x = x_offset + sample_xsize; x < xsize; x++)      proto[x][y]++;  }  nsamples++;}IMAGE *CHAR_PROTO::make_image() {   IMAGE *image;  IMAGELINE imline_p;  INT32 x;  INT32 y;  ASSERT_HOST (nsamples != 0);  image = new (IMAGE);  image->create (xsize, ysize, 8);  for (y = 0; y < ysize; y++) {    image->fast_get_line (0, y, xsize, &imline_p);    for (x = 0; x < xsize; x++) {      imline_p.pixels[x] = 128 +        (UINT8) ((proto[x][y] * 128.0) / (0.00001 + nsamples));    }    image->fast_put_line (0, y, xsize, &imline_p);  }  return image;}

⌨️ 快捷键说明

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