1 /**********************************************************************
2 * File: paircmp.cpp (Formerly paircmp.c)
3 * Description: Code to compare two blobs using the adaptive matcher
4 * Author: Ray Smith
5 * Created: Wed Apr 21 09:31:02 BST 1993
6 *
7 * (C) Copyright 1993, Hewlett-Packard Ltd.
8 ** Licensed under the Apache License, Version 2.0 (the "License");
9 ** you may not use this file except in compliance with the License.
10 ** You may obtain a copy of the License at
11 ** http://www.apache.org/licenses/LICENSE-2.0
12 ** Unless required by applicable law or agreed to in writing, software
13 ** distributed under the License is distributed on an "AS IS" BASIS,
14 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 ** See the License for the specific language governing permissions and
16 ** limitations under the License.
17 *
18 **********************************************************************/
19
20 #include "mfcpch.h"
21 #include "blobcmp.h"
22 #include "tfacep.h"
23 #include "paircmp.h"
24 #include "tesseractclass.h"
25
26 #define EXTERN
27
28 /**********************************************************************
29 * compare_blob_pairs
30 *
31 * A blob processor to compare pairs of selected blobs.
32 **********************************************************************/
33
34 namespace tesseract {
compare_blob_pairs(BLOCK *,ROW * row,WERD *,PBLOB * blob)35 BOOL8 Tesseract::compare_blob_pairs( //blob processor
36 BLOCK *,
37 ROW *row, //row it came from
38 WERD *,
39 PBLOB *blob //blob to compare
40 ) {
41 static ROW *prev_row = NULL; //other in pair
42 static PBLOB *prev_blob = NULL;
43 float rating; //from matcher
44
45 if (prev_row == NULL || prev_blob == NULL) {
46 prev_row = row;
47 prev_blob = blob;
48 }
49 else {
50 rating = compare_blobs (prev_blob, prev_row, blob, row);
51 tprintf ("Rating=%g\n", rating);
52 prev_row = NULL;
53 prev_blob = NULL;
54 }
55 return TRUE;
56 }
57
58
59 /**********************************************************************
60 * compare_blobs
61 *
62 * Compare 2 blobs and return the rating.
63 **********************************************************************/
64
compare_blobs(PBLOB * blob1,ROW * row1,PBLOB * blob2,ROW * row2)65 float Tesseract::compare_blobs( //match 2 blobs
66 PBLOB *blob1, //first blob
67 ROW *row1, //row it came from
68 PBLOB *blob2, //other blob
69 ROW *row2) {
70 PBLOB *bn_blob1; //baseline norm
71 PBLOB *bn_blob2;
72 DENORM denorm1, denorm2;
73 float rating; //match result
74
75 bn_blob1 = blob1->baseline_normalise (row1, &denorm1);
76 bn_blob2 = blob2->baseline_normalise (row2, &denorm2);
77 rating = compare_bln_blobs (bn_blob1, &denorm1, bn_blob2, &denorm2);
78 delete bn_blob1;
79 delete bn_blob2;
80 return rating;
81 }
82
83
84 /**********************************************************************
85 * compare_bln_blobs
86 *
87 * Compare 2 baseline normalised blobs and return the rating.
88 **********************************************************************/
compare_bln_blobs(PBLOB * blob1,DENORM * denorm1,PBLOB * blob2,DENORM * denorm2)89 float Tesseract::compare_bln_blobs( //match 2 blobs
90 PBLOB *blob1, //first blob
91 DENORM *denorm1,
92 PBLOB *blob2, //other blob
93 DENORM *denorm2) {
94 TBLOB *tblob1; //tessblobs
95 TBLOB *tblob2;
96 TEXTROW tessrow1, tessrow2; //tess rows
97 float rating; //match result
98
99 tblob1 = make_tess_blob (blob1, TRUE);
100 make_tess_row(denorm1, &tessrow1);
101 tblob2 = make_tess_blob (blob2, TRUE);
102 make_tess_row(denorm2, &tessrow2);
103 rating = compare_tess_blobs (tblob1, &tessrow1, tblob2, &tessrow2);
104 free_blob(tblob1);
105 free_blob(tblob2);
106
107 return rating;
108 }
109 } // namespace tesseract
110