1 /*====================================================================*
2 - Copyright (C) 2001 Leptonica. All rights reserved.
3 -
4 - Redistribution and use in source and binary forms, with or without
5 - modification, are permitted provided that the following conditions
6 - are met:
7 - 1. Redistributions of source code must retain the above copyright
8 - notice, this list of conditions and the following disclaimer.
9 - 2. Redistributions in binary form must reproduce the above
10 - copyright notice, this list of conditions and the following
11 - disclaimer in the documentation and/or other materials
12 - provided with the distribution.
13 -
14 - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
18 - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 - OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *====================================================================*/
26
27 /*
28 * recogtest1.c
29 *
30 * Tests the recog utility using the bootstrap number set,
31 * for both training and identification
32 *
33 * An example of greedy splitting of touching characters is given.
34 */
35
36 #include "string.h"
37 #include "allheaders.h"
38
39 static const l_int32 scaledw = 0;
40 static const l_int32 scaledh = 40;
41
main(int argc,char ** argv)42 l_int32 main(int argc,
43 char **argv)
44 {
45 l_int32 i, j, n, index, w, h, linew, same;
46 l_float32 score;
47 char *fname, *strchar;
48 char buf[256];
49 BOX *box;
50 BOXA *boxat;
51 NUMA *na1;
52 PIX *pixs, *pixd, *pix1, *pix2, *pixdb;
53 PIXA *pixa1, *pixa2, *pixa3, *pixa4;
54 L_RECOG *recog1, *recog2;
55 SARRAY *sa, *satext;
56
57 if (argc != 1) {
58 fprintf(stderr, " Syntax: recogtest1\n");
59 return 1;
60 }
61
62 setLeptDebugOK(1);
63 lept_mkdir("lept/digits");
64 recog1 = NULL;
65 recog2 = NULL;
66
67 #if 0
68 linew = 5; /* for lines */
69 #else
70 linew = 0; /* scanned image */
71 #endif
72
73 #if 1
74 pixa1 = pixaRead("recog/digits/bootnum1.pa");
75 recog1 = recogCreateFromPixa(pixa1, scaledw, scaledh, linew, 120, 1);
76 pix1 = pixaDisplayTiledWithText(pixa1, 1400, 1.0, 10, 2, 6, 0xff000000);
77 pixWrite("/tmp/lept/digits/bootnum1.png", pix1, IFF_PNG);
78 pixDisplay(pix1, 800, 800);
79 pixDestroy(&pix1);
80 pixaDestroy(&pixa1);
81 #endif
82
83 #if 1
84 fprintf(stderr, "Print Stats 1\n");
85 recogShowContent(stderr, recog1, 1, 1);
86 #endif
87
88 #if 1
89 fprintf(stderr, "AverageSamples\n");
90 recogAverageSamples(&recog1, 1);
91 recogShowAverageTemplates(recog1);
92 pix1 = pixaGetPix(recog1->pixadb_ave, 0, L_CLONE);
93 pixWrite("/tmp/lept/digits/unscaled_ave.png", pix1, IFF_PNG);
94 pixDestroy(&pix1);
95 pix1 = pixaGetPix(recog1->pixadb_ave, 1, L_CLONE);
96 pixWrite("/tmp/lept/digits/scaled_ave.png", pix1, IFF_PNG);
97 pixDestroy(&pix1);
98 #endif
99
100 #if 1
101 recogDebugAverages(&recog1, 0);
102 recogShowMatchesInRange(recog1, recog1->pixa_tr, 0.65, 1.0, 0);
103 pixWrite("/tmp/lept/digits/match_ave1.png", recog1->pixdb_range, IFF_PNG);
104 recogShowMatchesInRange(recog1, recog1->pixa_tr, 0.0, 1.0, 0);
105 pixWrite("/tmp/lept/digits/match_ave2.png", recog1->pixdb_range, IFF_PNG);
106 #endif
107
108 #if 1
109 fprintf(stderr, "Print stats 2\n");
110 recogShowContent(stderr, recog1, 2, 1);
111 recogWrite("/tmp/lept/digits/rec1.rec", recog1);
112 recog2 = recogRead("/tmp/lept/digits/rec1.rec");
113 recogShowContent(stderr, recog2, 3, 1);
114 recogWrite("/tmp/lept/digits/rec2.rec", recog2);
115 filesAreIdentical("/tmp/lept/digits/rec1.rec",
116 "/tmp/lept/digits/rec2.rec", &same);
117 if (!same)
118 fprintf(stderr, "Error in serialization!\n");
119 recogDestroy(&recog2);
120 #endif
121
122 #if 1
123 /* Three sets of parameters:
124 * 0.6, 0.3 : removes a few poor matches
125 * 0.8, 0.2 : remove many based on matching; remove some based on
126 * requiring retention of 20% of templates in each class
127 * 0.9, 0.01 : remove most based on matching; saved 1 in each class */
128 fprintf(stderr, "Remove outliers\n");
129 static const l_float32 MinScore[] = {0.6f, 0.7f, 0.9f};
130 static const l_int32 MinTarget[] = {4, 5, 4};
131 static const l_int32 MinSize[] = {3, 2, 3};
132 pixa2 = recogExtractPixa(recog1);
133 for (i = 0; i < 3; i++) {
134 pixa3 = pixaRemoveOutliers1(pixa2, MinScore[i], MinTarget[i],
135 MinSize[i], &pix1, &pix2);
136 pixDisplay(pix1, 900, 250 * i);
137 pixDisplay(pix2, 1300, 250 * i);
138 pixDestroy(&pix1);
139 pixDestroy(&pix2);
140 pixaDestroy(&pixa3);
141 }
142 pixaDestroy(&pixa2);
143 #endif
144
145 #if 1
146 /* Split touching characters */
147 fprintf(stderr, "Split touching\n");
148 pixd = pixRead("recog/digits/page.590.png"); /* 590 or 306 */
149 recogIdentifyMultiple(recog1, pixd, 0, 0, &boxat, &pixa2, &pixdb, 1);
150 pixDisplay(pixdb, 800, 800);
151 boxaWriteStream(stderr, boxat);
152 pix1 = pixaDisplay(pixa2, 0, 0);
153 pixDisplay(pix1, 1200, 800);
154 pixDestroy(&pixdb);
155 pixDestroy(&pix1);
156 pixDestroy(&pixd);
157 pixaDestroy(&pixa2);
158 boxaDestroy(&boxat);
159 #endif
160
161 #if 1
162 fprintf(stderr, "Reading new training set and computing averages\n");
163 fprintf(stderr, "Print stats 3\n");
164 pixa1 = pixaRead("recog/sets/train03.pa");
165 recog2 = recogCreateFromPixa(pixa1, 0, 40, 0, 128, 1);
166 recogShowContent(stderr, recog2, 3, 1);
167 recogDebugAverages(&recog2, 3);
168 pixWrite("/tmp/lept/digits/averages.png", recog2->pixdb_ave, IFF_PNG);
169 recogShowAverageTemplates(recog2);
170 pixaDestroy(&pixa1);
171 recogDestroy(&recog2);
172 #endif
173
174 recogDestroy(&recog1);
175 recogDestroy(&recog2);
176 return 0;
177 }
178