Leptonica 1.68
C Image Processing Library

watershedtest.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  * watershedtest.c
00018  *
00019  */
00020 
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <math.h>
00024 #include "allheaders.h"
00025 
00026 
00027 main(int    argc,
00028      char **argv)
00029 {
00030 l_int32      i, j, w, h, empty;
00031 l_uint32     redval, greenval;
00032 l_float32    f;
00033 L_WSHED     *wshed;
00034 PIX         *pixs, *pixc, *pixd;
00035 PIX         *pixt0, *pixt1, *pixt2, *pixt3, *pixt4, *pixt5;
00036 PIX         *pixt6, *pixt7, *pixt8;
00037 PIXA        *pixac;
00038 PTA         *pta;
00039 static char  mainName[] = "watershedtest";
00040 
00041     if (argc != 1)
00042         exit(ERROR_INT(" Syntax:  watershedtest", mainName, 1));
00043 
00044     pixac = pixaCreate(0);
00045 
00046     pixs = pixCreate(500, 500, 8);
00047     pixGetDimensions(pixs, &w, &h, NULL);
00048     for (i = 0; i < 500; i++) {
00049         for (j = 0; j < 500; j++) {
00050 #if 1
00051             f = 128.0 + 26.3 * sin(0.0438 * (l_float32)i);
00052             f += 33.4 * cos(0.0712 * (l_float32)i);
00053             f += 18.6 * sin(0.0561 * (l_float32)j);
00054             f += 23.6 * cos(0.0327 * (l_float32)j);
00055 #else
00056             f = 128.0 + 26.3 * sin(0.0238 * (l_float32)i);
00057             f += 33.4 * cos(0.0312 * (l_float32)i);
00058             f += 18.6 * sin(0.0261 * (l_float32)j);
00059             f += 23.6 * cos(0.0207 * (l_float32)j);
00060 #endif
00061             pixSetPixel(pixs, j, i, (l_int32)f);
00062         }
00063     }
00064     pixSaveTiled(pixs, pixac, 1, 1, 10, 32);
00065     pixWrite("/tmp/junkpattern.png", pixs, IFF_PNG);
00066     startTimer();
00067     pixLocalExtrema(pixs, 0, 0, &pixt1, &pixt2);
00068     fprintf(stderr, "Time for extrema: %7.3f\n", stopTimer());
00069     pixSetOrClearBorder(pixt1, 2, 2, 2, 2, PIX_CLR);
00070     composeRGBPixel(255, 0, 0, &redval);
00071     composeRGBPixel(0, 255, 0, &greenval);
00072     pixc = pixConvertTo32(pixs);
00073     pixPaintThroughMask(pixc, pixt2, 0, 0, greenval);
00074     pixPaintThroughMask(pixc, pixt1, 0, 0, redval);
00075     pixSaveTiled(pixc, pixac, 1, 0, 10, 32);
00076     pixWrite("/tmp/junkpixc.png", pixc, IFF_PNG);
00077     pixSaveTiled(pixt1, pixac, 1, 0, 10, 32);
00078     pta = pixSelectMinInConnComp(pixs, pixt1, NULL);
00079 /*    ptaWriteStream(stderr, pta, 1); */
00080     pixt3 = pixGenerateFromPta(pta, w, h);
00081     pixSaveTiled(pixt3, pixac, 1, 1, 10, 32);
00082 
00083     pixt4 = pixConvertTo32(pixs);
00084     pixPaintThroughMask(pixt4, pixt3, 0, 0, greenval);
00085     pixSaveTiled(pixt4, pixac, 1, 0, 10, 32);
00086     pixt5 = pixRemoveSeededComponents(NULL, pixt3, pixt1, 8, 2);
00087     pixSaveTiled(pixt5, pixac, 1, 0, 10, 32);
00088     pixZero(pixt5, &empty);
00089     fprintf(stderr, "Is empty?  %d\n", empty);
00090     pixDestroy(&pixt4);
00091     pixDestroy(&pixt5);
00092 
00093     wshed = wshedCreate(pixs, pixt3, 10, 0);
00094     startTimer();
00095     wshedApply(wshed);
00096     fprintf(stderr, "Time for wshed: %7.3f\n", stopTimer());
00097     pixt6 = pixaDisplayRandomCmap(wshed->pixad, w, h);
00098     pixSaveTiled(pixt6, pixac, 1, 1, 10, 32);
00099     numaWriteStream(stderr, wshed->nalevels);
00100     pixt7 = wshedRenderFill(wshed);
00101     pixSaveTiled(pixt7, pixac, 1, 0, 10, 32);
00102     pixt8 = wshedRenderColors(wshed);
00103     pixSaveTiled(pixt8, pixac, 1, 0, 10, 32);
00104     wshedDestroy(&wshed);
00105 
00106     pixd = pixaDisplay(pixac, 0, 0);
00107     pixDisplay(pixd, 100, 100);
00108     pixWrite("/tmp/junkwshed.png", pixd, IFF_PNG);
00109     pixDestroy(&pixd);
00110     pixaDestroy(&pixac);
00111 
00112     pixDestroy(&pixt1);
00113     pixDestroy(&pixt2);
00114     pixDestroy(&pixt3);
00115     pixDestroy(&pixt6);
00116     pixDestroy(&pixt7);
00117     pixDestroy(&pixt8);
00118     pixDestroy(&pixs);
00119     pixDestroy(&pixc);
00120     ptaDestroy(&pta);
00121     return 0;
00122 }
00123 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines