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