Leptonica 1.68
C Image Processing Library

cctest1.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  * cctest1.c
00018  *
00019  *    This is a test of the following function:
00020  *
00021  *        BOXA   *pixConnComp(PIX *pixs, PIXA  **ppixa, l_int32 connectivity)
00022  *
00023  *                    pixs:  input pix
00024  *                    ppixa: &pixa (<optional> pixa of each c.c.)
00025  *                    connectivity (4 or 8)
00026  *                    boxa: returned array of boxes of c.c.
00027  *
00028  *        Use NULL for &pixa if you don't want the pixa array.
00029  *
00030  *    It also demonstrates a few display modes.
00031  */
00032 
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035 #include "allheaders.h"
00036 
00037 #define  NTIMES             2
00038 
00039 
00040 main(int    argc,
00041      char **argv)
00042 {
00043 char        *filein;
00044 l_int32      i, n, np, same, count;
00045 FILE        *fp;
00046 BOX         *box;
00047 BOXA        *boxa, *boxa2;
00048 PIX         *pixs, *pixd;
00049 PIXA        *pixa;
00050 PIXCMAP     *cmap;
00051 static char  mainName[] = "cctest1";
00052 
00053     if (argc != 2)
00054         exit(ERROR_INT(" Syntax:  cctest1 filein", mainName, 1));
00055 
00056     filein = argv[1];
00057 
00058     if ((pixs = pixRead(filein)) == NULL)
00059         exit(ERROR_INT("pixs not made", mainName, 1));
00060     if (pixGetDepth(pixs) != 1)
00061         exit(ERROR_INT("pixs not 1 bpp", mainName, 1));
00062             
00063         /* Test speed of pixCountConnComp() */
00064     startTimer();
00065     for (i = 0; i < NTIMES; i++)
00066         pixCountConnComp(pixs, 4, &count);
00067     fprintf(stderr, "Time to compute 4-cc: %6.3f sec\n", stopTimer()/NTIMES);
00068     fprintf(stderr, "Number of 4-cc: %d\n", count);
00069     startTimer();
00070     for (i = 0; i < NTIMES; i++)
00071         pixCountConnComp(pixs, 8, &count);
00072     fprintf(stderr, "Time to compute 8-cc: %6.3f sec\n", stopTimer()/NTIMES);
00073     fprintf(stderr, "Number of 8-cc: %d\n", count);
00074 
00075         /* Test speed of pixConnComp(), with only boxa output  */
00076     startTimer();
00077     for (i = 0; i < NTIMES; i++) {
00078         boxa = pixConnComp(pixs, NULL, 4);
00079         boxaDestroy(&boxa);
00080     }
00081     fprintf(stderr, "Time to compute 4-cc: %6.3f sec\n", stopTimer()/NTIMES);
00082     startTimer();
00083     for (i = 0; i < NTIMES; i++) {
00084         boxa = pixConnComp(pixs, NULL, 8);
00085         boxaDestroy(&boxa);
00086     }
00087     fprintf(stderr, "Time to compute 8-cc: %6.3f sec\n", stopTimer()/NTIMES);
00088 
00089         /* Draw outline of each c.c. box */
00090     boxa = pixConnComp(pixs, NULL, 4);
00091     n = boxaGetCount(boxa);
00092     fprintf(stderr, "Num 4-cc boxes: %d\n", n);
00093     for (i = 0; i < n; i++) {
00094         box = boxaGetBox(boxa, i, L_CLONE);
00095         pixRenderBox(pixs, box, 3, L_FLIP_PIXELS);
00096         boxDestroy(&box);   /* remember, clones need to be destroyed */
00097     }
00098     pixDisplayWrite(pixs, 1);
00099     boxaDestroy(&boxa);
00100 
00101         /* Display each component as a random color in cmapped 8 bpp.
00102          * Background is color 0; it is set to white. */
00103     boxa = pixConnComp(pixs, &pixa, 4);
00104     pixd = pixaDisplayRandomCmap(pixa, pixGetWidth(pixs), pixGetHeight(pixs));
00105     cmap = pixGetColormap(pixd);
00106     pixcmapResetColor(cmap, 0, 255, 255, 255);  /* reset background to white */
00107     pixDisplay(pixd, 100, 100);
00108     pixDisplayWrite(pixd, 1);
00109     boxaDestroy(&boxa);
00110     pixDestroy(&pixd);
00111     pixaDestroy(&pixa);
00112 
00113     pixDestroy(&pixs);
00114     return 0;
00115 }
00116 
00117 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines