/*! * boxaaWrite() * * Input: filename * boxaa * Return: 0 if OK, 1 on error */ l_int32 boxaaWrite(const char *filename, BOXAA *baa) { FILE *fp; PROCNAME("boxaaWrite"); if (!filename) return ERROR_INT("filename not defined", procName, 1); if (!baa) return ERROR_INT("baa not defined", procName, 1); if ((fp = fopen(filename, "w")) == NULL) return ERROR_INT("stream not opened", procName, 1); if (boxaaWriteStream(fp, baa)) return ERROR_INT("baa not written to stream", procName, 1); fclose(fp); return 0; }
l_int32 main(int argc, char **argv) { char *boxatxt; l_int32 i; BOXA *boxa1, *boxa2, *boxa3; BOXAA *baa, *baa1; NUMAA *naa1; PIX *pixdb, *pix1, *pix2, *pix3, *pix4; PIXA *pixa1, *pixa2, *pixa3, *pixat; L_RECOG *recog; L_RECOGA *recoga; SARRAY *sa1; /* ----- Example identifying samples using training data ----- */ #if 1 /* Read the training data */ pixat = pixaRead("recog/sets/train06.pa"); recog = recogCreateFromPixa(pixat, 0, 0, L_USE_ALL, 128, 1); recoga = recogaCreateFromRecog(recog); pixaDestroy(&pixat); /* Read the data from all samples */ pix1 = pixRead("recog/sets/samples06.png"); boxatxt = pixGetText(pix1); boxa1 = boxaReadMem((l_uint8 *)boxatxt, strlen(boxatxt)); pixa1 = pixaCreateFromBoxa(pix1, boxa1, NULL); pixDestroy(&pix1); /* destroys boxa1 */ /* Identify components in the sample data */ pixa2 = pixaCreate(0); pixa3 = pixaCreate(0); for (i = 0; i < 9; i++) { /* if (i != 4) continue; */ /* dots form separate boxa */ /* if (i != 8) continue; */ /* broken 2 in '24' */ pix1 = pixaGetPix(pixa1, i, L_CLONE); /* Show the 2d box data in the sample */ boxa2 = pixConnComp(pix1, NULL, 8); baa = boxaSort2d(boxa2, NULL, 6, 6, 5); pix2 = boxaaDisplay(baa, 3, 1, 0xff000000, 0x00ff0000, 0, 0); pixaAddPix(pixa3, pix2, L_INSERT); boxaaDestroy(&baa); boxaDestroy(&boxa2); /* Get the numbers in the sample */ recogaIdentifyMultiple(recoga, pix1, 0, 5, 3, &boxa3, NULL, &pixdb, 0); sa1 = recogaExtractNumbers(recoga, boxa3, 0.7, -1, &baa1, &naa1); sarrayWriteStream(stderr, sa1); boxaaWriteStream(stderr, baa1); numaaWriteStream(stderr, naa1); pixaAddPix(pixa2, pixdb, L_INSERT); /* pixaWrite("/tmp/pixa.pa", pixa2); */ pixDestroy(&pix1); boxaDestroy(&boxa3); boxaaDestroy(&baa1); numaaDestroy(&naa1); sarrayDestroy(&sa1); } pix3 = pixaDisplayLinearly(pixa2, L_VERT, 1.0, 0, 20, 1, NULL); pixWrite("/tmp/pix3.png", pix3, IFF_PNG); pix4 = pixaDisplayTiledInRows(pixa3, 32, 1500, 1.0, 0, 20, 2); pixDisplay(pix4, 500, 0); pixWrite("/tmp/pix4.png", pix4, IFF_PNG); pixaDestroy(&pixa2); pixaDestroy(&pixa3); pixDestroy(&pix1); pixDestroy(&pix3); pixDestroy(&pix4); pixaDestroy(&pixa1); boxaDestroy(&boxa1); recogaDestroy(&recoga); #endif return 0; }
main(int argc, char **argv) { l_int32 i, j, w, h, error; l_float32 val1, val2; l_float32 val00, val10, val01, val11, valc00, valc10, valc01, valc11; PIX *pixs, *pixg, *pixt1, *pixt2, *pixt3, *pixt4, *pixt5; FPIXA *fpixam, *fpixav, *fpixarv; BOXAA *baa; static char mainName[] = "quadtreetest"; if (argc != 1) return ERROR_INT(" Syntax: quadtreetest", mainName, 1); /* Test generation of quadtree regions. */ baa = boxaaQuadtreeRegions(1000, 500, 3); boxaaWriteStream(stderr, baa); boxaaDestroy(&baa); baa = boxaaQuadtreeRegions(1001, 501, 3); boxaaWriteStream(stderr, baa); boxaaDestroy(&baa); /* Test quadtree stats generation */ #if 1 pixs = pixRead("rabi.png"); pixg = pixScaleToGray4(pixs); #else pixs = pixRead("test24.jpg"); pixg = pixConvertTo8(pixs, 0); #endif pixQuadtreeMean(pixg, 8, NULL, &fpixam); pixt1 = fpixaDisplayQuadtree(fpixam, 4); pixDisplay(pixt1, 100, 0); pixWrite("/tmp/quadtree1.png", pixt1, IFF_PNG); pixQuadtreeVariance(pixg, 8, NULL, NULL, &fpixav, &fpixarv); pixt2 = fpixaDisplayQuadtree(fpixav, 4); pixDisplay(pixt2, 100, 200); pixWrite("/tmp/quadtree2.png", pixt2, IFF_PNG); pixt3 = fpixaDisplayQuadtree(fpixarv, 4); pixDisplay(pixt3, 100, 400); pixWrite("/tmp/quadtree3.png", pixt3, IFF_PNG); /* Compare with fixed-size tiling at a resolution corresponding * to the deepest level of the quadtree above */ pixt4 = pixGetAverageTiled(pixg, 5, 6, L_MEAN_ABSVAL); pixt5 = pixExpandReplicate(pixt4, 4); pixWrite("/tmp/quadtree4.png", pixt5, IFF_PNG); pixDisplay(pixt5, 800, 0); pixDestroy(&pixt4); pixDestroy(&pixt5); pixt4 = pixGetAverageTiled(pixg, 5, 6, L_STANDARD_DEVIATION); pixt5 = pixExpandReplicate(pixt4, 4); pixWrite("/tmp/quadtree5.png", pixt5, IFF_PNG); pixDisplay(pixt5, 800, 400); /* Test quadtree parent/child access */ error = FALSE; fpixaGetFPixDimensions(fpixam, 4, &w, &h); for (i = 0; i < w; i += 2) { for (j = 0; j < h; j += 2) { quadtreeGetParent(fpixam, 4, j, i, &val1); fpixaGetPixel(fpixam, 3, j / 2, i / 2, &val2); if (val1 != val2) error = TRUE; } } if (error) fprintf(stderr, "\n======================\nError: parent access\n"); else fprintf(stderr, "\n======================\nSuccess: parent access\n"); error = FALSE; for (i = 0; i < w; i++) { for (j = 0; j < h; j++) { quadtreeGetChildren(fpixam, 4, j, i, &val00, &val10, &val01, &val11); fpixaGetPixel(fpixam, 5, 2 * j, 2 * i, &valc00); fpixaGetPixel(fpixam, 5, 2 * j + 1, 2 * i, &valc10); fpixaGetPixel(fpixam, 5, 2 * j, 2 * i + 1, &valc01); fpixaGetPixel(fpixam, 5, 2 * j + 1, 2 * i + 1, &valc11); if ((val00 != valc00) || (val10 != valc10) || (val01 != valc01) || (val11 != valc11)) error = TRUE; } } if (error) fprintf(stderr, "Error: child access\n======================\n"); else fprintf(stderr, "Success: child access\n======================\n"); pixDestroy(&pixs); pixDestroy(&pixg); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); pixDestroy(&pixt4); pixDestroy(&pixt5); fpixaDestroy(&fpixam); fpixaDestroy(&fpixav); fpixaDestroy(&fpixarv); return 0; }