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  *  recogtest6.c
29  *
30  *     Another test of character splitting.  This will test both DID
31  *     and greedy splitting.  To test greedy splitting, in recogident.c,
32  *       #define  SPLIT_WITH_DID   0
33  *
34  *     The timing info is used to measure the time to split touching
35  *     characters and identify them.  One set of 4 digits takes about 1 ms
36  *     with DID and 7 ms with greedy splitting.  Because DID is about
37  *     5x faster than greedy splitting, DID is the default that is used.
38  */
39 
40 #include "string.h"
41 #include "allheaders.h"
42 
43 static PIX *GetBigComponent(PIX *pixs);
44 
45 
main(int argc,char ** argv)46 l_int32 main(int    argc,
47              char **argv)
48 {
49 l_int32   item, debug, i;
50 l_int32   example[6] = {17, 20, 21, 22, 23, 24};  /* for decoding */
51 BOXA     *boxa;
52 NUMA     *nascore;
53 PIX      *pix1, *pix2, *pix3, *pixdb;
54 PIXA     *pixa1, *pixa2;
55 L_RECOG  *recog;
56 
57     if (argc != 1) {
58         fprintf(stderr, " Syntax: recogtest6\n");
59         return 1;
60     }
61 
62     setLeptDebugOK(1);
63     lept_mkdir("lept/recog");
64 
65         /* Generate the recognizer */
66     pixa1 = pixaRead("recog/sets/train01.pa");
67     recog = recogCreateFromPixa(pixa1, 0, 0, 0, 128, 1);
68     recogAverageSamples(&recog, 0);
69 
70         /* Show the templates */
71     recogDebugAverages(&recog, 1);
72     recogShowMatchesInRange(recog, recog->pixa_tr, 0.0, 1.0, 1);
73 
74         /* Get a set of problem images to decode */
75     pixa2 = pixaRead("recog/sets/test01.pa");
76 
77         /* Decode a subset of them */
78     debug = 1;
79     for (i = 0; i < 6; i++) {
80 /*        if (i != 3) continue; */
81         item = example[i];
82         pix1 = pixaGetPix(pixa2, item, L_CLONE);
83         pixDisplay(pix1, 100, 100);
84         pix2 = GetBigComponent(pix1);
85         if (debug) {
86             recogIdentifyMultiple(recog, pix2, 0, 0, &boxa, NULL, &pixdb, 1);
87             rchaExtract(recog->rcha, NULL, &nascore, NULL, NULL,
88                         NULL, NULL, NULL);
89             pixDisplay(pixdb, 300, 500);
90             boxaWriteStream(stderr, boxa);
91             numaWriteStream(stderr, nascore);
92             numaDestroy(&nascore);
93             pixDestroy(&pixdb);
94         } else {  /* just get the timing */
95             startTimer();
96             recogIdentifyMultiple(recog, pix2, 0, 0, &boxa, NULL, NULL, 0);
97             fprintf(stderr, "Time: %5.3f\n", stopTimer());
98         }
99         pixDestroy(&pix1);
100         pixDestroy(&pix2);
101         boxaDestroy(&boxa);
102     }
103     if (debug) {
104         pix3 = pixaDisplayTiledInRows(recog->pixadb_split, 1, 200,
105                                       1.0, 0, 20, 3);
106         pixDisplay(pix3, 0, 0);
107         pixDestroy(&pix3);
108     }
109 
110     pixaDestroy(&pixa1);
111     pixaDestroy(&pixa2);
112     recogDestroy(&recog);
113     return 0;
114 }
115 
116 
117 static PIX *
GetBigComponent(PIX * pixs)118 GetBigComponent(PIX  *pixs)
119 {
120 BOX  *box;
121 PIX  *pix1, *pixd;
122 
123     pix1 = pixMorphSequence(pixs, "c40.7 + o20.15 + d25.1", 0);
124     pixClipToForeground(pix1, NULL, &box);
125     pixd = pixClipRectangle(pixs, box, NULL);
126     pixDestroy(&pix1);
127     boxDestroy(&box);
128     return pixd;
129 }
130 
131