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  * baselinetest.c
29  *
30  *   This tests two things:
31  *   (1) The ability to find a projective transform that will deskew
32  *       textlines in an image with keystoning.
33  *   (2) The ability to find baselines in a text image.
34  *   (3) The ability to clean background to white in a dark and
35  *       mottled text image.
36  */
37 
38 #include "allheaders.h"
39 
main(int argc,char ** argv)40 int main(int    argc,
41          char **argv)
42 {
43 NUMA         *na;
44 PIX          *pixs, *pix1, *pix2, *pix3, *pix4, *pix5;
45 PIXA         *pixadb;
46 PTA          *pta;
47 L_REGPARAMS  *rp;
48 
49     if (regTestSetup(argc, argv, &rp))
50         return 1;
51 
52     pixs = pixRead("keystone.png");
53 
54         /* Test function for deskewing using projective transform
55 	 * on linear approximation for local skew angle */
56     pix1 = pixDeskewLocal(pixs, 10, 0, 0, 0.0, 0.0, 0.0);
57     regTestWritePixAndCheck(rp, pix1, IFF_PNG);  /* 0 */
58 
59         /* Test function for finding local skew angles */
60     na = pixGetLocalSkewAngles(pixs, 10, 0, 0, 0.0, 0.0, 0.0, NULL, NULL, 1);
61     gplotSimple1(na, GPLOT_PNG, "/tmp/lept/baseline/ang", "Angles in degrees");
62     pix2 = pixRead("/tmp/lept/baseline/ang.png");
63     pix3 = pixRead("/tmp/lept/baseline/skew.png");
64     regTestWritePixAndCheck(rp, pix2, IFF_PNG);  /* 1 */
65     regTestWritePixAndCheck(rp, pix3, IFF_PNG);  /* 2 */
66     pixDisplayWithTitle(pix2, 0, 550, NULL, rp->display);
67     pixDisplayWithTitle(pix3, 700, 550, NULL, rp->display);
68     numaDestroy(&na);
69     pixDestroy(&pix2);
70     pixDestroy(&pix3);
71     pixDestroy(&pixs);
72 
73         /* Test baseline finder */
74     pixadb = pixaCreate(6);
75     na = pixFindBaselines(pix1, &pta, pixadb);
76     pix2 = pixRead("/tmp/lept/baseline/diff.png");
77     pix3 = pixRead("/tmp/lept/baseline/loc.png");
78     pix4 = pixRead("/tmp/lept/baseline/baselines.png");
79     regTestWritePixAndCheck(rp, pix2, IFF_PNG);  /* 3 */
80     regTestWritePixAndCheck(rp, pix3, IFF_PNG);  /* 4 */
81     regTestWritePixAndCheck(rp, pix4, IFF_PNG);  /* 5 */
82     pixDisplayWithTitle(pix2, 0, 0, NULL, rp->display);
83     pixDisplayWithTitle(pix3, 700, 0, NULL, rp->display);
84     pixDisplayWithTitle(pix4, 1350, 0, NULL, rp->display);
85     pix5 = pixaDisplayTiledInRows(pixadb, 32, 1500, 1.0, 0, 30, 2);
86     pixDisplayWithTitle(pix5, 0, 500, NULL, rp->display);
87     regTestWritePixAndCheck(rp, pix5, IFF_PNG);  /* 6 */
88     pixDestroy(&pix1);
89     pixDestroy(&pix2);
90     pixDestroy(&pix3);
91     pixDestroy(&pix4);
92     pixDestroy(&pix5);
93     pixaDestroy(&pixadb);
94     numaDestroy(&na);
95     ptaDestroy(&pta);
96 
97         /* Another test for baselines, with dark image */
98     pixadb = pixaCreate(6);
99     pixs = pixRead("pedante.079.jpg");  /* 75 ppi */
100     pix1 = pixRemoveBorder(pixs, 30);
101     pixaAddPix(pixadb, pix1, L_COPY);
102     pix2 = pixConvertRGBToGray(pix1, 0.33, 0.34, 0.33);
103     pix3 = pixScale(pix2, 4.0, 4.0);   /* scale up to 300 ppi */
104     pix4 = pixCleanBackgroundToWhite(pix3, NULL, NULL, 1.0, 70, 170);
105     pix5 = pixThresholdToBinary(pix4, 170);
106     regTestWritePixAndCheck(rp, pix5, IFF_PNG);  /* 7 */
107     pixaAddPix(pixadb, pixScale(pix5, 0.25, 0.25), L_INSERT);
108     pixDestroy(&pix1);
109     pixDestroy(&pix2);
110     pixDestroy(&pix3);
111     pixDestroy(&pix4);
112     pix1 = pixDeskew(pix5, 2);
113     na = pixFindBaselines(pix1, &pta, pixadb);
114     pix2 = pixaDisplayTiledInRows(pixadb, 32, 1500, 1.0, 0, 30, 2);
115     regTestWritePixAndCheck(rp, pix2, IFF_PNG);  /* 8 */
116     pixDisplayWithTitle(pix2, 800, 500, NULL, rp->display);
117     pixaDestroy(&pixadb);
118     pixDestroy(&pixs);
119     pixDestroy(&pix1);
120     pixDestroy(&pix2);
121     pixDestroy(&pix5);
122     numaDestroy(&na);
123     ptaDestroy(&pta);
124 
125     return regTestCleanup(rp);
126 }
127