Leptonica 1.68
C Image Processing Library

distance_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  * distance_reg.c
00018  *
00019  *   This tests pixDistanceFunction for a variety of usage
00020  *   with all 8 combinations of these parameters:
00021  *
00022  *     connectivity :   4 or 8
00023  *     dest depth :     8 or 16
00024  *     boundary cond :  L_BOUNDARY_BG or L_BOUNDARY_FG
00025  */
00026 
00027 #include "allheaders.h"
00028 
00029 static void TestDistance(PIXA *pixa, PIX *pixs, l_int32 conn,
00030                          l_int32 depth, l_int32 bc, L_REGPARAMS *rp);
00031 
00032 #define  DEBUG    0
00033 
00034 
00035 main(int    argc,
00036      char **argv)
00037 {
00038 char          buf[256];
00039 l_int32       i, j, k, index, conn, depth, bc;
00040 BOX          *box;
00041 PIX          *pix, *pixs, *pixd;
00042 PIXA         *pixa;
00043 L_REGPARAMS  *rp;
00044 
00045     if (regTestSetup(argc, argv, &rp))
00046         return 1;
00047 
00048     pix = pixRead("feyn.tif");
00049     box = boxCreate(383, 338, 1480, 1050);
00050     pixs = pixClipRectangle(pix, box, NULL);
00051     regTestWritePixAndCheck(rp, pixs, IFF_PNG);  /* 0 */
00052             
00053     for (i = 0; i < 2; i++) {
00054         conn = 4 + 4 * i;
00055         for (j = 0; j < 2; j++) {
00056             depth = 8 + 8 * j;
00057             for (k = 0; k < 2; k++) {
00058                 bc = k + 1;
00059                 index = 4 * i + 2 * j + k;
00060                 fprintf(stderr, "Set %d\n", index);
00061                 if (DEBUG) {
00062                     fprintf(stderr, "%d: conn = %d, depth = %d, bc = %d\n",
00063                             rp->index + 1, conn, depth, bc);
00064                 }
00065                 pixa = pixaCreate(0);
00066                 pixSaveTiled(pixs, pixa, 1, 1, 20, 8);
00067                 TestDistance(pixa, pixs, conn, depth, bc, rp);
00068                 pixd = pixaDisplay(pixa, 0, 0);
00069                 pixDisplayWithTitle(pixd, 0, 0, NULL, rp->display);
00070                 pixaDestroy(&pixa);
00071                 pixDestroy(&pixd);
00072             }
00073         }
00074     }
00075 
00076     boxDestroy(&box);
00077     pixDestroy(&pix);
00078     pixDestroy(&pixs);
00079     regTestCleanup(rp);
00080     return 0;
00081 }
00082 
00083 
00084 static void
00085 TestDistance(PIXA         *pixa,
00086              PIX          *pixs,
00087              l_int32       conn,
00088              l_int32       depth,
00089              l_int32       bc,
00090              L_REGPARAMS  *rp)
00091 {
00092 PIX  *pixt1, *pixt2, *pixt3, *pixt4, *pixt5;
00093 
00094         /* Test the distance function and display */
00095     pixInvert(pixs, pixs);
00096     pixt1 = pixDistanceFunction(pixs, conn, depth, bc);
00097     regTestWritePixAndCheck(rp, pixt1, IFF_PNG);  /* a + 1 */
00098     pixSaveTiled(pixt1, pixa, 1, 1, 20, 0);
00099     pixInvert(pixs, pixs);
00100     pixt2 = pixMaxDynamicRange(pixt1, L_LOG_SCALE);
00101     regTestWritePixAndCheck(rp, pixt2, IFF_JFIF_JPEG);  /* a + 2 */
00102     pixSaveTiled(pixt2, pixa, 1, 0, 20, 0);
00103     pixDestroy(&pixt1);
00104     pixDestroy(&pixt2);
00105 
00106         /* Test the distance function and display with contour rendering */
00107     pixInvert(pixs, pixs);
00108     pixt1 = pixDistanceFunction(pixs, conn, depth, bc);
00109     regTestWritePixAndCheck(rp, pixt1, IFF_PNG);  /* a + 3 */
00110     pixSaveTiled(pixt1, pixa, 1, 1, 20, 0);
00111     pixInvert(pixs, pixs);
00112     pixt2 = pixRenderContours(pixt1, 2, 4, 1);  /* binary output */
00113     regTestWritePixAndCheck(rp, pixt2, IFF_PNG);  /* a + 4 */
00114     pixSaveTiled(pixt2, pixa, 1, 0, 20, 0);
00115     pixt3 = pixRenderContours(pixt1, 2, 4, depth);
00116     pixt4 = pixMaxDynamicRange(pixt3, L_LINEAR_SCALE);
00117     regTestWritePixAndCheck(rp, pixt4, IFF_JFIF_JPEG);  /* a + 5 */
00118     pixSaveTiled(pixt4, pixa, 1, 0, 20, 0);
00119     pixt5 = pixMaxDynamicRange(pixt3, L_LOG_SCALE);
00120     regTestWritePixAndCheck(rp, pixt5, IFF_JFIF_JPEG);  /* a + 6 */
00121     pixSaveTiled(pixt5, pixa, 1, 0, 20, 0);
00122     pixDestroy(&pixt1);
00123     pixDestroy(&pixt2);
00124     pixDestroy(&pixt3);
00125     pixDestroy(&pixt4);
00126     pixDestroy(&pixt5);
00127 
00128         /* Label all pixels in each c.c. with a color equal to the
00129          * max distance of any pixel within that c.c. from the bg.
00130          * Note that we've normalized so the dynamic range extends
00131          * to 255.  For the image here, each unit of distance is
00132          * represented by about 21 grayscale units.  The largest
00133          * distance is 12.  */
00134     if (depth == 8) {
00135         pixt1 = pixDistanceFunction(pixs, conn, depth, bc);
00136         pixt4 = pixMaxDynamicRange(pixt1, L_LOG_SCALE);
00137         regTestWritePixAndCheck(rp, pixt4, IFF_JFIF_JPEG);  /* b + 1 */
00138         pixSaveTiled(pixt4, pixa, 1, 1, 20, 0);
00139         pixt2 = pixCreateTemplate(pixt1);
00140         pixSetMasked(pixt2, pixs, 255);
00141         regTestWritePixAndCheck(rp, pixt2, IFF_JFIF_JPEG);  /* b + 2 */
00142         pixSaveTiled(pixt2, pixa, 1, 0, 20, 0);
00143         pixSeedfillGray(pixt1, pixt2, 4);
00144         pixt3 = pixMaxDynamicRange(pixt1, L_LINEAR_SCALE);
00145         regTestWritePixAndCheck(rp, pixt3, IFF_JFIF_JPEG);  /* b + 3 */
00146         pixSaveTiled(pixt3, pixa, 1, 0, 20, 0);
00147         pixDestroy(&pixt1);
00148         pixDestroy(&pixt2);
00149         pixDestroy(&pixt3);
00150         pixDestroy(&pixt4);
00151     }
00152 
00153     return;
00154 }
00155 
00156 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines