Leptonica 1.68
C Image Processing Library

ptra2_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  * ptra2_reg.c
00018  *
00019  *    Testing:
00020  *       - basic ptra and ptraa operations
00021  *       - bin sort using ptra
00022  *       - boxaEqual() and pixaEqual()
00023  */
00024 
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include "allheaders.h"
00028 
00029 void BoxaSortTest(const char *fname, l_int32 index, const char *text);
00030 void PixaSortTest(const char *fname, l_int32 index, const char *text);
00031 
00032 main(int    argc,
00033      char **argv)
00034 {
00035 static char  mainName[] = "ptra2_reg";
00036 
00037     BoxaSortTest("feyn-fract.tif", 1, "Boxa sort test on small image");
00038     BoxaSortTest("feyn.tif", 2, "Boxa sort test on large image");
00039     PixaSortTest("feyn-fract.tif", 3, "Pixa sort test on small image");
00040     PixaSortTest("feyn.tif", 4, "Pixa sort test on large image");
00041 
00042     return 0;
00043 }
00044 
00045 
00046 void
00047 BoxaSortTest(const char  *fname,
00048              l_int32      index,
00049              const char  *text)
00050 {
00051 l_int32   i, n, m, imax, w, h, x, count, same;
00052 BOX      *box;
00053 BOXA     *boxa, *boxa1, *boxa2, *boxa3;
00054 NUMA     *na, *nad1, *nad2, *nad3, *naindex;
00055 PIX      *pixs;
00056 L_PTRA   *pa, *pad, *paindex;
00057 L_PTRAA  *paa;
00058 char     buf[256];
00059 
00060     fprintf(stderr, "\nTest %d: %s\n", index, text);
00061     pixs = pixRead(fname);
00062     boxa = pixConnComp(pixs, NULL, 8);
00063     
00064         /* Sort by x */
00065     boxa1 = boxaSort(boxa, L_SORT_BY_X, L_SORT_INCREASING, &nad1);
00066     sprintf(buf, "/tmp/junkboxa1.%d.ba", index);
00067     boxaWrite(buf, boxa1);
00068     sprintf(buf, "/tmp/junknad1.%d.na", index);
00069     numaWrite(buf, nad1);
00070 
00071     startTimer();
00072     boxa2 = boxaBinSort(boxa, L_SORT_BY_X, L_SORT_INCREASING, &nad2);
00073     sprintf(buf, "/tmp/junkboxa2.%d.ba", index);
00074     boxaWrite(buf, boxa2);
00075     sprintf(buf, "/tmp/junknad2.%d.na", index);
00076     numaWrite(buf, nad2);
00077 
00078     boxaEqual(boxa1, boxa2, 0, &naindex, &same);
00079     if (same)
00080         fprintf(stderr, "boxa1 and boxa2 are identical\n");
00081     else
00082         fprintf(stderr, "boxa1 and boxa2 are not identical\n");
00083     numaDestroy(&naindex);
00084     boxaEqual(boxa1, boxa2, 2, &naindex, &same);
00085     if (same)
00086         fprintf(stderr, "boxa1 and boxa2 are same at maxdiff = 2\n");
00087     else
00088         fprintf(stderr, "boxa1 and boxa2 differ at maxdiff = 2\n");
00089     sprintf(buf, "/tmp/junknaindex.%d.na", index);
00090     numaWrite(buf, naindex);
00091     numaDestroy(&naindex);
00092     boxaDestroy(&boxa1);
00093     numaDestroy(&nad1);
00094     numaDestroy(&nad2);
00095 
00096         /* Now do this stuff with ptra and ptraa */
00097         /* First, store the boxes in a ptraa, where each ptra contains
00098          * the boxes, and store the sort index in a ptra of numa */
00099     startTimer();
00100     pixGetDimensions(pixs, &w, &h, NULL);
00101     paa = ptraaCreate(w);
00102     paindex = ptraCreate(w);
00103     n = boxaGetCount(boxa);
00104     fprintf(stderr, "n = %d\n", n);
00105     for (i = 0; i < n; i++) {
00106         box = boxaGetBox(boxa, i, L_CLONE);
00107         boxGetGeometry(box, &x, NULL, NULL, NULL);
00108         pa = ptraaGetPtra(paa, x, L_HANDLE_ONLY);
00109         na = (NUMA *)ptraGetHandle(paindex, x);
00110         if (!pa) {  /* na also needs to be made */
00111             pa = ptraCreate(1);
00112             ptraaInsertPtra(paa, x, pa);
00113             na = numaCreate(1);
00114             ptraInsert(paindex, x, na, L_MIN_DOWNSHIFT);
00115         }
00116         ptraAdd(pa, box);
00117         numaAddNumber(na, i);
00118     }
00119 
00120     ptraGetActualCount(paindex, &count);
00121     fprintf(stderr, "count = %d\n", count);
00122     
00123         /* Flatten the ptraa to a ptra containing all the boxes
00124          * in sorted order, and put them in a boxa */
00125     pad = ptraaFlattenToPtra(paa);
00126     ptraaDestroy(&paa, FALSE, FALSE);
00127     ptraGetActualCount(pad, &m);
00128     if (m != n)
00129         fprintf(stderr, "n(orig) = %d, m(new) = %d\n", n, m);
00130     boxa3 = boxaCreate(m);
00131     for (i = 0; i < m; i++) {
00132         box = (BOX *)ptraRemove(pad, i, L_NO_COMPACTION);
00133         boxaAddBox(boxa3, box, L_INSERT);
00134     }
00135     ptraDestroy(&pad, FALSE, FALSE);
00136 
00137         /* Extract the data from the ptra of Numa, putting it into
00138          * a single Numa */
00139     ptraGetMaxIndex(paindex, &imax);
00140     nad3 = numaCreate(0);
00141     fprintf(stderr, "imax = %d\n", imax);
00142     for (i = 0; i <= imax; i++) {
00143         na = (NUMA *)ptraRemove(paindex, i, L_NO_COMPACTION);
00144         numaJoin(nad3, na, 0, 0);
00145         numaDestroy(&na);
00146     }
00147 
00148     fprintf(stderr, "Time for sort: %7.3f sec\n", stopTimer());
00149     sprintf(buf, "/tmp/junkboxa3.%d.ba", index);
00150     boxaWrite(buf, boxa3);
00151     sprintf(buf, "/tmp/junknad3.%d.na", index);
00152     numaWrite(buf, nad3);
00153 
00154     boxaDestroy(&boxa2);
00155     boxaDestroy(&boxa3);
00156     numaDestroy(&nad3);
00157     ptraDestroy(&paindex, FALSE, FALSE);
00158 
00159     pixDestroy(&pixs);
00160     boxaDestroy(&boxa);
00161     return;
00162 }
00163 
00164 
00165 void
00166 PixaSortTest(const char  *fname,
00167              l_int32      index,
00168              const char  *text)
00169 {
00170 l_int32  same;
00171 BOXA    *boxa, *boxa1, *boxa2;
00172 NUMA    *nap1, *nap2, *naindex;
00173 PIX     *pixs;
00174 PIXA    *pixa, *pixa1, *pixa2;
00175 char     buf[256];
00176 
00177     fprintf(stderr, "\nTest %d: %s\n", index, text);
00178     pixs = pixRead(fname);
00179     boxa = pixConnComp(pixs, &pixa, 8);
00180     
00181     startTimer();
00182     pixa1 = pixaSort(pixa, L_SORT_BY_X, L_SORT_INCREASING, &nap1, L_CLONE);
00183     fprintf(stderr, "Time for pixa sort: %7.3f sec\n", stopTimer());
00184     boxa1 = pixaGetBoxa(pixa1, L_CLONE);
00185     sprintf(buf, "/tmp/junkbap1.%d.ba", index);
00186     boxaWrite(buf, boxa1);
00187     sprintf(buf, "/tmp/junknap1.%d.na", index);
00188     numaWrite(buf, nap1);
00189     sprintf(buf, "/tmp/junkpixa1.%d.pa", index);
00190     pixaWrite(buf, pixa1);
00191 
00192     startTimer();
00193     pixa2 = pixaBinSort(pixa, L_SORT_BY_X, L_SORT_INCREASING, &nap2, L_CLONE);
00194     fprintf(stderr, "Time for pixa sort: %7.3f sec\n", stopTimer());
00195     boxa2 = pixaGetBoxa(pixa2, L_CLONE);
00196     sprintf(buf, "/tmp/junkbap2.%d.ba", index);
00197     boxaWrite(buf, boxa2);
00198     sprintf(buf, "/tmp/junknap2.%d.na", index);
00199     numaWrite(buf, nap2);
00200     sprintf(buf, "/tmp/junkpixa2.%d.pa", index);
00201     pixaWrite(buf, pixa2);
00202 
00203     startTimer();
00204     boxaEqual(boxa1, boxa2, 0, &naindex, &same);
00205     fprintf(stderr, "Time for boxaEqual: %7.3f sec\n", stopTimer());
00206     if (same)
00207         fprintf(stderr, "boxa1 and boxa2 are identical\n");
00208     else
00209         fprintf(stderr, "boxa1 and boxa2 are not identical\n");
00210     numaDestroy(&naindex);
00211     boxaEqual(boxa1, boxa2, 3, &naindex, &same);
00212     if (same)
00213         fprintf(stderr, "boxa1 and boxa2 are same at maxdiff = 3\n");
00214     else
00215         fprintf(stderr, "boxa1 and boxa2 differ at maxdiff = 3\n");
00216     numaDestroy(&naindex);
00217 
00218     startTimer();
00219     pixaEqual(pixa1, pixa2, 0, &naindex, &same);
00220     fprintf(stderr, "Time for pixaEqual: %7.3f sec\n", stopTimer());
00221     if (same)
00222         fprintf(stderr, "pixa1 and pixa2 are identical\n");
00223     else
00224         fprintf(stderr, "pixa1 and pixa2 are not identical\n");
00225     numaDestroy(&naindex);
00226     pixaEqual(pixa1, pixa2, 3, &naindex, &same);
00227     if (same)
00228         fprintf(stderr, "pixa1 and pixa2 are same at maxdiff = 3\n");
00229     else
00230         fprintf(stderr, "pixa1 and pixa2 differ at maxdiff = 3\n");
00231     numaDestroy(&naindex);
00232 
00233     boxaDestroy(&boxa);
00234     boxaDestroy(&boxa1);
00235     boxaDestroy(&boxa2);
00236     numaDestroy(&nap1);
00237     numaDestroy(&nap2);
00238     pixaDestroy(&pixa);
00239     pixaDestroy(&pixa1);
00240     pixaDestroy(&pixa2);
00241     pixDestroy(&pixs);
00242     return;
00243 }
00244 
00245 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines