Leptonica 1.68
C Image Processing Library
|
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 * pta_reg.c 00018 * 00019 * This tests several ptaa functions, including: 00020 * - ptaaGetBoundaryPixels() 00021 * - pixRenderRandomCmapPtaa() 00022 */ 00023 00024 #include <stdio.h> 00025 #include <stdlib.h> 00026 #include "allheaders.h" 00027 00028 00029 main(int argc, 00030 char **argv) 00031 { 00032 l_int32 i, n, nbox, npta, fgcount, bgcount, count, same, ok; 00033 BOXA *boxa; 00034 PIX *pixs, *pixfg, *pixbg, *pixc, *pixb, *pixd; 00035 PIXA *pixa; 00036 PTA *pta; 00037 PTAA *ptaafg, *ptaabg; 00038 static char mainName[] = "pta_reg.c"; 00039 00040 ok = TRUE; 00041 pixs = pixRead("feyn-fract.tif"); 00042 boxa = pixConnComp(pixs, NULL, 8); 00043 nbox = boxaGetCount(boxa); 00044 if (nbox != 464) ok = FALSE; 00045 fprintf(stderr, "Num boxes = %d\n", nbox); 00046 00047 /* Get fg and bg boundary pixels */ 00048 pixfg = pixMorphSequence(pixs, "e3.3", 0); 00049 pixXor(pixfg, pixfg, pixs); 00050 pixCountPixels(pixfg, &fgcount, NULL); 00051 if (fgcount == 58764) 00052 fprintf(stderr, "num fg pixels = %d\n", fgcount); 00053 else { 00054 ok = FALSE; 00055 fprintf(stderr, "Error: num fg pixels = %d\n", fgcount); 00056 } 00057 00058 pixbg = pixMorphSequence(pixs, "d3.3", 0); 00059 pixXor(pixbg, pixbg, pixs); 00060 pixCountPixels(pixbg, &bgcount, NULL); 00061 if (bgcount == 60335) 00062 fprintf(stderr, "num bg pixels = %d\n", bgcount); 00063 else { 00064 ok = FALSE; 00065 fprintf(stderr, "Error: num bg pixels = %d\n", bgcount); 00066 } 00067 00068 /* Get ptaa of fg pixels */ 00069 ptaafg = ptaaGetBoundaryPixels(pixs, L_BOUNDARY_FG, 8, NULL, NULL); 00070 ptaaWrite("/tmp/junkfg.ptaa", ptaafg, 1); 00071 npta = ptaaGetCount(ptaafg); 00072 if (npta != nbox) { 00073 ok = FALSE; 00074 fprintf(stderr, "Error: ptaa count = %d, boxa count = %d\n", 00075 npta, nbox); 00076 } 00077 count = 0; 00078 for (i = 0; i < npta; i++) { 00079 pta = ptaaGetPta(ptaafg, i, L_CLONE); 00080 count += ptaGetCount(pta); 00081 ptaDestroy(&pta); 00082 } 00083 fprintf(stderr, "num fg pts = %d\n", count); 00084 if (fgcount != count) { 00085 ok = FALSE; 00086 fprintf(stderr, "Error: npix = %d, num fg pts = %d\n", fgcount, count); 00087 } 00088 00089 /* Get ptaa of bg pixels. Note that the number of bg pts 00090 * is, in general, larger than the number of bg boundary pixels, 00091 * because bg boundary pixels are shared by two c.c. that 00092 * are 1 pixel apart. */ 00093 ptaabg = ptaaGetBoundaryPixels(pixs, L_BOUNDARY_BG, 8, NULL, NULL); 00094 ptaaWrite("/tmp/junkbg.ptaa", ptaabg, 1); 00095 npta = ptaaGetCount(ptaabg); 00096 if (npta != nbox) { 00097 ok = FALSE; 00098 fprintf(stderr, "Error: ptaa count = %d, boxa count = %d\n", 00099 npta, nbox); 00100 } 00101 count = 0; 00102 for (i = 0; i < npta; i++) { 00103 pta = ptaaGetPta(ptaabg, i, L_CLONE); 00104 count += ptaGetCount(pta); 00105 ptaDestroy(&pta); 00106 } 00107 fprintf(stderr, "num bg pts = %d\n", count); 00108 if (count != 60602) { 00109 fprintf(stderr, "Error: npix = %d, num bg pts = %d\n", bgcount, count); 00110 ok = FALSE; 00111 } 00112 00113 /* Render the fg boundary pixels on top of pixs. */ 00114 pixa = pixaCreate(4); 00115 pixc = pixRenderRandomCmapPtaa(pixs, ptaafg, 0, 0, 0); 00116 pixSaveTiled(pixc, pixa, 1, 1, 30, 32); 00117 pixDestroy(&pixc); 00118 00119 /* Render the bg boundary pixels on top of pixs. */ 00120 pixc = pixRenderRandomCmapPtaa(pixs, ptaabg, 0, 0, 0); 00121 pixSaveTiled(pixc, pixa, 1, 0, 30, 32); 00122 pixDestroy(&pixc); 00123 00124 pixClearAll(pixs); 00125 00126 /* Render the fg boundary pixels alone. */ 00127 pixc = pixRenderRandomCmapPtaa(pixs, ptaafg, 0, 0, 0); 00128 pixSaveTiled(pixc, pixa, 1, 1, 30, 32); 00129 00130 /* Verify that the fg pixels are the same set as we 00131 * originally started with. */ 00132 pixb = pixConvertTo1(pixc, 255); 00133 pixEqual(pixb, pixfg, &same); 00134 if (!same) { 00135 fprintf(stderr, "Fg pixel set not correct\n"); 00136 ok = FALSE; 00137 } 00138 pixDestroy(&pixc); 00139 pixDestroy(&pixb); 00140 00141 /* Render the fg boundary pixels alone. */ 00142 pixc = pixRenderRandomCmapPtaa(pixs, ptaabg, 0, 0, 0); 00143 pixSaveTiled(pixc, pixa, 1, 0, 30, 32); 00144 00145 /* Verify that the bg pixels are the same set as we 00146 * originally started with. */ 00147 pixb = pixConvertTo1(pixc, 255); 00148 pixEqual(pixb, pixbg, &same); 00149 if (!same) { 00150 fprintf(stderr, "Bg pixel set not correct\n"); 00151 ok = FALSE; 00152 } 00153 pixDestroy(&pixc); 00154 pixDestroy(&pixb); 00155 00156 if (ok) 00157 fprintf(stderr, "OK!\n"); 00158 else 00159 fprintf(stderr, "Error!\n"); 00160 00161 pixd = pixaDisplay(pixa, 0, 0); 00162 pixWrite("/tmp/junkboundary.png", pixd, IFF_PNG); 00163 pixDisplay(pixd, 0, 0); 00164 00165 ptaaDestroy(&ptaafg); 00166 ptaaDestroy(&ptaabg); 00167 pixDestroy(&pixs); 00168 pixDestroy(&pixfg); 00169 pixDestroy(&pixbg); 00170 pixDestroy(&pixd); 00171 pixaDestroy(&pixa); 00172 boxaDestroy(&boxa); 00173 00174 return 0; 00175 } 00176 00177