main(int argc, char **argv) { char *filein; l_int32 i, n, ns; BOX *box; BOXA *boxa, *boxas; PIX *pixs, *pixt; PIXA *pixa, *pixas, *pixas2; static char mainName[] = "sorttest"; if (argc != 2) exit(ERROR_INT(" Syntax: sorttest filein", mainName, 1)); filein = argv[1]; if ((pixs = pixRead(filein)) == NULL) exit(ERROR_INT("pixs not made", mainName, 1)); #if 0 boxa = pixConnComp(pixs, NULL, 8); n = boxaGetCount(boxa); boxas = boxaSort(boxa, L_SORT_BY_PERIMETER, L_SORT_DECREASING, NULL); ns = boxaGetCount(boxas); fprintf(stderr, "Number of cc: n = %d, ns = %d\n", n, ns); boxaWrite("/tmp/junkboxa.ba", boxas); for (i = 0; i < n; i++) { box = boxaGetBox(boxas, i, L_CLONE); pixRenderBox(pixs, box, 2, L_FLIP_PIXELS); boxDestroy(&box); } pixWrite("/tmp/junkout.png", pixs, IFF_PNG); boxaDestroy(&boxa); boxaDestroy(&boxas); #endif #if 1 boxa = pixConnComp(pixs, &pixa, 8); n = pixaGetCount(pixa); pixas = pixaSort(pixa, L_SORT_BY_Y, L_SORT_INCREASING, NULL, L_CLONE); ns = pixaGetCount(pixas); fprintf(stderr, "Number of cc: n = %d, ns = %d\n", n, ns); pixaWrite("/tmp/junkpixa.pa", pixas); pixas2 = pixaRead("/tmp/junkpixa.pa"); pixaWrite("/tmp/junkpixa2.pa", pixas2); pixt = pixaDisplayOnLattice(pixas, 100, 100); pixWrite("/tmp/junkpix.png", pixt, IFF_PNG); boxaWrite("/tmp/junkboxa.ba", pixas->boxa); pixDestroy(&pixt); pixaDestroy(&pixa); pixaDestroy(&pixas); pixaDestroy(&pixas2); boxaDestroy(&boxa); #endif pixDestroy(&pixs); return 0; }
main(int argc, char **argv) { l_int32 i, j, w, h; l_int32 minsum[5] = { 2, 40, 50, 50, 70}; l_int32 skipdist[5] = { 5, 5, 10, 10, 30}; l_int32 delta[5] = { 2, 10, 10, 25, 40}; l_int32 maxbg[5] = {10, 15, 10, 20, 40}; BOX *box1, *box2, *box3, *box4; BOXA *boxa; PIX *pixs, *pixc, *pixt, *pixd, *pix32; PIXA *pixas, *pixad; L_REGPARAMS *rp; if (regTestSetup(argc, argv, &rp)) return 1; /* Generate and save 1 bpp masks */ pixas = pixaCreate(0); pixs = pixCreate(300, 250, 1); pixSetAll(pixs); box1 = boxCreate(50, 0, 140, 25); box2 = boxCreate(120, 100, 100, 25); box3 = boxCreate(75, 170, 80, 20); box4 = boxCreate(150, 80, 25, 70); pixClearInRect(pixs, box1); pixaAddPix(pixas, pixs, L_COPY); pixt = pixRotateOrth(pixs, 1); pixaAddPix(pixas, pixt, L_INSERT); pixClearInRect(pixs, box2); pixaAddPix(pixas, pixs, L_COPY); pixt = pixRotateOrth(pixs, 1); pixaAddPix(pixas, pixt, L_INSERT); pixClearInRect(pixs, box3); pixaAddPix(pixas, pixs, L_COPY); pixt = pixRotateOrth(pixs, 1); pixaAddPix(pixas, pixt, L_INSERT); pixClearInRect(pixs, box4); pixaAddPix(pixas, pixs, L_COPY); pixt = pixRotateOrth(pixs, 1); pixaAddPix(pixas, pixt, L_INSERT); boxDestroy(&box1); boxDestroy(&box2); boxDestroy(&box3); boxDestroy(&box4); pixDestroy(&pixs); /* Do 5 splittings on each of the 8 masks */ pixad = pixaCreate(0); for (j = 0; j < 8; j++) { pixt = pixaGetPix(pixas, j, L_CLONE); pixGetDimensions(pixt, &w, &h, NULL); pix32 = pixCreate(w, h, 32); pixSetAll(pix32); pixPaintThroughMask(pix32, pixt, 0, 0, 0xc0c0c000); pixSaveTiled(pix32, pixad, 1, 1, 30, 32); for (i = 0; i < 5; i++) { pixc = pixCopy(NULL, pix32); boxa = pixSplitComponentIntoBoxa(pixt, NULL, minsum[i], skipdist[i], delta[i], maxbg[i], 0, 1); /* boxaWriteStream(stderr, boxa); */ pixd = pixBlendBoxaRandom(pixc, boxa, 0.4); pixRenderBoxaArb(pixd, boxa, 2, 255, 0, 0); pixSaveTiled(pixd, pixad, 1, 0, 30, 32); pixDestroy(&pixd); pixDestroy(&pixc); boxaDestroy(&boxa); } pixDestroy(&pixt); pixDestroy(&pix32); } /* Display results */ pixd = pixaDisplay(pixad, 0, 0); regTestWritePixAndCheck(rp, pixd, IFF_PNG); /* 0 */ pixDisplayWithTitle(pixd, 100, 100, NULL, rp->display); pixDestroy(&pixd); pixaDestroy(&pixad); /* Put the 8 masks all together, and split 5 ways */ pixad = pixaCreate(0); pixs = pixaDisplayOnLattice(pixas, 325, 325); pixGetDimensions(pixs, &w, &h, NULL); pix32 = pixCreate(w, h, 32); pixSetAll(pix32); pixPaintThroughMask(pix32, pixs, 0, 0, 0xc0c0c000); pixSaveTiled(pix32, pixad, 1, 1, 30, 32); for (i = 0; i < 5; i++) { pixc = pixCopy(NULL, pix32); boxa = pixSplitIntoBoxa(pixs, minsum[i], skipdist[i], delta[i], maxbg[i], 0, 1); /* boxaWriteStream(stderr, boxa); */ pixd = pixBlendBoxaRandom(pixc, boxa, 0.4); pixRenderBoxaArb(pixd, boxa, 2, 255, 0, 0); pixSaveTiled(pixd, pixad, 1, 0, 30, 32); pixDestroy(&pixd); pixDestroy(&pixc); boxaDestroy(&boxa); } pixDestroy(&pix32); pixDestroy(&pixs); /* Display results */ pixd = pixaDisplay(pixad, 0, 0); regTestWritePixAndCheck(rp, pixd, IFF_PNG); /* 1 */ pixDisplayWithTitle(pixd, 600, 100, NULL, rp->display); pixDestroy(&pixd); pixaDestroy(&pixad); pixaDestroy(&pixas); regTestCleanup(rp); return 0; }
/* ----------------------------------------------------- */ void ProcessDigits(l_int32 index) { char rootname[8] = "digit5"; char buf[64]; l_int32 i, nc, ns, same; NUMA *na1; PIX *pix1, *pix2, *pix3, *pix4, *pix5, *pix6; PIXA *pixa1, *pixa2, *pixa3; /* Read the unfiltered, unscaled pixa of twenty-five 5s */ snprintf(buf, sizeof(buf), "digits/%s.orig-25.pa", rootname); pixa1 = pixaRead(buf); /* Number and show the input images */ snprintf(buf, sizeof(buf), "/tmp/lept/digit/%s.orig-num", rootname); PixaDisplayNumbered(pixa1, buf); /* Remove some of them */ na1 = numaCreateFromString(removeset); pixaRemoveSelected(pixa1, na1); numaDestroy(&na1); snprintf(buf, sizeof(buf), "/tmp/lept/digit/%s.filt.pa", rootname); pixaWrite(buf, pixa1); /* Number and show the filtered images */ snprintf(buf, sizeof(buf), "/tmp/lept/digit/%s.filt-num", rootname); PixaDisplayNumbered(pixa1, buf); /* Extract the largest c.c., clip to the foreground, * and scale the result to a fixed size. */ nc = pixaGetCount(pixa1); pixa2 = pixaCreate(nc); for (i = 0; i < nc; i++) { pix1 = pixaGetPix(pixa1, i, L_CLONE); /* A threshold of 140 gives reasonable results */ pix2 = pixThresholdToBinary(pix1, 140); /* Join nearly touching pieces */ pix3 = pixCloseSafeBrick(NULL, pix2, 5, 5); /* Take the largest (by area) connected component */ pix4 = pixFilterComponentBySize(pix3, 0, L_SELECT_BY_AREA, 8, NULL); /* Extract the original 1 bpp pixels that have been * covered by the closing operation */ pixAnd(pix4, pix4, pix2); /* Grab the result as an image with no surrounding whitespace */ pixClipToForeground(pix4, &pix5, NULL); /* Rescale the result to the canonical size */ pix6 = pixScaleToSize(pix5, 20, 30); pixaAddPix(pixa2, pix6, L_INSERT); pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); pixDestroy(&pix4); pixDestroy(&pix5); } /* Add the index (a "5") in the text field of each pix; save pixa2 */ snprintf(buf, sizeof(buf), "%d", index); for (i = 0; i < nc; i++) { pix1 = pixaGetPix(pixa2, i, L_CLONE); pixSetText(pix1, buf); pixDestroy(&pix1); } snprintf(buf, sizeof(buf), "/tmp/lept/digit/%s.comp.pa", rootname); pixaWrite(buf, pixa2); /* Number and show the resulting binary templates */ snprintf(buf, sizeof(buf), "/tmp/lept/digit/%s.comp-num", rootname); PixaDisplayNumbered(pixa2, buf); /* Save the binary templates as a packed tiling (tiff g4). * This is the most efficient way to represent the templates. */ pix1 = pixaDisplayOnLattice(pixa2, 20, 30, NULL, NULL); pixDisplay(pix1, 1000, 500); snprintf(buf, sizeof(buf), "/tmp/lept/digit/%s.comp.tif", rootname); pixWrite(buf, pix1, IFF_TIFF_G4); /* The number of templates is in the pix text string; check it. */ pix2 = pixRead(buf); if (sscanf(pixGetText(pix2), "n = %d", &ns) != 1) fprintf(stderr, "Failed to read the number of templates!\n"); if (ns != nc) fprintf(stderr, "(stored = %d) != (actual number = %d)\n", ns, nc); /* Reconstruct the pixa of templates from the tiled compressed * image, and verify that the resulting pixa is the same. */ pixa3 = pixaMakeFromTiledPix(pix1, 20, 30, 0, 0, NULL); pixaEqual(pixa2, pixa3, 0, NULL, &same); if (!same) fprintf(stderr, "Pixa are not the same!\n"); pixDestroy(&pix1); pixDestroy(&pix2); pixaDestroy(&pixa1); pixaDestroy(&pixa2); pixaDestroy(&pixa3); }
main(int argc, char **argv) { l_int32 ws, hs; BOX *box; BOXA *boxa; PIX *pixs, *pixc, *pix32, *pixt, *pixd; PIXA *pixat, *pixas, *pixac; static char mainName[] = "pixadisp_reg"; if (argc != 1) exit(ERROR_INT(" Syntax: pixadisp_reg", mainName, 1)); if ((pixs = pixRead("feyn.tif")) == NULL) exit(ERROR_INT("pixs not made", mainName, 1)); box = boxCreate(683, 799, 970, 479); pixc = pixClipRectangle(pixs, box, NULL); boxDestroy(&box); pixDisplayWrite(pixc, 1); if ((pix32 = pixRead("marge.jpg")) == NULL) exit(ERROR_INT("pix32 not made", mainName, 1)); /* Generate pixas from pixs and pixac from pixc */ boxa = pixConnComp(pixs, &pixat, 8); pixas = pixaSelectBySize(pixat, 60, 60, L_SELECT_IF_BOTH, L_SELECT_IF_LTE, NULL); pixaDestroy(&pixat); boxaDestroy(&boxa); boxa = pixConnComp(pixc, &pixac, 8); boxaDestroy(&boxa); /* pixaDisplay() */ pixGetDimensions(pixs, &ws, &hs, NULL); pixd = pixaDisplay(pixas, ws, hs); pixDisplayWrite(pixd, 1); pixDestroy(&pixd); /* pixaDisplayRandomCmap() */ pixd = pixaDisplayRandomCmap(pixas, ws, hs); /* black bg */ pixDisplayWrite(pixd, 1); pixcmapResetColor(pixGetColormap(pixd), 0, 255, 255, 255); /* white bg */ pixDisplayWrite(pixd, 1); pixDestroy(&pixd); /* pixaDisplayOnLattice() */ pixd = pixaDisplayOnLattice(pixac, 50, 50); pixDisplayWrite(pixd, 1); pixDestroy(&pixd); /* pixaDisplayUnsplit() */ pixat = pixaSplitPix(pix32, 5, 7, 10, 0x0000ff00); pixd = pixaDisplayUnsplit(pixat, 5, 7, 10, 0x00ff0000); pixDisplayWrite(pixd, 1); pixaDestroy(&pixat); pixDestroy(&pixd); /* pixaDisplayTiled() */ pixd = pixaDisplayTiled(pixac, 1000, 0, 10); pixDisplayWrite(pixd, 1); pixDestroy(&pixd); /* pixaDisplayTiledInRows() */ pixd = pixaDisplayTiledInRows(pixac, 1, 1000, 1.0, 0, 10, 2); pixDisplayWrite(pixd, 1); pixDestroy(&pixd); /* pixaDisplayTiledAndScaled() */ pixd = pixaDisplayTiledAndScaled(pixac, 1, 25, 20, 0, 5, 0); pixDisplayWrite(pixd, 1); pixDestroy(&pixd); pixat = pixaCreate(10); pixd = pixRankFilter(pix32, 8, 8, 0.5); pixaAddPix(pixat, pixd, L_INSERT); pixt = pixScale(pix32, 0.5, 0.5); pixd = pixRankFilter(pixt, 8, 8, 0.5); pixaAddPix(pixat, pixd, L_INSERT); pixDestroy(&pixt); pixt = pixScale(pix32, 0.25, 0.25); pixd = pixRankFilter(pixt, 8, 8, 0.5); pixaAddPix(pixat, pixd, L_INSERT); pixDestroy(&pixt); pixd = pixaDisplayTiledAndScaled(pixat, 32, 500, 1, 0, 25, 0); pixDisplayWrite(pixd, 1); pixaDestroy(&pixat); pixDestroy(&pixd); pixaDestroy(&pixas); pixaDestroy(&pixac); pixDestroy(&pixs); pixDestroy(&pixc); pixDestroy(&pix32); pixDisplayMultiple("/tmp/junk_write_display*"); return 0; }