Leptonica 1.68
C Image Processing Library

grayfill_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  * grayfill_reg.c
00018  *
00019  */
00020 
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include "allheaders.h"
00024 
00025 void PixTestEqual(PIX *pixs1, PIX *pixs2, PIX *pixm, l_int32 set,
00026                   l_int32 connectivity);
00027 
00028 
00029 main(int    argc,
00030      char **argv)
00031 {
00032 l_int32      i, j, same;
00033 PIX         *pixm, *pixmi, *pixs1, *pixs1_8;
00034 PIX         *pixs2, *pixs2_8, *pixs3, *pixs3_8;
00035 PIX         *pixb1, *pixb2, *pixb3, *pixmin, *pixd;
00036 PIXA        *pixac;
00037 static char  mainName[] = "grayfill_reg";
00038 
00039 
00040     pixDisplayWrite(NULL, -1);
00041     pixac = pixaCreate(0);
00042 
00043         /* Mask */
00044     pixm = pixCreate(200, 200, 8);
00045     for (i = 0; i < 200; i++)
00046         for (j = 0; j < 200; j++)
00047             pixSetPixel(pixm, j, i, 20 + L_ABS((100 - i) * (100 - j)) / 50);
00048     pixmi = pixInvert(NULL, pixm);
00049 
00050         /* Seed1 */
00051     pixs1 = pixCreate(200, 200, 8);
00052     for (i = 99; i <= 101; i++)
00053         for (j = 99; j <= 101; j++)
00054             pixSetPixel(pixs1, j, i, 50 - i/100 - j/100);
00055     pixs1_8 = pixCopy(NULL, pixs1);
00056 
00057         /* Seed2 */
00058     pixs2 = pixCreate(200, 200, 8);
00059     for (i = 99; i <= 101; i++)
00060         for (j = 99; j <= 101; j++)
00061             pixSetPixel(pixs2, j, i, 205 - i/100 - j/100);
00062     pixs2_8 = pixCopy(NULL, pixs2);
00063 
00064         /* Inverse grayscale fill */
00065     pixSaveTiled(pixm, pixac, 1, 1, 10, 8);
00066     pixSaveTiled(pixs1, pixac, 1, 0, 10, 0);
00067     pixSeedfillGrayInv(pixs1, pixm, 4);
00068     pixSeedfillGrayInv(pixs1_8, pixm, 8);
00069     pixSaveTiled(pixs1, pixac, 1, 0, 10, 0);
00070     pixSaveTiled(pixs1_8, pixac, 1, 0, 10, 0);
00071     pixb1 = pixThresholdToBinary(pixs1, 20);
00072     pixSaveTiled(pixb1, pixac, 1, 0, 10, 0);
00073     pixCombineMasked(pixs1, pixm, pixb1);
00074     pixSaveTiled(pixs1, pixac, 1, 0, 10, 0);
00075     pixDestroy(&pixs1);
00076     pixDestroy(&pixs1_8);
00077     pixDestroy(&pixb1);
00078 
00079         /* Standard grayscale fill */
00080     pixSaveTiled(pixmi, pixac, 1, 1, 10, 0);
00081     pixSaveTiled(pixs2, pixac, 1, 0, 10, 0);
00082     pixSeedfillGray(pixs2, pixmi, 4);
00083     pixSeedfillGray(pixs2_8, pixmi, 8);
00084     pixSaveTiled(pixs2, pixac, 1, 0, 10, 0);
00085     pixSaveTiled(pixs2_8, pixac, 1, 0, 10, 0);
00086     pixb2 = pixThresholdToBinary(pixs2, 205);
00087     pixSaveTiled(pixb2, pixac, 1, 0, 10, 0);
00088     pixDestroy(&pixs2);
00089     pixDestroy(&pixs2_8);
00090     pixDestroy(&pixb2);
00091 
00092         /* Basin fill from minima as seed */
00093     pixSaveTiled(pixm, pixac, 1, 1, 10, 8);
00094     pixLocalExtrema(pixm, 0, 0, &pixmin, NULL);
00095     pixSaveTiled(pixmin, pixac, 1, 0, 10, 0);
00096     pixs3 = pixSeedfillGrayBasin(pixmin, pixm, 30, 4);
00097     pixs3_8 = pixSeedfillGrayBasin(pixmin, pixm, 30, 8);
00098     pixSaveTiled(pixs3, pixac, 1, 0, 10, 0);
00099     pixSaveTiled(pixs3_8, pixac, 1, 0, 10, 0);
00100     pixb3 = pixThresholdToBinary(pixs3, 60);
00101     pixSaveTiled(pixb3, pixac, 1, 0, 10, 0);
00102     pixDestroy(&pixs3);
00103     pixDestroy(&pixs3_8);
00104     pixDestroy(&pixb3);
00105 
00106     pixd = pixaDisplay(pixac, 0, 0);
00107     pixDisplay(pixd, 100, 100);
00108     pixWrite("/tmp/junkfill.png", pixd, IFF_PNG);
00109     pixDestroy(&pixd);
00110     pixaDestroy(&pixac);
00111 
00112         /* Compare hybrid and iterative gray seedfills */
00113     pixs1 = pixCopy(NULL, pixm);
00114     pixs2 = pixCopy(NULL, pixm);
00115     pixAddConstantGray(pixs1, -30);
00116     pixAddConstantGray(pixs2, 60);
00117 
00118     PixTestEqual(pixs1, pixs2, pixm, 1, 4);
00119     PixTestEqual(pixs1, pixs2, pixm, 2, 8);
00120     PixTestEqual(pixs2, pixs1, pixm, 3, 4);
00121     PixTestEqual(pixs2, pixs1, pixm, 4, 8);
00122     pixDestroy(&pixs1);
00123     pixDestroy(&pixs2);
00124 
00125     pixDestroy(&pixm);
00126     pixDestroy(&pixmi);
00127     pixDestroy(&pixmin);
00128     return 0;
00129 }
00130 
00131 
00132 void
00133 PixTestEqual(PIX     *pixs1,
00134              PIX     *pixs2,
00135              PIX     *pixm,
00136              l_int32  set,
00137              l_int32  connectivity)
00138 {
00139 l_int32  same;
00140 PIX     *pixc11, *pixc12, *pixc21, *pixc22, *pixmi;
00141 
00142     pixmi = pixInvert(NULL, pixm);
00143     pixc11 = pixCopy(NULL, pixs1);
00144     pixc12 = pixCopy(NULL, pixs1);
00145     pixc21 = pixCopy(NULL, pixs2);
00146     pixc22 = pixCopy(NULL, pixs2);
00147 
00148         /* Test inverse seed filling */
00149     pixSeedfillGrayInv(pixc11, pixm, connectivity);
00150     pixSeedfillGrayInvSimple(pixc12, pixm, connectivity);
00151     pixEqual(pixc11, pixc12, &same);
00152     if (same)
00153         fprintf(stderr, "\nSuccess for inv set %d\n", set);
00154     else
00155         fprintf(stderr, "\nFailure for inv set %d\n", set);
00156 
00157         /* Test seed filling */
00158     pixSeedfillGray(pixc21, pixm, connectivity);
00159     pixSeedfillGraySimple(pixc22, pixm, connectivity);
00160     pixEqual(pixc21, pixc22, &same);
00161     if (same)
00162         fprintf(stderr, "Success for set %d\n", set);
00163     else
00164         fprintf(stderr, "Failure for set %d\n", set);
00165 
00166        /* Display the filling results */
00167 /*    pixDisplay(pixc11, 220 * (set - 1), 100);
00168     pixDisplay(pixc21, 220 * (set - 1), 320); */
00169 
00170     pixDestroy(&pixmi);
00171     pixDestroy(&pixc11);
00172     pixDestroy(&pixc12);
00173     pixDestroy(&pixc21);
00174     pixDestroy(&pixc22);
00175     return;
00176 }
00177 
00178 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines