#include <charsample.h>
Created from character sample. x_size and y_size are max_x and max_y of sample, respectively; n_samples is HOW MANY samples have been added to THIS prototype, see CHAR_PROTO::add_sample(); initial_value is ??; and c is the class these sample represent.
The n_samples is used in computing the match score:
score = (1.0 - sum / (xsize * y_size * nsamples * test_proto->n_samples ()));
Definition at line 186 of file charsample.h.
CHAR_PROTO::CHAR_PROTO | ( | ) |
Definition at line 396 of file charsample.cpp.
References ch, nsamples, NULL, proto_data, and ysize.
Referenced by match().
00396 { 00397 xsize = 0; 00398 ysize = 0; 00399 ch = '\0'; 00400 nsamples = 0; 00401 proto_data = NULL; 00402 proto = NULL; 00403 }
Definition at line 407 of file charsample.cpp.
References ALLOC_2D_ARRAY, ch, nsamples, proto_data, and ysize.
00411 { 00412 INT32 x; 00413 INT32 y; 00414 00415 xsize = x_size; 00416 ysize = y_size; 00417 ch = c; 00418 nsamples = n_samples; 00419 00420 ALLOC_2D_ARRAY(xsize, ysize, proto_data, proto, float); 00421 00422 for (y = 0; y < ysize; y++) 00423 for (x = 0; x < xsize; x++) 00424 proto[x][y] = initial_value; 00425 }
CHAR_PROTO::CHAR_PROTO | ( | CHAR_SAMPLE * | sample | ) |
Definition at line 430 of file charsample.cpp.
References ALLOC_2D_ARRAY, BINIM_WHITE, ch, CHAR_SAMPLE::character(), IMAGE::fast_get_line(), IMAGE::get_xsize(), IMAGE::get_ysize(), CHAR_SAMPLE::image(), nsamples, NULL, IMAGELINE::pixels, proto_data, and ysize.
00430 { 00431 INT32 x; 00432 INT32 y; 00433 IMAGELINE imline_s; 00434 00435 if (sample->image () == NULL) { 00436 xsize = 0; 00437 ysize = 0; 00438 ch = '\0'; 00439 nsamples = 0; 00440 proto_data = NULL; 00441 proto = NULL; 00442 } 00443 else { 00444 ch = sample->character (); 00445 xsize = sample->image ()->get_xsize (); 00446 ysize = sample->image ()->get_ysize (); 00447 nsamples = 1; 00448 00449 ALLOC_2D_ARRAY(xsize, ysize, proto_data, proto, float); 00450 00451 for (y = 0; y < ysize; y++) { 00452 sample->image ()->fast_get_line (0, y, xsize, &imline_s); 00453 for (x = 0; x < xsize; x++) 00454 if (imline_s.pixels[x] == BINIM_WHITE) 00455 proto[x][y] = 1.0; 00456 else 00457 proto[x][y] = -1.0; 00458 } 00459 } 00460 }
CHAR_PROTO::~CHAR_PROTO | ( | ) |
Destructor
Definition at line 466 of file charsample.cpp.
References FREE_2D_ARRAY, NULL, and proto_data.
00466 { 00467 if (proto_data != NULL) 00468 FREE_2D_ARRAY(proto_data, proto); 00469 }
void CHAR_PROTO::add_sample | ( | CHAR_SAMPLE * | sample | ) |
Definition at line 694 of file charsample.cpp.
References ASSERT_HOST, BINIM_WHITE, IMAGE::fast_get_line(), IMAGE::get_xsize(), IMAGE::get_ysize(), CHAR_SAMPLE::image(), nsamples, IMAGELINE::pixels, and ysize.
00694 { 00695 INT32 x_offset; 00696 INT32 y_offset; 00697 INT32 x; 00698 INT32 y; 00699 IMAGELINE imline_s; 00700 INT32 sample_xsize = sample->image ()->get_xsize (); 00701 INT32 sample_ysize = sample->image ()->get_ysize (); 00702 00703 x_offset = (xsize - sample_xsize) / 2; 00704 y_offset = (ysize - sample_ysize) / 2; 00705 00706 ASSERT_HOST (x_offset >= 0 && y_offset >= 0); 00707 00708 for (y = 0; y < y_offset; y++) 00709 for (x = 0; x < xsize; x++) 00710 proto[x][y]++; // Treat pixels outside the range as white 00711 for (y = y_offset + sample_ysize; y < ysize; y++) 00712 for (x = 0; x < xsize; x++) 00713 proto[x][y]++; 00714 00715 for (y = y_offset; y < y_offset + sample_ysize; y++) { 00716 sample->image ()->fast_get_line (0, 00717 y - y_offset, sample_xsize, &imline_s); 00718 for (x = x_offset; x < x_offset + sample_xsize; x++) { 00719 if (imline_s.pixels[x - x_offset] == BINIM_WHITE) 00720 proto[x][y]++; 00721 else 00722 proto[x][y]--; 00723 } 00724 00725 for (x = 0; x < x_offset; x++) 00726 proto[x][y]++; 00727 00728 for (x = x_offset + sample_xsize; x < xsize; x++) 00729 proto[x][y]++; 00730 } 00731 00732 nsamples++; 00733 }
char CHAR_PROTO::character | ( | ) | [inline] |
float** CHAR_PROTO::data | ( | ) | [inline] |
Definition at line 218 of file charsample.h.
Referenced by match().
00218 { 00219 return proto; 00220 }
Definition at line 651 of file charsample.cpp.
References ALLOC_2D_ARRAY, ASSERT_HOST, FREE_2D_ARRAY, nsamples, proto_data, and ysize.
00651 { 00652 float *old_proto_data = proto_data; 00653 float **old_proto = proto; 00654 INT32 old_xsize = xsize; 00655 INT32 old_ysize = ysize; 00656 INT32 x_offset; 00657 INT32 y_offset; 00658 INT32 x; 00659 INT32 y; 00660 00661 ASSERT_HOST (new_xsize >= xsize && new_ysize >= ysize); 00662 00663 xsize = new_xsize; 00664 ysize = new_ysize; 00665 ALLOC_2D_ARRAY(xsize, ysize, proto_data, proto, float); 00666 x_offset = (xsize - old_xsize) / 2; 00667 y_offset = (ysize - old_ysize) / 2; 00668 00669 for (y = 0; y < y_offset; y++) 00670 for (x = 0; x < xsize; x++) 00671 proto[x][y] = nsamples; 00672 00673 for (y = y_offset + old_ysize; y < ysize; y++) 00674 for (x = 0; x < xsize; x++) 00675 proto[x][y] = nsamples; 00676 00677 for (y = y_offset; y < y_offset + old_ysize; y++) { 00678 for (x = 0; x < x_offset; x++) 00679 proto[x][y] = nsamples; 00680 00681 for (x = x_offset + old_xsize; x < xsize; x++) 00682 proto[x][y] = nsamples; 00683 00684 for (x = x_offset; x < x_offset + old_xsize; x++) 00685 proto[x][y] = old_proto[x - x_offset][y - y_offset]; 00686 } 00687 00688 FREE_2D_ARRAY(old_proto_data, old_proto); 00689 }
IMAGE * CHAR_PROTO::make_image | ( | ) |
Definition at line 738 of file charsample.cpp.
References ASSERT_HOST, IMAGE::create(), IMAGE::fast_get_line(), IMAGE::fast_put_line(), nsamples, IMAGELINE::pixels, and ysize.
Referenced by match().
00738 { 00739 IMAGE *image; 00740 IMAGELINE imline_p; 00741 INT32 x; 00742 INT32 y; 00743 00744 ASSERT_HOST (nsamples != 0); 00745 00746 image = new (IMAGE); 00747 image->create (xsize, ysize, 8); 00748 00749 for (y = 0; y < ysize; y++) { 00750 image->fast_get_line (0, y, xsize, &imline_p); 00751 00752 for (x = 0; x < xsize; x++) { 00753 imline_p.pixels[x] = 128 + 00754 (UINT8) ((proto[x][y] * 128.0) / (0.00001 + nsamples)); 00755 } 00756 00757 image->fast_put_line (0, y, xsize, &imline_p); 00758 } 00759 return image; 00760 }
float CHAR_PROTO::match | ( | CHAR_PROTO * | test_proto | ) |
Definition at line 498 of file charsample.cpp.
References ASSERT_HOST, CHAR_PROTO(), data(), debug_fp, demo_word, display_image, display_images(), FALSE, make_image(), n_samples(), nsamples, tprintf(), TRUE, x_size(), y_size(), and ysize.
Referenced by match_sample().
00498 { 00499 INT32 xsize2 = test_proto->x_size (); 00500 INT32 y_size; 00501 INT32 y_size2; 00502 INT32 x_offset; 00503 INT32 y_offset; 00504 INT32 x; 00505 INT32 y; 00506 CHAR_PROTO *match_proto; 00507 float score; 00508 float sum = 0.0; 00509 00510 ASSERT_HOST (xsize >= xsize2); 00511 00512 x_offset = (xsize - xsize2) / 2; 00513 00514 if (ysize < test_proto->y_size ()) { 00515 y_size = test_proto->y_size (); 00516 y_size2 = ysize; 00517 y_offset = (y_size - y_size2) / 2; 00518 00519 match_proto = new CHAR_PROTO (xsize, 00520 y_size, 00521 nsamples * test_proto->n_samples (), 00522 0, '\0'); 00523 00524 for (y = 0; y < y_offset; y++) { 00525 for (x = 0; x < xsize2; x++) { 00526 match_proto->data ()[x + x_offset][y] = 00527 test_proto->data ()[x][y] * nsamples; 00528 sum += match_proto->data ()[x + x_offset][y]; 00529 } 00530 } 00531 00532 for (y = y_offset + y_size2; y < y_size; y++) { 00533 for (x = 0; x < xsize2; x++) { 00534 match_proto->data ()[x + x_offset][y] = 00535 test_proto->data ()[x][y] * nsamples; 00536 sum += match_proto->data ()[x + x_offset][y]; 00537 } 00538 } 00539 00540 for (y = y_offset; y < y_offset + y_size2; y++) { 00541 for (x = 0; x < x_offset; x++) { 00542 match_proto->data ()[x][y] = proto[x][y - y_offset] * 00543 test_proto->n_samples (); 00544 sum += match_proto->data ()[x][y]; 00545 } 00546 00547 for (x = x_offset + xsize2; x < xsize; x++) { 00548 match_proto->data ()[x][y] = proto[x][y - y_offset] * 00549 test_proto->n_samples (); 00550 sum += match_proto->data ()[x][y]; 00551 } 00552 00553 for (x = x_offset; x < x_offset + xsize2; x++) { 00554 match_proto->data ()[x][y] = 00555 proto[x][y - y_offset] * test_proto->data ()[x - x_offset][y]; 00556 sum += match_proto->data ()[x][y]; 00557 } 00558 } 00559 } 00560 else { 00561 y_size = ysize; 00562 y_size2 = test_proto->y_size (); 00563 y_offset = (y_size - y_size2) / 2; 00564 00565 match_proto = new CHAR_PROTO (xsize, 00566 y_size, 00567 nsamples * test_proto->n_samples (), 00568 0, '\0'); 00569 00570 for (y = 0; y < y_offset; y++) 00571 for (x = 0; x < xsize; x++) { 00572 match_proto->data ()[x][y] = 00573 proto[x][y] * test_proto->n_samples (); 00574 sum += match_proto->data ()[x][y]; 00575 } 00576 00577 for (y = y_offset + y_size2; y < y_size; y++) 00578 for (x = 0; x < xsize; x++) { 00579 match_proto->data ()[x][y] = 00580 proto[x][y] * test_proto->n_samples (); 00581 sum += match_proto->data ()[x][y]; 00582 } 00583 00584 for (y = y_offset; y < y_offset + y_size2; y++) { 00585 for (x = 0; x < x_offset; x++) { 00586 match_proto->data ()[x][y] = 00587 proto[x][y] * test_proto->n_samples (); 00588 sum += match_proto->data ()[x][y]; 00589 } 00590 00591 for (x = x_offset + xsize2; x < xsize; x++) { 00592 match_proto->data ()[x][y] = 00593 proto[x][y] * test_proto->n_samples (); 00594 sum += match_proto->data ()[x][y]; 00595 } 00596 00597 for (x = x_offset; x < x_offset + xsize2; x++) { 00598 match_proto->data ()[x][y] = proto[x][y] * 00599 test_proto->data ()[x - x_offset][y - y_offset]; 00600 sum += match_proto->data ()[x][y]; 00601 } 00602 } 00603 } 00604 00605 score = (1.0 - sum / 00606 (xsize * y_size * nsamples * test_proto->n_samples ())); 00607 00608 if (tessedit_mm_debug) { 00609 if (score < 0) { 00610 tprintf ("Match score %f\n", score); 00611 tprintf ("x: %d, y: %d, ns: %d, nt: %d, dx %d, dy: %d\n", 00612 xsize, y_size, nsamples, test_proto->n_samples (), 00613 x_offset, y_offset); 00614 for (y = 0; y < y_size; y++) { 00615 tprintf ("\n%d", y); 00616 for (x = 0; x < xsize; x++) 00617 tprintf ("\t%d", match_proto->data ()[x][y]); 00618 00619 } 00620 tprintf ("\n"); 00621 fflush(debug_fp); 00622 } 00623 } 00624 00625 #ifndef GRAPHICS_DISABLED 00626 if (tessedit_display_mm) { 00627 tprintf ("Match score %f\n", score); 00628 display_images (this->make_image (), 00629 test_proto->make_image (), match_proto->make_image ()); 00630 } 00631 else if (demo_word != 0) { 00632 if (demo_word > 0) 00633 display_image (test_proto->make_image (), "Test sample", 00634 300, 400, FALSE); 00635 else 00636 display_image (this->make_image (), "Test sample", 300, 400, FALSE); 00637 00638 display_image (match_proto->make_image (), "Best match", 00639 700, 400, TRUE); 00640 } 00641 #endif 00642 00643 delete match_proto; 00644 00645 return score; 00646 }
float CHAR_PROTO::match_sample | ( | CHAR_SAMPLE * | test_sample | ) |
Definition at line 474 of file charsample.cpp.
References BAD_SCORE, demo_word, CHAR_SAMPLE::image(), match(), NULL, and x_size().
00474 { 00475 CHAR_PROTO *test_proto; 00476 float score; 00477 00478 if (test_sample->image () != NULL) { 00479 test_proto = new CHAR_PROTO (test_sample); 00480 if (xsize > test_proto->x_size ()) 00481 score = this->match (test_proto); 00482 else { 00483 demo_word = -demo_word; // Flag different call 00484 score = test_proto->match (this); 00485 } 00486 } 00487 else 00488 return BAD_SCORE; 00489 00490 delete test_proto; 00491 00492 return score; 00493 }
INT32 CHAR_PROTO::n_samples | ( | ) | [inline] |
Definition at line 206 of file charsample.h.
Referenced by match().
00206 { 00207 return nsamples; 00208 }
void CHAR_PROTO::print | ( | FILE * | f | ) |
INT32 CHAR_PROTO::x_size | ( | ) | [inline] |
INT32 CHAR_PROTO::y_size | ( | ) | [inline] |
Definition at line 214 of file charsample.h.
Referenced by match().
00214 { 00215 return ysize; 00216 }
char CHAR_PROTO::ch |
Definition at line 238 of file charsample.h.
Referenced by add_sample(), CHAR_PROTO(), enlarge_prototype(), make_image(), and match().
float** CHAR_PROTO::proto |
Definition at line 237 of file charsample.h.
float* CHAR_PROTO::proto_data |
Definition at line 236 of file charsample.h.
Referenced by CHAR_PROTO(), enlarge_prototype(), and ~CHAR_PROTO().
NEWDELETE2 (CHAR_PROTO) private INT32 CHAR_PROTO::ysize |
Definition at line 233 of file charsample.h.
Referenced by add_sample(), CHAR_PROTO(), enlarge_prototype(), make_image(), and match().