Leptonica 1.68
C Image Processing Library

dewarp_reg.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  *   dewarp_reg.c
00018  *
00019  *     Regression test for image dewarp based on text lines
00020  *
00021  *     We also test some of the fpix and dpix functions (scaling,
00022  *     serialization, interconversion)
00023  */
00024 
00025 #include "allheaders.h"
00026 
00027 l_int32 main(int    argc,
00028              char **argv)
00029 {
00030 l_int32       i, n;
00031 l_float32     a, b, c;
00032 L_DEWARP     *dew, *dew2;
00033 DPIX         *dpix1, *dpix2, *dpix3;
00034 FPIX         *fpix1, *fpix2, *fpix3;
00035 NUMA         *nax, *nafit;
00036 PIX          *pixs, *pixn, *pixg, *pixb, *pixt1, *pixt2;
00037 PIX          *pixs2, *pixn2, *pixg2, *pixb2;
00038 PTA          *pta, *ptad;
00039 PTAA         *ptaa1, *ptaa2;
00040 L_REGPARAMS  *rp;
00041 
00042     if (regTestSetup(argc, argv, &rp))
00043               return 1;
00044 
00045     pixs = pixRead("1555-7.jpg");
00046     
00047         /* Normalize for varying background and binarize */
00048     pixn = pixBackgroundNormSimple(pixs, NULL, NULL);
00049     pixg = pixConvertRGBToGray(pixn, 0.5, 0.3, 0.2);
00050     pixb = pixThresholdToBinary(pixg, 130);
00051     pixDestroy(&pixn);
00052     pixDestroy(&pixg);
00053     regTestWritePixAndCheck(rp, pixb, IFF_PNG);  /* 0 */
00054     pixDisplayWithTitle(pixb, 0, 0, "binarized input", rp->display);
00055 
00056         /* Get the textline centers */
00057     ptaa1 = pixGetTextlineCenters(pixb, 0);
00058     pixt1 = pixCreateTemplate(pixs);
00059     pixt2 = pixDisplayPtaa(pixt1, ptaa1);
00060     regTestWritePixAndCheck(rp, pixt2, IFF_PNG);  /* 1 */
00061     pixDisplayWithTitle(pixt2, 0, 500, "textline centers", rp->display);
00062     pixDestroy(&pixt1);
00063 
00064         /* Remove short lines */
00065     ptaa2 = ptaaRemoveShortLines(pixb, ptaa1, 0.8, 0);
00066 
00067         /* Fit to quadratic */
00068     n = ptaaGetCount(ptaa2);
00069     for (i = 0; i < n; i++) {
00070         pta = ptaaGetPta(ptaa2, i, L_CLONE);
00071         ptaGetArrays(pta, &nax, NULL);
00072         ptaGetQuadraticLSF(pta, &a, &b, &c, &nafit);
00073         ptad = ptaCreateFromNuma(nax, nafit);
00074         pixDisplayPta(pixt2, pixt2, ptad);
00075         ptaDestroy(&pta);
00076         ptaDestroy(&ptad);
00077         numaDestroy(&nax);
00078         numaDestroy(&nafit);
00079     }
00080     regTestWritePixAndCheck(rp, pixt2, IFF_PNG);  /* 2 */
00081     pixDisplayWithTitle(pixt2, 300, 500, "fitted lines superimposed",
00082                         rp->display);
00083     ptaaDestroy(&ptaa1);
00084     ptaaDestroy(&ptaa2);
00085     pixDestroy(&pixt2);
00086 
00087         /* Run with only vertical disparity correction */
00088     if ((dew = dewarpCreate(pixb, 7, 30, 15, 0)) == NULL)
00089         return ERROR_INT("\n\n\n FAILURE !!! \n\n\n", rp->testname, 1);
00090     dewarpBuildModel(dew, 0);
00091     dewarpApplyDisparity(dew, pixb, 0);
00092     regTestWritePixAndCheck(rp, dew->pixd, IFF_PNG);  /* 3 */
00093     pixDisplayWithTitle(dew->pixd, 400, 0, "fixed for vert disparity",
00094                         rp->display);
00095     dewarpDestroy(&dew);
00096 
00097         /* Run with both vertical and horizontal disparity correction */
00098     if ((dew = dewarpCreate(pixb, 7, 30, 15, 1)) == NULL)
00099         return ERROR_INT("\n\n\n FAILURE !!! \n\n\n", rp->testname, 1);
00100     dewarpBuildModel(dew, 0);
00101     dewarpApplyDisparity(dew, pixb, 0);
00102     regTestWritePixAndCheck(rp, dew->pixd, IFF_PNG);  /* 4 */
00103     pixDisplayWithTitle(dew->pixd, 800, 0, "fixed for both disparities",
00104                         rp->display);
00105 
00106         /* Read another image, normalize background and binarize */
00107     pixs2 = pixRead("1555-3.jpg");
00108     pixn2 = pixBackgroundNormSimple(pixs2, NULL, NULL);
00109     pixg2 = pixConvertRGBToGray(pixn2, 0.5, 0.3, 0.2);
00110     pixb2 = pixThresholdToBinary(pixg2, 130);
00111     pixDestroy(&pixn2);
00112     pixDestroy(&pixg2);
00113     regTestWritePixAndCheck(rp, pixb, IFF_PNG);  /* 5 */
00114     pixDisplayWithTitle(pixb, 0, 400, "binarized input (2)", rp->display);
00115 
00116         /* Minimize and re-apply previous disparity to this image */
00117     dewarpMinimize(dew);
00118     dewarpApplyDisparity(dew, pixb2, 0);
00119     regTestWritePixAndCheck(rp, dew->pixd, IFF_PNG);  /* 6 */
00120     pixDisplayWithTitle(dew->pixd, 400, 400, "fixed (2) for both disparities",
00121                         rp->display);
00122 
00123         /* Write and read back minimized dewarp struct */
00124     dewarpWrite("/tmp/dewarp.7.dew", dew);
00125     regTestCheckFile(rp, "/tmp/dewarp.7.dew");  /* 7 */
00126     dew2 = dewarpRead("/tmp/dewarp.7.dew");
00127     dewarpWrite("/tmp/dewarp.8.dew", dew2);
00128     regTestCheckFile(rp, "/tmp/dewarp.8.dew");  /* 8 */
00129     regTestCompareFiles(rp, 7, 8);  /* 9 */
00130 
00131         /* Apply dew2 to pixb2 */
00132     dewarpApplyDisparity(dew2, pixb2, 0);
00133     regTestWritePixAndCheck(rp, dew2->pixd, IFF_PNG);  /* 10 */
00134     pixDisplayWithTitle(dew->pixd, 800, 400, "fixed (3) for both disparities",
00135                         rp->display);
00136 
00137         /* Minimize, repopulate disparity arrays, and apply again */
00138     dewarpMinimize(dew2);
00139     dewarpApplyDisparity(dew2, pixb2, 0);
00140     regTestWritePixAndCheck(rp, dew2->pixd, IFF_PNG);  /* 11 */
00141     regTestCompareFiles(rp, 10, 11);  /* 12 */
00142     pixDisplayWithTitle(dew->pixd, 900, 400, "fixed (4) for both disparities",
00143                         rp->display);
00144 
00145         /* Test a few of the fpix functions */
00146     fpix1 = fpixClone(dew->sampvdispar);
00147     fpixWrite("/tmp/sampv.13.fpix", fpix1);
00148     regTestCheckFile(rp, "/tmp/sampv.13.fpix");  /* 13 */
00149     fpix2 = fpixRead("/tmp/sampv.13.fpix");
00150     fpixWrite("/tmp/sampv.14.fpix", fpix2);
00151     regTestCheckFile(rp, "/tmp/sampv.14.fpix");  /* 14 */
00152     regTestCompareFiles(rp, 13, 14);  /* 15 */
00153     fpix3 = fpixScaleByInteger(fpix2, 30);
00154     pixt1 = fpixRenderContours(fpix3, -2., 2.0, 0.2);
00155     regTestWritePixAndCheck(rp, pixt1, IFF_PNG);  /* 16 */
00156     pixDisplayWithTitle(pixt1, 0, 800, "v. disparity contours", rp->display);
00157     fpixDestroy(&fpix1);
00158     fpixDestroy(&fpix2);
00159     fpixDestroy(&fpix3);
00160     pixDestroy(&pixt1);
00161 
00162         /* Test a few of the dpix functions */
00163     dpix1 = fpixConvertToDPix(dew->sampvdispar);
00164     dpixWrite("/tmp/sampv.17.dpix", dpix1);
00165     regTestCheckFile(rp, "/tmp/sampv.17.dpix");  /* 17 */
00166     dpix2 = dpixRead("/tmp/sampv.17.dpix");
00167     dpixWrite("/tmp/sampv.18.dpix", dpix2);
00168     regTestCheckFile(rp, "/tmp/sampv.18.dpix");  /* 18 */
00169     regTestCompareFiles(rp, 17, 18);  /* 19 */
00170     dpix3 = dpixScaleByInteger(dpix2, 30);
00171     fpix3 = dpixConvertToFPix(dpix3);
00172     pixt1 = fpixRenderContours(fpix3, -2., 2.0, 0.2);
00173     regTestWritePixAndCheck(rp, pixt1, IFF_PNG);  /* 20 */
00174     pixDisplayWithTitle(pixt1, 400, 800, "v. disparity contours", rp->display);
00175     regTestCompareFiles(rp, 16, 20);  /* 21 */
00176     dpixDestroy(&dpix1);
00177     dpixDestroy(&dpix2);
00178     dpixDestroy(&dpix3);
00179     fpixDestroy(&fpix3);
00180     pixDestroy(&pixt1);
00181 
00182     dewarpDestroy(&dew);
00183     dewarpDestroy(&dew2);
00184     pixDestroy(&pixs);
00185     pixDestroy(&pixb);
00186     pixDestroy(&pixs2);
00187     pixDestroy(&pixb2);
00188     regTestCleanup(rp);
00189     return 0;
00190 }
00191 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines