Leptonica 1.68
C Image Processing Library

dewarptest.c

Go to the documentation of this file.
00001 /*====================================================================*
00002  -  Copyright (C) 2001 Leptonica.  All rights reserved.
00003  -  This software is distributed in the hope that it will be
00004  -  useful, but with NO WARRANTY OF ANY KIND.
00005  -  No author or distributor accepts responsibility to anyone for the
00006  -  consequences of using this software, or for whether it serves any
00007  -  particular purpose or works at all, unless he or she says so in
00008  -  writing.  Everyone is granted permission to copy, modify and
00009  -  redistribute this source code, for commercial or non-commercial
00010  -  purposes, with the following restrictions: (1) the origin of this
00011  -  source code must not be misrepresented; (2) modified versions must
00012  -  be plainly marked as such; and (3) this notice may not be removed
00013  -  or altered from any source or modified source distribution.
00014  *====================================================================*/
00015 
00016 /*
00017  *   dewarptest.c
00018  *
00019  *   This exercise functions in dewarp.c for dewarping based on lines
00020  *   of horizontal text.  It also creates a 19-image pdf of steps
00021  *   in the process.
00022  */
00023 
00024 #include "allheaders.h"
00025 
00026 #define   DO_QUAD     1
00027 #define   DO_CUBIC    0
00028 #define   DO_QUARTIC  0
00029 
00030 l_int32 main(int    argc,
00031              char **argv)
00032 {
00033 l_int32    i, n, ignore;
00034 l_float32  a, b, c, d, e;
00035 L_DEWARP  *dew;
00036 FILE      *fp;
00037 FPIX      *fpix;
00038 NUMA      *nax, *nay, *nafit;
00039 PIX       *pixs, *pixn, *pixg, *pixb, *pixt1, *pixt2, *pixt3;
00040 PIX       *pixs2, *pixn2, *pixg2, *pixb2, *pixv, *pixd;
00041 PTA       *pta, *ptad;
00042 PTAA      *ptaa1, *ptaa2;
00043 
00044     pixs = pixRead("1555-7.jpg");
00045 
00046         /* Normalize for varying background and binarize */
00047     pixn = pixBackgroundNormSimple(pixs, NULL, NULL);
00048     pixg = pixConvertRGBToGray(pixn, 0.5, 0.3, 0.2);
00049     pixb = pixThresholdToBinary(pixg, 130);
00050 
00051         /* Run the basic functions */
00052     dew = dewarpCreate(pixb, 7, 30, 15, 1);
00053     dewarpBuildModel(dew, 1);
00054     dewarpApplyDisparity(dew, pixg, 1);
00055 
00056         /* Save the intermediate dewarped images */
00057     pixv = pixRead("/tmp/pixv.png");
00058     pixd = pixRead("/tmp/pixd.png");
00059 
00060         /* Normalize another image, that doesn't have enough textlines
00061          * to build an accurate model */
00062     pixs2 = pixRead("1555-3.jpg");
00063     pixn2 = pixBackgroundNormSimple(pixs2, NULL, NULL);
00064     pixg2 = pixConvertRGBToGray(pixn2, 0.5, 0.3, 0.2);
00065     pixb2 = pixThresholdToBinary(pixg2, 130);
00066 
00067         /* Apply the previous disparity model to this image */
00068     dewarpApplyDisparity(dew, pixg2, 1);
00069     dewarpDestroy(&dew);
00070 
00071         /* Get the textline centers */
00072     ptaa1 = pixGetTextlineCenters(pixb, 0);
00073     pixt1 = pixCreateTemplate(pixs);
00074     pixt2 = pixDisplayPtaa(pixt1, ptaa1);
00075     pixWrite("/tmp/textline1.png", pixt2, IFF_PNG);
00076     pixDisplayWithTitle(pixt2, 500, 100, "textline centers", 1);
00077     pixDestroy(&pixt1);
00078 
00079         /* Remove short lines */
00080     fprintf(stderr, "Num all lines = %d\n", ptaaGetCount(ptaa1));
00081     ptaa2 = ptaaRemoveShortLines(pixb, ptaa1, 0.8, 0);
00082 
00083         /* Fit to curve */
00084     n = ptaaGetCount(ptaa2);
00085     fprintf(stderr, "Num long lines = %d\n", n);
00086     for (i = 0; i < n; i++) {
00087         pta = ptaaGetPta(ptaa2, i, L_CLONE);
00088         ptaGetArrays(pta, &nax, NULL);
00089 #if DO_QUAD
00090         ptaGetQuadraticLSF(pta, &a, &b, &c, &nafit);
00091 /*        fprintf(stderr, "a = %7.3f, b = %7.3f, c = %7.3f\n", a, b, c); */
00092 #elif  DO_CUBIC
00093         ptaGetCubicLSF(pta, &a, &b, &c, &d, &nafit);
00094 /*        fprintf(stderr, "a = %7.3f, b = %7.3f, c = %7.3f, d = %7.3f\n",
00095                 a, b, c, d);  */
00096 #elif DO_QUARTIC
00097         ptaGetQuarticLSF(pta, &a, &b, &c, &d, &e, &nafit);
00098 /*        fprintf(stderr,
00099               "a = %7.3f, b = %7.3f, c = %7.3f, d = %7.3f, e = %7.3f\n",
00100               a, b, c, d, e); */
00101 #endif
00102         ptad = ptaCreateFromNuma(nax, nafit);
00103         pixDisplayPta(pixt2, pixt2, ptad);
00104         ptaDestroy(&pta);
00105         ptaDestroy(&ptad);
00106         numaDestroy(&nax);
00107         numaDestroy(&nafit);
00108     }
00109 
00110     pixDisplayWithTitle(pixt2, 700, 100, "fitted lines superimposed", 1);
00111     pixWrite("/tmp/textline2.png", pixt2, IFF_PNG);
00112     ptaaDestroy(&ptaa1);
00113     ptaaDestroy(&ptaa2);
00114     pixDestroy(&pixt2);
00115 
00116          /* Write out the files to be imaged */
00117     lept_mkdir("junkdir");
00118     pixWrite("/tmp/junkdir/001.jpg", pixs, IFF_JFIF_JPEG);
00119     pixWrite("/tmp/junkdir/002.jpg", pixn, IFF_JFIF_JPEG);
00120     pixWrite("/tmp/junkdir/003.jpg", pixg, IFF_JFIF_JPEG);
00121     pixWrite("/tmp/junkdir/004.png", pixb, IFF_TIFF_G4);
00122     pixt1 = pixRead("/tmp/textline1.png");
00123     pixWrite("/tmp/junkdir/005.png", pixt1, IFF_PNG);
00124     pixDestroy(&pixt1);
00125     pixt1 = pixRead("/tmp/textline2.png");
00126     pixWrite("/tmp/junkdir/006.png", pixt1, IFF_PNG);
00127     pixDestroy(&pixt1);
00128     pixt1 = pixRead("/tmp/lines1.png");
00129     pixWrite("/tmp/junkdir/007.png", pixt1, IFF_PNG);
00130     pixDestroy(&pixt1);
00131     pixt1 = pixRead("/tmp/lines2.png");
00132     pixWrite("/tmp/junkdir/008.png", pixt1, IFF_PNG);
00133     pixDestroy(&pixt1);
00134     pixt1 = pixRead("/tmp/vert-contours.png");
00135     pixWrite("/tmp/junkdir/009.png", pixt1, IFF_PNG);
00136     pixDestroy(&pixt1);
00137     pixWrite("/tmp/junkdir/010.png", pixv, IFF_PNG);
00138     pixt1 = pixThresholdToBinary(pixv, 130);
00139     pixWrite("/tmp/junkdir/011.png", pixt1, IFF_PNG);
00140     pixDestroy(&pixt1);
00141     pixt1 = pixRead("/tmp/horiz-contours.png");
00142     pixWrite("/tmp/junkdir/012.png", pixt1, IFF_PNG);
00143     pixDestroy(&pixt1);
00144     pixWrite("/tmp/junkdir/013.png", pixd, IFF_PNG);
00145     pixt1 = pixThresholdToBinary(pixd, 130);
00146     pixWrite("/tmp/junkdir/014.png", pixt1, IFF_PNG);
00147     pixDestroy(&pixt1);
00148     pixWrite("/tmp/junkdir/015.png", pixb, IFF_TIFF_G4);
00149 
00150         /* (these are for the second image) */
00151     pixWrite("/tmp/junkdir/016.jpg", pixs2, IFF_JFIF_JPEG);
00152     pixWrite("/tmp/junkdir/017.png", pixb2, IFF_TIFF_G4);
00153     pixt1 = pixRead("/tmp/pixv.png");
00154     pixt2 = pixThresholdToBinary(pixt1, 130);
00155     pixWrite("/tmp/junkdir/018.png", pixt2, IFF_PNG);
00156     pixDestroy(&pixt1);
00157     pixDestroy(&pixt2);
00158     pixt1 = pixRead("/tmp/pixd.png");
00159     pixt2 = pixThresholdToBinary(pixt1, 130);
00160     pixWrite("/tmp/junkdir/019.png", pixt2, IFF_PNG);
00161     pixDestroy(&pixt1);
00162     pixDestroy(&pixt2);
00163 
00164         /* Generate the 19 page ps and pdf files */
00165     convertFilesToPS("/tmp/junkdir", NULL, 135, "/tmp/dewarp.ps");
00166     fprintf(stderr, "ps file made: /tmp/dewarp.ps\n");
00167     ignore = system("ps2pdf /tmp/dewarp.ps /tmp/dewarp.pdf");
00168     fprintf(stderr, "pdf file made: /tmp/dewarp.pdf\n");
00169 
00170     pixDestroy(&pixs);
00171     pixDestroy(&pixn);
00172     pixDestroy(&pixg);
00173     pixDestroy(&pixb);
00174     pixDestroy(&pixs2);
00175     pixDestroy(&pixn2);
00176     pixDestroy(&pixg2);
00177     pixDestroy(&pixb2);
00178     pixDestroy(&pixv);
00179     pixDestroy(&pixd);
00180 
00181     return 0;
00182 }
00183 
00184 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines