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  * findpattern3.c
29  *
30  *    We use pixGenerateSelBoundary() to generate the sels.
31  *
32  *    This is set up with input parameters to work on feyn.tif.
33  *
34  *    (1) We extract an "e" bitmap, generate a hit-miss sel, and
35  *    then produce several 4 bpp colormapped renditions,
36  *    with the pattern either removed or highlighted.
37  *
38  *    (2) We do the same with the word "Caltech".
39  */
40 
41 #include "allheaders.h"
42 
43     /* for pixDisplayHitMissSel() */
44 static const l_uint32  HitColor = 0x33aa4400;
45 static const l_uint32  MissColor = 0xaa44bb00;
46 
main(int argc,char ** argv)47 int main(int    argc,
48          char **argv)
49 {
50 BOX         *box;
51 PIX         *pixs, *pixc, *pixp, *pixsel, *pixhmt;
52 PIX         *pixd1, *pixd2, *pixd3;
53 SEL         *selhm;
54 static char  mainName[] = "findpattern3";
55 
56     if (argc != 1)
57         return ERROR_INT(" Syntax:  findpattern3", mainName, 1);
58 
59     setLeptDebugOK(1);
60     lept_mkdir("lept/hmt");
61 
62         /* -------------------------------------------- *
63          * Extract the pattern for a single character   *
64          * ---------------------------------------------*/
65     pixs = pixRead("feyn.tif");
66     box = boxCreate(599, 1055, 18, 23);
67     pixc = pixClipRectangle(pixs, box, NULL);
68 
69         /* Make a hit-miss sel */
70     selhm = pixGenerateSelBoundary(pixc, 1, 2, 2, 2, 1, 1, 0, 0, &pixp);
71 
72         /* Display the sel */
73     pixsel = pixDisplayHitMissSel(pixp, selhm, 7, HitColor, MissColor);
74     pixDisplay(pixsel, 200, 200);
75     pixWrite("/tmp/lept/hmt/pixsel1.png", pixsel, IFF_PNG);
76 
77         /* Use the Sel to find all instances in the page */
78     startTimer();
79     pixhmt = pixHMT(NULL, pixs, selhm);
80     fprintf(stderr, "Time to find patterns = %7.3f\n", stopTimer());
81 
82         /* Color each instance at full res */
83     pixd1 = pixDisplayMatchedPattern(pixs, pixp, pixhmt, selhm->cx,
84                                      selhm->cy, 0x0000ff00, 1.0, 5);
85     pixWrite("/tmp/lept/hmt/pixd11.png", pixd1, IFF_PNG);
86 
87         /* Color each instance at 0.3 scale */
88     pixd2 = pixDisplayMatchedPattern(pixs, pixp, pixhmt, selhm->cx,
89                                      selhm->cy, 0x0000ff00, 0.5, 5);
90     pixWrite("/tmp/lept/hmt/pixd12.png", pixd2, IFF_PNG);
91 
92         /* Remove each instance from the input image */
93     pixd3 = pixCopy(NULL, pixs);
94     pixRemoveMatchedPattern(pixd3, pixp, pixhmt, selhm->cx,
95                                     selhm->cy, 1);
96     pixWrite("/tmp/lept/hmt/pixr1.png", pixd3, IFF_PNG);
97 
98     boxDestroy(&box);
99     selDestroy(&selhm);
100     pixDestroy(&pixc);
101     pixDestroy(&pixp);
102     pixDestroy(&pixsel);
103     pixDestroy(&pixhmt);
104     pixDestroy(&pixd1);
105     pixDestroy(&pixd2);
106     pixDestroy(&pixd3);
107 
108 
109         /* -------------------------------------------- *
110          *        Extract the pattern for a word        *
111          * ---------------------------------------------*/
112     box = boxCreate(208, 872, 130, 35);
113     pixc = pixClipRectangle(pixs, box, NULL);
114 
115         /* Make a hit-miss sel */
116     selhm = pixGenerateSelBoundary(pixc, 2, 2, 1, 4, 1, 1, 0, 0, &pixp);
117 
118         /* Display the sel */
119     pixsel = pixDisplayHitMissSel(pixp, selhm, 7, HitColor, MissColor);
120     pixDisplay(pixsel, 200, 200);
121     pixWrite("/tmp/lept/hmt/pixsel2.png", pixsel, IFF_PNG);
122 
123         /* Use the Sel to find all instances in the page */
124     startTimer();
125     pixhmt = pixHMT(NULL, pixs, selhm);
126     fprintf(stderr, "Time to find word patterns = %7.3f\n", stopTimer());
127 
128         /* Color each instance at full res */
129     pixd1 = pixDisplayMatchedPattern(pixs, pixp, pixhmt, selhm->cx,
130                                      selhm->cy, 0x0000ff00, 1.0, 5);
131     pixWrite("/tmp/lept/hmt/pixd21.png", pixd1, IFF_PNG);
132 
133         /* Color each instance at 0.3 scale */
134     pixd2 = pixDisplayMatchedPattern(pixs, pixp, pixhmt, selhm->cx,
135                                      selhm->cy, 0x0000ff00, 0.5, 5);
136     pixWrite("/tmp/lept/hmt/pixd22.png", pixd2, IFF_PNG);
137 
138         /* Remove each instance from the input image */
139     pixd3 = pixCopy(NULL, pixs);
140     pixRemoveMatchedPattern(pixd3, pixp, pixhmt, selhm->cx,
141                                     selhm->cy, 1);
142     pixWrite("/tmp/lept/hmt/pixr2.png", pixd3, IFF_PNG);
143 
144     selDestroy(&selhm);
145     boxDestroy(&box);
146     pixDestroy(&pixc);
147     pixDestroy(&pixp);
148     pixDestroy(&pixsel);
149     pixDestroy(&pixhmt);
150     pixDestroy(&pixd1);
151     pixDestroy(&pixd2);
152     pixDestroy(&pixd3);
153     pixDestroy(&pixs);
154     return 0;
155 }
156 
157