1 /**********************************************************************
2 * File: blobcmp.c (Formerly blobcmp.c)
3 * Description: Code to compare blobs using the adaptive matcher.
4 * Author: Ray Smith
5 * Created: Wed Apr 21 09:28:51 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 "fxdefs.h"
22 #include "ocrfeatures.h"
23 #include "intmatcher.h"
24 #include "intproto.h"
25 #include "adaptive.h"
26 #include "adaptmatch.h"
27 #include "const.h"
28 #include "tessvars.h"
29 #include "tesseractclass.h"
30
31 #define CMP_CLASS 0
32
33 /**********************************************************************
34 * compare_tess_blobs
35 *
36 * Match 2 blobs using the adaptive classifier.
37 **********************************************************************/
38 namespace tesseract {
compare_tess_blobs(TBLOB * blob1,TEXTROW * row1,TBLOB * blob2,TEXTROW * row2)39 float Tesseract::compare_tess_blobs(TBLOB *blob1,
40 TEXTROW *row1,
41 TBLOB *blob2,
42 TEXTROW *row2) {
43 int fcount; /*number of features */
44 ADAPT_CLASS adapted_class;
45 ADAPT_TEMPLATES ad_templates;
46 LINE_STATS line_stats1, line_stats2;
47 INT_FEATURE_ARRAY int_features;
48 FEATURE_SET float_features;
49 INT_RESULT_STRUCT int_result; /*output */
50
51 BIT_VECTOR AllProtosOn = NewBitVector (MAX_NUM_PROTOS);
52 BIT_VECTOR AllConfigsOn = NewBitVector (MAX_NUM_CONFIGS);
53 set_all_bits (AllProtosOn, WordsInVectorOfSize (MAX_NUM_PROTOS));
54 set_all_bits (AllConfigsOn, WordsInVectorOfSize (MAX_NUM_CONFIGS));
55
56 EnterClassifyMode;
57 ad_templates = NewAdaptedTemplates (false);
58 GetLineStatsFromRow(row1, &line_stats1);
59 /*copy baseline stuff */
60 GetLineStatsFromRow(row2, &line_stats2);
61 adapted_class = NewAdaptedClass ();
62 AddAdaptedClass (ad_templates, adapted_class, CMP_CLASS);
63 InitAdaptedClass(blob1, &line_stats1, CMP_CLASS, adapted_class, ad_templates);
64 fcount = GetAdaptiveFeatures (blob2, &line_stats2,
65 int_features, &float_features);
66 if (fcount > 0) {
67 SetBaseLineMatch();
68 IntegerMatcher (ClassForClassId (ad_templates->Templates, CMP_CLASS),
69 AllProtosOn, AllConfigsOn, fcount, fcount,
70 int_features, 0, &int_result, testedit_match_debug);
71 FreeFeatureSet(float_features);
72 if (int_result.Rating < 0)
73 int_result.Rating = MAX_FLOAT32;
74 }
75
76 free_adapted_templates(ad_templates);
77 FreeBitVector(AllConfigsOn);
78 FreeBitVector(AllProtosOn);
79
80 return fcount > 0 ? int_result.Rating * fcount : MAX_FLOAT32;
81 }
82 } // namespace tesseract
83