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  *  recogtest5.c
29  *
30  *     Test document image decoding (DID) approach to splitting characters
31  *
32  *     This uses recogIdentifyMultiple() to first split the touching
33  *     characters and then do the identification on the resulting
34  *     single characters.  Compare with recogtest4.c.
35  */
36 
37 #include "string.h"
38 #include "allheaders.h"
39 
40 static PIX *GetBigComponent(PIX *pixs);
41 
42 
main(int argc,char ** argv)43 l_int32 main(int    argc,
44              char **argv)
45 {
46 char      buf[256];
47 l_int32   i, n, item;
48 l_int32   example[6] = {17, 20, 21, 22, 23, 24};  /* for decoding */
49 BOXA     *boxa;
50 PIX      *pix1, *pix2, *pix3, *pixdb;
51 PIXA     *pixa1, *pixa2, *pixa3;
52 L_RECOG  *recog;
53 
54     if (argc != 1) {
55         fprintf(stderr, " Syntax: recogtest5\n");
56         return 1;
57     }
58 
59     setLeptDebugOK(1);
60     lept_mkdir("lept/recog");
61 
62         /* Generate the recognizer */
63     pixa1 = pixaRead("recog/sets/train01.pa");
64     recog = recogCreateFromPixa(pixa1, 0, 0, 0, 128, 1);  /* no scaling */
65     recogAverageSamples(&recog, 1);
66     recogWrite("/tmp/lept/recog/rec1.rec", recog);
67 
68         /* Show the templates */
69     recogDebugAverages(&recog, 1);
70     recogShowMatchesInRange(recog, recog->pixa_tr, 0.0, 1.0, 1);
71 
72         /* Get a set of problem images to decode */
73     pixa2 = pixaRead("recog/sets/test01.pa");
74 
75         /* Decode a subset of them.  It takes about 2 ms to decode a
76          * 4 digit number (Viterbi for splitting; identification against
77          * all templates; debug off. */
78     for (i = 0; i < 6; i++) {
79 /*        if (i != 3) continue;  */ /* remove this comment to do all 6 */
80         item = example[i];
81         pix1 = pixaGetPix(pixa2, item, L_CLONE);
82         pixDisplay(pix1, 100, 100);
83         pix2 = GetBigComponent(pix1);
84         recogIdentifyMultiple(recog, pix2, 0, 0, NULL, &pixa3, NULL, 1);
85         pix3 = pixaDisplayTiledInColumns(pixa3, 1, 1.0, 20, 2);
86         pixDisplay(pix3, 800, 100);
87         pixDestroy(&pix1);
88         pixDestroy(&pix2);
89         pixDestroy(&pix3);
90         pixaDestroy(&pixa3);
91     }
92 
93     pixaDestroy(&pixa1);
94     pixaDestroy(&pixa2);
95     recogDestroy(&recog);
96     return 0;
97 }
98 
99 static PIX *
GetBigComponent(PIX * pixs)100 GetBigComponent(PIX  *pixs)
101 {
102 BOX  *box;
103 PIX  *pix1, *pixd;
104 
105     pix1 = pixMorphSequence(pixs, "c40.7 + o20.15 + d25.1", 0);
106     pixClipToForeground(pix1, NULL, &box);
107     pixd = pixClipRectangle(pixs, box, NULL);
108     pixDestroy(&pix1);
109     boxDestroy(&box);
110     return pixd;
111 }
112 
113 
114