Leptonica 1.68
C Image Processing Library

ccbordtest.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  * ccbordtest.c
00018  *
00019  *      Comprehensive test for border-following representations
00020  *      of binary images.
00021  */
00022 
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include "allheaders.h"
00026 
00027 
00028 main(int    argc,
00029      char **argv)
00030 {
00031 char        *filein;
00032 l_int32      count;
00033 CCBORDA     *ccba, *ccba2;
00034 PIX         *pixs, *pixd, *pixd2, *pixd3;
00035 PIX         *pixt, *pixc, *pixc2;
00036 static char  mainName[] = "ccbordtest";
00037 
00038     if (argc != 2)
00039         exit(ERROR_INT(" Syntax:  ccbordtest filein", mainName, 1));
00040 
00041     filein = argv[1];
00042 
00043     if ((pixs = pixRead(filein)) == NULL)
00044         exit(ERROR_INT("pixs not made", mainName, 1));
00045             
00046     fprintf(stderr, "Get border representation...");
00047     startTimer();
00048     ccba = pixGetAllCCBorders(pixs);
00049     fprintf(stderr, "%6.3f sec\n", stopTimer());
00050 
00051 #if 0
00052         /* get global locs directly and display borders */
00053     fprintf(stderr, "Convert from local to global locs...");
00054     startTimer();
00055     ccbaGenerateGlobalLocs(ccba);
00056     fprintf(stderr, "%6.3f sec\n", stopTimer());
00057     fprintf(stderr, "Display border representation...");
00058     startTimer();
00059     pixd = ccbaDisplayBorder(ccba);
00060     fprintf(stderr, "%6.3f sec\n", stopTimer());
00061     pixWrite("/tmp/junkborder1.png", pixd, IFF_PNG);
00062 
00063 #else
00064         /* get step chain code, then global coords, and display borders */
00065     fprintf(stderr, "Get step chain code...");
00066     startTimer();
00067     ccbaGenerateStepChains(ccba);
00068     fprintf(stderr, "%6.3f sec\n", stopTimer());
00069     fprintf(stderr, "Convert from step chain to global locs...");
00070     startTimer();
00071     ccbaStepChainsToPixCoords(ccba, CCB_GLOBAL_COORDS);
00072     fprintf(stderr, "%6.3f sec\n", stopTimer());
00073     fprintf(stderr, "Display border representation...");
00074     startTimer();
00075     pixd = ccbaDisplayBorder(ccba);
00076     fprintf(stderr, "%6.3f sec\n", stopTimer());
00077     pixWrite("/tmp/junkborder1.png", pixd, IFF_PNG);
00078 #endif
00079 
00080         /* check if border pixels are in original set */
00081     fprintf(stderr, "Check if border pixels are in original set ...\n");
00082     pixt = pixSubtract(NULL, pixd, pixs);
00083     pixCountPixels(pixt, &count, NULL);
00084     if (count == 0)
00085         fprintf(stderr, "   all border pixels are in original set\n");
00086     else
00087         fprintf(stderr, "   %d border pixels are not in original set\n", count);
00088     pixDestroy(&pixt);
00089 
00090         /* display image */
00091     fprintf(stderr, "Reconstruct image ...");
00092     startTimer();
00093 /*    pixc = ccbaDisplayImage1(ccba); */
00094     pixc = ccbaDisplayImage2(ccba);
00095     fprintf(stderr, "%6.3f sec\n", stopTimer());
00096     pixWrite("/tmp/junkrecon1.png", pixc, IFF_PNG);
00097 
00098         /* check with original to see if correct */
00099     fprintf(stderr, "Check with original to see if correct ...\n");
00100     pixXor(pixc, pixc, pixs);
00101     pixCountPixels(pixc, &count, NULL);
00102     if (count == 0)
00103         fprintf(stderr, "   perfect direct recon\n");
00104     else {
00105         l_int32  w, h, i, j;
00106         l_uint32 val;
00107         fprintf(stderr, "   %d pixels in error in recon\n", count);
00108 #if 1
00109         w = pixGetWidth(pixc);
00110         h = pixGetHeight(pixc);
00111         for (i = 0; i < h; i++) {
00112             for (j = 0; j < w; j++) {
00113                 pixGetPixel(pixc, j, i, &val);
00114                 if (val == 1)
00115                     fprintf(stderr, "bad pixel at (%d, %d)\n", j, i);
00116             }
00117         }
00118         pixWrite("/tmp/junkbadpixels.png", pixc, IFF_PNG);
00119 #endif
00120     }
00121 
00122 
00123     /*----------------------------------------------------------*
00124      *        write to file (compressed) and read back          *
00125      *----------------------------------------------------------*/
00126     fprintf(stderr, "Write serialized step data...");
00127     startTimer();
00128     ccbaWrite("/tmp/junkstepout", ccba);
00129     fprintf(stderr, "%6.3f sec\n", stopTimer());
00130     fprintf(stderr, "Read serialized step data...");
00131     startTimer();
00132     ccba2 = ccbaRead("/tmp/junkstepout");
00133     fprintf(stderr, "%6.3f sec\n", stopTimer());
00134 
00135         /* display the border pixels again */
00136     fprintf(stderr, "Convert from step chain to global locs...");
00137     startTimer();
00138     ccbaStepChainsToPixCoords(ccba2, CCB_GLOBAL_COORDS);
00139     fprintf(stderr, "%6.3f sec\n", stopTimer());
00140     fprintf(stderr, "Display border representation...");
00141     startTimer();
00142     pixd2 = ccbaDisplayBorder(ccba2);
00143     fprintf(stderr, "%6.3f sec\n", stopTimer());
00144     pixWrite("/tmp/junkborder2.png", pixd2, IFF_PNG);
00145 
00146         /* check if border pixels are same as first time */
00147     pixXor(pixd2, pixd2, pixd);
00148     pixCountPixels(pixd2, &count, NULL);
00149     if (count == 0)
00150         fprintf(stderr, "   perfect w/r border recon\n");
00151     else {
00152         l_int32  w, h, i, j, val;
00153         fprintf(stderr, "   %d pixels in error in w/r recon\n", count);
00154     }
00155     pixDestroy(&pixd2);
00156 
00157         /* display image again */
00158     fprintf(stderr, "Convert from step chain to local coords...");
00159     startTimer();
00160     ccbaStepChainsToPixCoords(ccba2, CCB_LOCAL_COORDS);
00161     fprintf(stderr, "%6.3f sec\n", stopTimer());
00162     fprintf(stderr, "Reconstruct image from file ...");
00163     startTimer();
00164 /*    pixc2 = ccbaDisplayImage1(ccba2); */
00165     pixc2 = ccbaDisplayImage2(ccba2);
00166     fprintf(stderr, "%6.3f sec\n", stopTimer());
00167     pixWrite("/tmp/junkrecon2.png", pixc2, IFF_PNG);
00168 
00169         /* check with original to see if correct */
00170     fprintf(stderr, "Check with original to see if correct ...\n");
00171     pixXor(pixc2, pixc2, pixs);
00172     pixCountPixels(pixc2, &count, NULL);
00173     if (count == 0)
00174         fprintf(stderr, "   perfect image recon\n");
00175     else {
00176         l_int32  w, h, i, j;
00177         l_uint32 val;
00178         fprintf(stderr, "   %d pixels in error in image recon\n", count);
00179 #if 1
00180         w = pixGetWidth(pixc2);
00181         h = pixGetHeight(pixc2);
00182         for (i = 0; i < h; i++) {
00183             for (j = 0; j < w; j++) {
00184                 pixGetPixel(pixc2, j, i, &val);
00185                 if (val == 1)
00186                     fprintf(stderr, "bad pixel at (%d, %d)\n", j, i);
00187             }
00188         }
00189         pixWrite("/tmp/junkbadpixels2.png", pixc2, IFF_PNG);
00190 #endif
00191     }
00192 
00193     /*----------------------------------------------------------*
00194      *     make, display and check single path border for svg   *
00195      *----------------------------------------------------------*/
00196         /* make local single path border for svg */
00197     fprintf(stderr, "Make local single path borders for svg ...");
00198     startTimer();
00199     ccbaGenerateSinglePath(ccba);
00200     fprintf(stderr, "%6.3f sec\n", stopTimer());
00201         /* generate global single path border */
00202     fprintf(stderr, "Generate global single path borders ...");
00203     startTimer();
00204     ccbaGenerateSPGlobalLocs(ccba, CCB_SAVE_TURNING_PTS);
00205     fprintf(stderr, "%6.3f sec\n", stopTimer());
00206         /* display border pixels from single path */
00207     fprintf(stderr, "Display border from single path...");
00208     startTimer();
00209     pixd3 = ccbaDisplaySPBorder(ccba);
00210     fprintf(stderr, "%6.3f sec\n", stopTimer());
00211     pixWrite("/tmp/junkborder3.png", pixd3, IFF_PNG);
00212         /* check if border pixels are in original set */
00213     fprintf(stderr, "Check if border pixels are in original set ...\n");
00214     pixt = pixSubtract(NULL, pixd3, pixs);
00215     pixCountPixels(pixt, &count, NULL);
00216     if (count == 0)
00217         fprintf(stderr, "   all border pixels are in original set\n");
00218     else
00219         fprintf(stderr, "   %d border pixels are not in original set\n", count);
00220     pixDestroy(&pixt);
00221     pixDestroy(&pixd3);
00222 
00223         /*  output in svg file format */
00224     fprintf(stderr, "Write output in svg file format ...\n");
00225     startTimer();
00226     ccbaWriteSVG("/tmp/junksvg", ccba);
00227     fprintf(stderr, "%6.3f sec\n", stopTimer());
00228 
00229     ccbaDestroy(&ccba2);
00230     ccbaDestroy(&ccba);
00231     pixDestroy(&pixs);
00232     pixDestroy(&pixd);
00233     pixDestroy(&pixc);
00234     pixDestroy(&pixc2);
00235     return 0;
00236 }
00237 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines