Leptonica 1.68
C Image Processing Library

binmorph5_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  *  binmorph5_reg.c
00018  *
00019  *    Regression test for expanded dwa morph operations.
00020  *    We compare:
00021  *       (1) dwa composite     vs.    morph composite
00022  *       (2) dwa composite     vs.    morph non-composite
00023  */
00024 
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include "allheaders.h"
00028 
00029 l_int32 DoComparisonDwa1(PIX *pixs, PIX *pixt1, PIX *pixt2, PIX *pixt3,
00030                          PIX *pixt4, PIX *pixt5, PIX *pixt6, l_int32 isize);
00031 l_int32 DoComparisonDwa2(PIX *pixs, PIX *pixt1, PIX *pixt2, PIX *pixt3,
00032                          PIX *pixt4, PIX *pixt5, PIX *pixt6, l_int32 size);
00033 l_int32 PixCompareDwa(l_int32 size, const char *type, PIX *pixt1, PIX *pixt2,
00034                       PIX *pixt3, PIX *pixt4, PIX *pixt5, PIX *pixt6);
00035 
00036 #define  TIMING         0
00037 #define  FASTER_TEST    1
00038 #define  SLOWER_TEST    1
00039 
00040     /* This fails on the symmetric case, but the differences are
00041      * relatively small.  Most of the problems seems to be in the
00042      * non-dwa code, because we are doing sequential erosions
00043      * without an extra border, and things aren't being properly
00044      * initialized.  To avoid these errors, add a border in advance
00045      * for symmetric b.c.  Note that asymmetric b.c. are recommended
00046      * for document image operations, and this test passes for
00047      * asymmetric b.c. */
00048 #define    TEST_SYMMETRIC   0     /* set to 1 for symmetric b.c.;
00049                                      otherwise, it tests asymmetric b.c. */
00050 
00051 
00052 main(int    argc,
00053      char **argv)
00054 {
00055 l_int32      i, n, rsize, fact1, fact2, extra;
00056 l_int32      size, lastsize;
00057 l_int32      dwasize[256];
00058 l_int32      ropsize[256];
00059 PIX         *pixs, *pixt0, *pixt1, *pixt2, *pixt3, *pixt4, *pixt5, *pixt6;
00060 static char  mainName[] = "binmorph5_reg";
00061 
00062     pixs = pixRead("feyn.tif");
00063 
00064 #if TEST_SYMMETRIC
00065         /* This works properly if there's an added border */
00066     resetMorphBoundaryCondition(SYMMETRIC_MORPH_BC);
00067 #if 1
00068     pixt1 = pixAddBorder(pixs, 64, 0);
00069     pixTransferAllData(pixs, &pixt1, 0, 0);
00070 #endif
00071 #endif  /* TEST_SYMMETRIC */
00072 
00073     pixt1 = pixCreateTemplateNoInit(pixs);
00074     pixt2 = pixCreateTemplateNoInit(pixs);
00075     pixt3 = pixCreateTemplateNoInit(pixs);
00076     pixt4 = pixCreateTemplateNoInit(pixs);
00077     pixt5 = pixCreateTemplateNoInit(pixs);
00078     pixt6 = pixCreateTemplateNoInit(pixs);
00079     
00080 
00081     /* ---------------------------------------------------------------- *
00082      *                  Faster test; testing fewer sizes                *
00083      * ---------------------------------------------------------------- */
00084 #if  FASTER_TEST 
00085         /* Compute the actual sizes used for each input size 'i' */
00086     for (i = 0; i < 256; i++) {
00087         dwasize[i] = 0;
00088         ropsize[i] = 0;
00089     }
00090     for (i = 65; i < 256; i++) {
00091         selectComposableSizes(i, &fact1, &fact2);
00092         rsize = fact1 * fact2;
00093         ropsize[i] = rsize;
00094         getExtendedCompositeParameters(i, &n, &extra, &dwasize[i]);
00095     }
00096 
00097         /* Use only values where the resulting sizes are equal */
00098     for (i = 65; i < 240; i++) {
00099         n = 1 + (l_int32)((i - 63) / 62);
00100         extra = i - 63 - (n - 1) * 62 + 1;
00101         if (extra == 2) continue;  /* don't use this one (e.g., i == 126) */
00102         if (ropsize[i] == dwasize[i])
00103             DoComparisonDwa1(pixs, pixt1, pixt2, pixt3, pixt4,
00104                              pixt5, pixt6, i);
00105     }
00106 #endif  /* FASTER_TEST */
00107 
00108     /* ---------------------------------------------------------------- *
00109      *          Slower test; testing maximum number of sizes            *
00110      * ---------------------------------------------------------------- */
00111 #if  SLOWER_TEST 
00112     lastsize = 0;
00113     for (i = 65; i < 199; i++) {
00114         getExtendedCompositeParameters(i, &n, &extra, &size);
00115         if (size == lastsize) continue;
00116         if (size == 126 || size == 188) continue;  /* deliberately off by one */
00117         lastsize = size;
00118         DoComparisonDwa2(pixs, pixt1, pixt2, pixt3, pixt4,
00119                          pixt5, pixt6, size);
00120     }
00121 #endif  /* SLOWER_TEST */
00122 
00123     pixDestroy(&pixs);
00124     pixDestroy(&pixt1);
00125     pixDestroy(&pixt2);
00126     pixDestroy(&pixt3);
00127     pixDestroy(&pixt4);
00128     pixDestroy(&pixt5);
00129     pixDestroy(&pixt6);
00130     return 0;
00131 }
00132 
00133 
00134 l_int32
00135 DoComparisonDwa1(PIX     *pixs,
00136                  PIX     *pixt1,
00137                  PIX     *pixt2,
00138                  PIX     *pixt3,
00139                  PIX     *pixt4,
00140                  PIX     *pixt5,
00141                  PIX     *pixt6,
00142                  l_int32  isize)
00143 {
00144 l_int32   fact1, fact2, size;
00145 
00146     selectComposableSizes(isize, &fact1, &fact2);
00147     size = fact1 * fact2;
00148     
00149     fprintf(stderr, "..%d..", size);
00150 
00151     if (TIMING) startTimer();
00152     pixDilateCompBrickExtendDwa(pixt1, pixs, size, 1);
00153     pixDilateCompBrickExtendDwa(pixt3, pixs, 1, size);
00154     pixDilateCompBrickExtendDwa(pixt5, pixs, size, size);
00155     if (TIMING) fprintf(stderr, "Time Dwa: %7.3f sec\n", stopTimer());
00156     if (TIMING) startTimer();
00157     pixDilateCompBrick(pixt2, pixs, size, 1);
00158     pixDilateCompBrick(pixt4, pixs, 1, size);
00159     pixDilateCompBrick(pixt6, pixs, size, size);
00160     if (TIMING) fprintf(stderr, "Time Rop: %7.3f sec\n", stopTimer());
00161     PixCompareDwa(size, "dilate", pixt1, pixt2, pixt3, pixt4, pixt5, pixt6);
00162 
00163     if (TIMING) startTimer();
00164     pixErodeCompBrickExtendDwa(pixt1, pixs, size, 1);
00165     pixErodeCompBrickExtendDwa(pixt3, pixs, 1, size);
00166     pixErodeCompBrickExtendDwa(pixt5, pixs, size, size);
00167     if (TIMING) fprintf(stderr, "Time Dwa: %7.3f sec\n", stopTimer());
00168     if (TIMING) startTimer();
00169     pixErodeCompBrick(pixt2, pixs, size, 1);
00170     pixErodeCompBrick(pixt4, pixs, 1, size);
00171     pixErodeCompBrick(pixt6, pixs, size, size);
00172     if (TIMING) fprintf(stderr, "Time Rop: %7.3f sec\n", stopTimer());
00173     PixCompareDwa(size, "erode", pixt1, pixt2, pixt3, pixt4, pixt5, pixt6);
00174 
00175     if (TIMING) startTimer();
00176     pixOpenCompBrickExtendDwa(pixt1, pixs, size, 1);
00177     pixOpenCompBrickExtendDwa(pixt3, pixs, 1, size);
00178     pixOpenCompBrickExtendDwa(pixt5, pixs, size, size);
00179     if (TIMING) fprintf(stderr, "Time Dwa: %7.3f sec\n", stopTimer());
00180     if (TIMING) startTimer();
00181     pixOpenCompBrick(pixt2, pixs, size, 1);
00182     pixOpenCompBrick(pixt4, pixs, 1, size);
00183     pixOpenCompBrick(pixt6, pixs, size, size);
00184     if (TIMING) fprintf(stderr, "Time Rop: %7.3f sec\n", stopTimer());
00185     PixCompareDwa(size, "open", pixt1, pixt2, pixt3, pixt4, pixt5, pixt6);
00186 
00187     if (TIMING) startTimer();
00188     pixCloseCompBrickExtendDwa(pixt1, pixs, size, 1);
00189     pixCloseCompBrickExtendDwa(pixt3, pixs, 1, size);
00190     pixCloseCompBrickExtendDwa(pixt5, pixs, size, size);
00191     if (TIMING) fprintf(stderr, "Time Dwa: %7.3f sec\n", stopTimer());
00192     if (TIMING) startTimer();
00193     pixCloseSafeCompBrick(pixt2, pixs, size, 1);
00194     pixCloseSafeCompBrick(pixt4, pixs, 1, size);
00195     pixCloseSafeCompBrick(pixt6, pixs, size, size);
00196     if (TIMING) fprintf(stderr, "Time Rop: %7.3f sec\n", stopTimer());
00197     PixCompareDwa(size, "close", pixt1, pixt2, pixt3, pixt4, pixt5, pixt6);
00198 
00199 #if 0
00200     pixWrite("/tmp/junkpixt3.png", pixt3, IFF_PNG);
00201     pixWrite("/tmp/junkpixt4.png", pixt4, IFF_PNG);
00202     pixXor(pixt3, pixt3, pixt4);
00203     pixWrite("/tmp/junkxor.png", pixt3, IFF_PNG);
00204 #endif
00205 
00206     return 0;
00207 }
00208 
00209 
00210 l_int32
00211 DoComparisonDwa2(PIX     *pixs,
00212                  PIX     *pixt1,
00213                  PIX     *pixt2,
00214                  PIX     *pixt3,
00215                  PIX     *pixt4,
00216                  PIX     *pixt5,
00217                  PIX     *pixt6,
00218                  l_int32  size)  /* exactly decomposable */
00219 {
00220     fprintf(stderr, "..%d..", size);
00221 
00222     if (TIMING) startTimer();
00223     pixDilateCompBrickExtendDwa(pixt1, pixs, size, 1);
00224     pixDilateCompBrickExtendDwa(pixt3, pixs, 1, size);
00225     pixDilateCompBrickExtendDwa(pixt5, pixs, size, size);
00226     if (TIMING) fprintf(stderr, "Time Dwa: %7.3f sec\n", stopTimer());
00227     if (TIMING) startTimer();
00228     pixDilateBrick(pixt2, pixs, size, 1);
00229     pixDilateBrick(pixt4, pixs, 1, size);
00230     pixDilateBrick(pixt6, pixs, size, size);
00231     if (TIMING) fprintf(stderr, "Time Rop: %7.3f sec\n", stopTimer());
00232     PixCompareDwa(size, "dilate", pixt1, pixt2, pixt3, pixt4, pixt5, pixt6);
00233 
00234     if (TIMING) startTimer();
00235     pixErodeCompBrickExtendDwa(pixt1, pixs, size, 1);
00236     pixErodeCompBrickExtendDwa(pixt3, pixs, 1, size);
00237     pixErodeCompBrickExtendDwa(pixt5, pixs, size, size);
00238     if (TIMING) fprintf(stderr, "Time Dwa: %7.3f sec\n", stopTimer());
00239     if (TIMING) startTimer();
00240     pixErodeBrick(pixt2, pixs, size, 1);
00241     pixErodeBrick(pixt4, pixs, 1, size);
00242     pixErodeBrick(pixt6, pixs, size, size);
00243     if (TIMING) fprintf(stderr, "Time Rop: %7.3f sec\n", stopTimer());
00244     PixCompareDwa(size, "erode", pixt1, pixt2, pixt3, pixt4, pixt5, pixt6);
00245 
00246     if (TIMING) startTimer();
00247     pixOpenCompBrickExtendDwa(pixt1, pixs, size, 1);
00248     pixOpenCompBrickExtendDwa(pixt3, pixs, 1, size);
00249     pixOpenCompBrickExtendDwa(pixt5, pixs, size, size);
00250     if (TIMING) fprintf(stderr, "Time Dwa: %7.3f sec\n", stopTimer());
00251     if (TIMING) startTimer();
00252     pixOpenBrick(pixt2, pixs, size, 1);
00253     pixOpenBrick(pixt4, pixs, 1, size);
00254     pixOpenBrick(pixt6, pixs, size, size);
00255     if (TIMING) fprintf(stderr, "Time Rop: %7.3f sec\n", stopTimer());
00256     PixCompareDwa(size, "open", pixt1, pixt2, pixt3, pixt4, pixt5, pixt6);
00257 
00258     if (TIMING) startTimer();
00259     pixCloseCompBrickExtendDwa(pixt1, pixs, size, 1);
00260     pixCloseCompBrickExtendDwa(pixt3, pixs, 1, size);
00261     pixCloseCompBrickExtendDwa(pixt5, pixs, size, size);
00262     if (TIMING) fprintf(stderr, "Time Dwa: %7.3f sec\n", stopTimer());
00263     if (TIMING) startTimer();
00264     pixCloseSafeBrick(pixt2, pixs, size, 1);
00265     pixCloseSafeBrick(pixt4, pixs, 1, size);
00266     pixCloseSafeBrick(pixt6, pixs, size, size);
00267     if (TIMING) fprintf(stderr, "Time Rop: %7.3f sec\n", stopTimer());
00268     PixCompareDwa(size, "close", pixt1, pixt2, pixt3, pixt4, pixt5, pixt6);
00269 
00270 #if 0
00271     pixWrite("/tmp/junkpixt3.png", pixt3, IFF_PNG);
00272     pixWrite("/tmp/junkpixt4.png", pixt4, IFF_PNG);
00273     pixXor(pixt3, pixt3, pixt4);
00274     pixWrite("/tmp/junkxor.png", pixt3, IFF_PNG);
00275 #endif
00276 
00277     return 0;
00278 }
00279 
00280 
00281 l_int32
00282 PixCompareDwa(l_int32      size,
00283               const char  *type,
00284               PIX         *pixt1,
00285               PIX         *pixt2,
00286               PIX         *pixt3,
00287               PIX         *pixt4,
00288               PIX         *pixt5,
00289               PIX         *pixt6)
00290 {
00291 l_int32  same, fail;
00292 
00293     fail = FALSE;
00294     pixEqual(pixt1, pixt2, &same);
00295     if (!same) {
00296         fail = TRUE;
00297         fprintf(stderr, "%s (%d, 1) not same\n", type, size);
00298     }
00299     pixEqual(pixt3, pixt4, &same);
00300     if (!same) {
00301         fail = TRUE;
00302         fprintf(stderr, "%s (1, %d) not same\n", type, size);
00303     }
00304     pixEqual(pixt5, pixt6, &same);
00305     if (!same) {
00306         fail = TRUE;
00307         fprintf(stderr, "%s (%d, %d) not same\n", type, size, size);
00308     }
00309     return fail;
00310 }
00311 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines