jboolean Java_com_example_ocr_Pixa_nativeWriteToFileRandomCmap(JNIEnv *env, jclass clazz, jint nativePixa, jstring fileName, jint width, jint height) { PIX *pixtemp; PIXA *pixa = (PIXA *) nativePixa; const char *c_fileName = env->GetStringUTFChars(fileName, NULL); if (c_fileName == NULL) { LOGE("could not extract fileName string!"); return JNI_FALSE; } if (pixaGetCount(pixa) > 0) { pixtemp = pixaDisplayRandomCmap(pixa, (l_int32) width, (l_int32) height); } else { pixtemp = pixCreate((l_int32) width, (l_int32) height, 1); } pixWrite(c_fileName, pixtemp, IFF_BMP); pixDestroy(&pixtemp); env->ReleaseStringUTFChars(fileName, c_fileName); return JNI_TRUE; }
/*! * wshedRenderColors() * * Input: wshed * Return: pixd (initial image with all basins filled), or null on error */ PIX * wshedRenderColors(L_WSHED *wshed) { l_int32 w, h; PIX *pixg, *pixt, *pixc, *pixm, *pixd; PIXA *pixa; PROCNAME("wshedRenderColors"); if (!wshed) return (PIX *) ERROR_PTR("wshed not defined", procName, NULL); wshedBasins(wshed, &pixa, NULL); pixg = pixCopy(NULL, wshed->pixs); pixGetDimensions(wshed->pixs, &w, &h, NULL); pixd = pixConvertTo32(pixg); pixt = pixaDisplayRandomCmap(pixa, w, h); pixc = pixConvertTo32(pixt); pixm = pixaDisplay(pixa, w, h); pixCombineMasked(pixd, pixc, pixm); pixDestroy(&pixg); pixDestroy(&pixt); pixDestroy(&pixc); pixDestroy(&pixm); pixaDestroy(&pixa); return pixd; }
int main(int argc, char **argv) { l_uint8 *array1, *array2; l_int32 n1, n2, n3; size_t size1, size2; FILE *fp; BOXA *boxa1, *boxa2; PIX *pixs, *pix1; PIXA *pixa1; PIXCMAP *cmap; L_REGPARAMS *rp; if (regTestSetup(argc, argv, &rp)) return 1; pixs = pixRead("feyn.tif"); /* --------------------------------------------------------------- * * Test pixConnComp() and pixCountConnComp(), * * with output to both boxa and pixa * * --------------------------------------------------------------- */ /* First, test with 4-cc */ boxa1= pixConnComp(pixs, &pixa1, 4); n1 = boxaGetCount(boxa1); boxa2= pixConnComp(pixs, NULL, 4); n2 = boxaGetCount(boxa2); pixCountConnComp(pixs, 4, &n3); fprintf(stderr, "Number of 4 c.c.: n1 = %d; n2 = %d, n3 = %d\n", n1, n2, n3); regTestCompareValues(rp, n1, n2, 0); /* 0 */ regTestCompareValues(rp, n1, n3, 0); /* 1 */ regTestCompareValues(rp, n1, 4452, 0); /* 2 */ pix1 = pixaDisplay(pixa1, pixGetWidth(pixs), pixGetHeight(pixs)); regTestWritePixAndCheck(rp, pix1, IFF_PNG); /* 3 */ regTestComparePix(rp, pixs, pix1); /* 4 */ pixaDestroy(&pixa1); boxaDestroy(&boxa1); boxaDestroy(&boxa2); pixDestroy(&pix1); /* Test with 8-cc */ boxa1= pixConnComp(pixs, &pixa1, 8); n1 = boxaGetCount(boxa1); boxa2= pixConnComp(pixs, NULL, 8); n2 = boxaGetCount(boxa2); pixCountConnComp(pixs, 8, &n3); fprintf(stderr, "Number of 8 c.c.: n1 = %d; n2 = %d, n3 = %d\n", n1, n2, n3); regTestCompareValues(rp, n1, n2, 0); /* 5 */ regTestCompareValues(rp, n1, n3, 0); /* 6 */ regTestCompareValues(rp, n1, 4305, 0); /* 7 */ pix1 = pixaDisplay(pixa1, pixGetWidth(pixs), pixGetHeight(pixs)); regTestWritePixAndCheck(rp, pix1, IFF_PNG); /* 8 */ regTestComparePix(rp, pixs, pix1); /* 9 */ pixaDestroy(&pixa1); boxaDestroy(&boxa1); boxaDestroy(&boxa2); pixDestroy(&pix1); /* --------------------------------------------------------------- * * Test boxa I/O * * --------------------------------------------------------------- */ lept_mkdir("lept/conn"); boxa1 = pixConnComp(pixs, NULL, 4); fp = lept_fopen("/tmp/lept/conn/boxa1.ba", "wb+"); boxaWriteStream(fp, boxa1); lept_fclose(fp); fp = lept_fopen("/tmp/lept/conn/boxa1.ba", "rb"); boxa2 = boxaReadStream(fp); lept_fclose(fp); fp = lept_fopen("/tmp/lept/conn/boxa2.ba", "wb+"); boxaWriteStream(fp, boxa2); lept_fclose(fp); array1 = l_binaryRead("/tmp/lept/conn/boxa1.ba", &size1); array2 = l_binaryRead("/tmp/lept/conn/boxa2.ba", &size2); regTestCompareStrings(rp, array1, size1, array2, size2); /* 10 */ lept_free(array1); lept_free(array2); boxaDestroy(&boxa1); boxaDestroy(&boxa2); /* --------------------------------------------------------------- * * Just for fun, display each component as a random color in * * cmapped 8 bpp. Background is color 0; it is set to white. * * --------------------------------------------------------------- */ boxa1 = pixConnComp(pixs, &pixa1, 4); pix1 = pixaDisplayRandomCmap(pixa1, pixGetWidth(pixs), pixGetHeight(pixs)); cmap = pixGetColormap(pix1); pixcmapResetColor(cmap, 0, 255, 255, 255); /* reset background to white */ regTestWritePixAndCheck(rp, pix1, IFF_PNG); /* 11 */ if (rp->display) pixDisplay(pix1, 100, 100); boxaDestroy(&boxa1); pixDestroy(&pix1); pixaDestroy(&pixa1); pixDestroy(&pixs); return regTestCleanup(rp); }
int main(int argc, char **argv) { l_int32 i, j, w, h, empty; l_uint32 redval, greenval; l_float32 f; L_WSHED *wshed; PIX *pixs, *pixc, *pixd; PIX *pix1, *pix2, *pix3, *pix4, *pix5, *pix6, *pix7, *pix8; PIXA *pixac; PTA *pta; static char mainName[] = "watershedtest"; if (argc != 1) return ERROR_INT(" Syntax: watershedtest", mainName, 1); pixac = pixaCreate(0); pixs = pixCreate(500, 500, 8); pixGetDimensions(pixs, &w, &h, NULL); for (i = 0; i < 500; i++) { for (j = 0; j < 500; j++) { #if 1 f = 128.0 + 26.3 * sin(0.0438 * (l_float32) i); f += 33.4 * cos(0.0712 * (l_float32) i); f += 18.6 * sin(0.0561 * (l_float32) j); f += 23.6 * cos(0.0327 * (l_float32) j); #else f = 128.0 + 26.3 * sin(0.0238 * (l_float32)i); f += 33.4 * cos(0.0312 * (l_float32)i); f += 18.6 * sin(0.0261 * (l_float32)j); f += 23.6 * cos(0.0207 * (l_float32)j); #endif pixSetPixel(pixs, j, i, (l_int32) f); } } pixSaveTiled(pixs, pixac, 1.0, 1, 10, 32); pixWrite("/tmp/pattern.png", pixs, IFF_PNG); startTimer(); pixLocalExtrema(pixs, 0, 0, &pix1, &pix2); fprintf(stderr, "Time for extrema: %7.3f\n", stopTimer()); pixSetOrClearBorder(pix1, 2, 2, 2, 2, PIX_CLR); composeRGBPixel(255, 0, 0, &redval); composeRGBPixel(0, 255, 0, &greenval); pixc = pixConvertTo32(pixs); pixPaintThroughMask(pixc, pix2, 0, 0, greenval); pixPaintThroughMask(pixc, pix1, 0, 0, redval); pixSaveTiled(pixc, pixac, 1.0, 0, 10, 32); pixWrite("/tmp/pixc.png", pixc, IFF_PNG); pixSaveTiled(pix1, pixac, 1.0, 0, 10, 32); pixSelectMinInConnComp(pixs, pix1, &pta, NULL); /* ptaWriteStream(stderr, pta, 1); */ pix3 = pixGenerateFromPta(pta, w, h); pixSaveTiled(pix3, pixac, 1.0, 1, 10, 32); pix4 = pixConvertTo32(pixs); pixPaintThroughMask(pix4, pix3, 0, 0, greenval); pixSaveTiled(pix4, pixac, 1.0, 0, 10, 32); pix5 = pixRemoveSeededComponents(NULL, pix3, pix1, 8, 2); pixSaveTiled(pix5, pixac, 1.0, 0, 10, 32); pixZero(pix5, &empty); fprintf(stderr, "Is empty? %d\n", empty); pixDestroy(&pix4); pixDestroy(&pix5); wshed = wshedCreate(pixs, pix3, 10, 0); startTimer(); wshedApply(wshed); fprintf(stderr, "Time for wshed: %7.3f\n", stopTimer()); pix6 = pixaDisplayRandomCmap(wshed->pixad, w, h); pixSaveTiled(pix6, pixac, 1.0, 1, 10, 32); numaWriteStream(stderr, wshed->nalevels); pix7 = wshedRenderFill(wshed); pixSaveTiled(pix7, pixac, 1.0, 0, 10, 32); pix8 = wshedRenderColors(wshed); pixSaveTiled(pix8, pixac, 1.0, 0, 10, 32); wshedDestroy(&wshed); pixd = pixaDisplay(pixac, 0, 0); pixDisplay(pixd, 100, 100); pixWrite("/tmp/wshed.png", pixd, IFF_PNG); pixDestroy(&pixd); pixaDestroy(&pixac); pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); pixDestroy(&pix6); pixDestroy(&pix7); pixDestroy(&pix8); pixDestroy(&pixs); pixDestroy(&pixc); ptaDestroy(&pta); return 0; }
int main(int argc, char **argv) { l_int32 w, h, d, w2, h2, i, ncols, ret; l_float32 angle, conf; BOX *box; BOXA *boxa, *boxa2; PIX *pix, *pixs, *pixb, *pixb2, *pixd; PIX *pix1, *pix2, *pix3, *pix4, *pix5, *pix6; PIXA *pixam; /* mask with a single component over each column */ PIXA *pixac, *pixad, *pixat; PIXAA *pixaa, *pixaa2; SEL *selsplit; static char mainName[] = "arabic_lines"; if (argc != 1) return ERROR_INT(" Syntax: arabic_lines", mainName, 1); pixDisplayWrite(NULL, -1); /* init debug output */ /* Binarize input */ pixs = pixRead("arabic.png"); pixGetDimensions(pixs, &w, &h, &d); pix = pixConvertTo1(pixs, 128); /* Deskew */ pixb = pixFindSkewAndDeskew(pix, 1, &angle, &conf); pixDestroy(&pix); fprintf(stderr, "Skew angle: %7.2f degrees; %6.2f conf\n", angle, conf); pixDisplayWrite(pixb, 1); /* Use full image morphology to find columns, at 2x reduction. This only works for very simple layouts where each column of text extends the full height of the input image. */ pixb2 = pixReduceRankBinary2(pixb, 2, NULL); pix1 = pixMorphCompSequence(pixb2, "c5.500", 0); boxa = pixConnComp(pix1, &pixam, 8); ncols = boxaGetCount(boxa); fprintf(stderr, "Num columns: %d\n", ncols); pixDisplayWrite(pix1, 1); /* Use selective region-based morphology to get the textline mask. */ pixad = pixaMorphSequenceByRegion(pixb2, pixam, "c100.3", 0, 0); pixGetDimensions(pixb2, &w2, &h2, NULL); pix2 = pixaDisplay(pixad, w2, h2); pixDisplayWrite(pix2, 1); pixDestroy(&pix2); /* Some of the lines may be touching, so use a HMT to split the lines in each column, and use a pixaa to save the results. */ selsplit = selCreateFromString(seltext, 17, 7, "selsplit"); pixaa = pixaaCreate(ncols); for (i = 0; i < ncols; i++) { pix3 = pixaGetPix(pixad, i, L_CLONE); box = pixaGetBox(pixad, i, L_COPY); pix4 = pixHMT(NULL, pix3, selsplit); pixXor(pix4, pix4, pix3); boxa2 = pixConnComp(pix4, &pixac, 8); pixaaAddPixa(pixaa, pixac, L_INSERT); pixaaAddBox(pixaa, box, L_INSERT); pix5 = pixaDisplayRandomCmap(pixac, 0, 0); pixDisplayWrite(pix5, 1); fprintf(stderr, "Num textlines in col %d: %d\n", i, boxaGetCount(boxa2)); pixDestroy(&pix5); pixDestroy(&pix3); pixDestroy(&pix4); boxaDestroy(&boxa2); } /* Visual output */ ret = system("gthumb /tmp/display/file* &"); pixat = pixaReadFiles("/tmp/display", "file"); pix5 = selDisplayInPix(selsplit, 31, 2); pixaAddPix(pixat, pix5, L_INSERT); pix6 = pixaDisplayTiledAndScaled(pixat, 32, 400, 3, 0, 35, 3); pixWrite("/tmp/result.png", pix6, IFF_PNG); pixaDestroy(&pixat); pixDestroy(&pix6); /* Test pixaa I/O */ pixaaWrite("/tmp/pixaa", pixaa); pixaa2 = pixaaRead("/tmp/pixaa"); pixaaWrite("/tmp/pixaa2", pixaa2); /* Test pixaa display */ pixd = pixaaDisplay(pixaa, w2, h2); pixWrite("/tmp/textlines.png", pixd, IFF_PNG); pixDestroy(&pixd); /* Cleanup */ pixDestroy(&pixb2); pixDestroy(&pix1); pixaDestroy(&pixam); pixaDestroy(&pixad); pixaaDestroy(&pixaa); pixaaDestroy(&pixaa2); boxaDestroy(&boxa); selDestroy(&selsplit); pixDestroy(&pixs); pixDestroy(&pixb); return 0; }
int main(int argc, char **argv) { char *filein; l_int32 i, n, count; BOX *box; BOXA *boxa; PIX *pixs, *pixd; PIXA *pixa; PIXCMAP *cmap; static char mainName[] = "cctest1"; if (argc != 2) return ERROR_INT(" Syntax: cctest1 filein", mainName, 1); filein = argv[1]; if ((pixs = pixRead(filein)) == NULL) return ERROR_INT("pixs not made", mainName, 1); if (pixGetDepth(pixs) != 1) exit(ERROR_INT("pixs not 1 bpp", mainName, 1)); /* Test speed of pixCountConnComp() */ startTimer(); for (i = 0; i < NTIMES; i++) pixCountConnComp(pixs, 4, &count); fprintf(stderr, "Time to compute 4-cc: %6.3f sec\n", stopTimer()/NTIMES); fprintf(stderr, "Number of 4-cc: %d\n", count); startTimer(); for (i = 0; i < NTIMES; i++) pixCountConnComp(pixs, 8, &count); fprintf(stderr, "Time to compute 8-cc: %6.3f sec\n", stopTimer()/NTIMES); fprintf(stderr, "Number of 8-cc: %d\n", count); /* Test speed of pixConnComp(), with only boxa output */ startTimer(); for (i = 0; i < NTIMES; i++) { boxa = pixConnComp(pixs, NULL, 4); boxaDestroy(&boxa); } fprintf(stderr, "Time to compute 4-cc: %6.3f sec\n", stopTimer()/NTIMES); startTimer(); for (i = 0; i < NTIMES; i++) { boxa = pixConnComp(pixs, NULL, 8); boxaDestroy(&boxa); } fprintf(stderr, "Time to compute 8-cc: %6.3f sec\n", stopTimer()/NTIMES); /* Draw outline of each c.c. box */ boxa = pixConnComp(pixs, NULL, 4); n = boxaGetCount(boxa); fprintf(stderr, "Num 4-cc boxes: %d\n", n); for (i = 0; i < n; i++) { box = boxaGetBox(boxa, i, L_CLONE); pixRenderBox(pixs, box, 3, L_FLIP_PIXELS); boxDestroy(&box); /* remember, clones need to be destroyed */ } pixDisplayWrite(pixs, 1); boxaDestroy(&boxa); /* Display each component as a random color in cmapped 8 bpp. * Background is color 0; it is set to white. */ boxa = pixConnComp(pixs, &pixa, 4); pixd = pixaDisplayRandomCmap(pixa, pixGetWidth(pixs), pixGetHeight(pixs)); cmap = pixGetColormap(pixd); pixcmapResetColor(cmap, 0, 255, 255, 255); /* reset background to white */ pixDisplay(pixd, 100, 100); pixDisplayWrite(pixd, 1); boxaDestroy(&boxa); pixDestroy(&pixd); pixaDestroy(&pixa); pixDestroy(&pixs); return 0; }
main(int argc, char **argv) { char *filein, *fileout; l_int32 w, h, d, w2, h2, i, ncols; l_float32 angle, conf; BOX *box; BOXA *boxa, *boxas, *boxad, *boxa2; NUMA *numa; PIX *pixs, *pixt, *pixb, *pixb2, *pixd; PIX *pixtlm, *pixvws; PIX *pixt1, *pixt2, *pixt3, *pixt4, *pixt5, *pixt6; PIXA *pixam, *pixac, *pixad, *pixat; PIXAA *pixaa, *pixaa2; PTA *pta; SEL *selsplit; static char mainName[] = "textlinemask"; if (argc != 3) exit(ERROR_INT(" Syntax: textlinemask filein fileout", mainName, 1)); filein = argv[1]; fileout = argv[2]; pixDisplayWrite(NULL, -1); /* init debug output */ if ((pixs = pixRead(filein)) == NULL) return ERROR_INT("pixs not made", mainName, 1); pixGetDimensions(pixs, &w, &h, &d); /* Binarize input */ if (d == 8) pixt = pixThresholdToBinary(pixs, 128); else if (d == 1) pixt = pixClone(pixs); else { fprintf(stderr, "depth is %d\n", d); exit(1); } /* Deskew */ pixb = pixFindSkewAndDeskew(pixt, 1, &angle, &conf); pixDestroy(&pixt); fprintf(stderr, "Skew angle: %7.2f degrees; %6.2f conf\n", angle, conf); pixDisplayWrite(pixb, DEBUG_OUTPUT); #if 1 /* Use full image morphology to find columns, at 2x reduction. * This only works for very simple layouts where each column * of text extends the full height of the input image. * pixam has a pix component over each column. */ pixb2 = pixReduceRankBinary2(pixb, 2, NULL); pixt1 = pixMorphCompSequence(pixb2, "c5.500", 0); boxa = pixConnComp(pixt1, &pixam, 8); ncols = boxaGetCount(boxa); fprintf(stderr, "Num columns: %d\n", ncols); pixDisplayWrite(pixt1, DEBUG_OUTPUT); /* Use selective region-based morphology to get the textline mask. */ pixad = pixaMorphSequenceByRegion(pixb2, pixam, "c100.3", 0, 0); pixGetDimensions(pixb2, &w2, &h2, NULL); if (DEBUG_OUTPUT) { pixt2 = pixaDisplay(pixad, w2, h2); pixDisplayWrite(pixt2, DEBUG_OUTPUT); pixDestroy(&pixt2); } /* Some of the lines may be touching, so use a HMT to split the * lines in each column, and use a pixaa to save the results. */ selsplit = selCreateFromString(seltext, 17, 7, "selsplit"); pixaa = pixaaCreate(ncols); for (i = 0; i < ncols; i++) { pixt3 = pixaGetPix(pixad, i, L_CLONE); box = pixaGetBox(pixad, i, L_COPY); pixt4 = pixHMT(NULL, pixt3, selsplit); pixXor(pixt4, pixt4, pixt3); boxa2 = pixConnComp(pixt4, &pixac, 8); pixaaAddPixa(pixaa, pixac, L_INSERT); pixaaAddBox(pixaa, box, L_INSERT); if (DEBUG_OUTPUT) { pixt5 = pixaDisplayRandomCmap(pixac, 0, 0); pixDisplayWrite(pixt5, DEBUG_OUTPUT); fprintf(stderr, "Num textlines in col %d: %d\n", i, boxaGetCount(boxa2)); pixDestroy(&pixt5); } pixDestroy(&pixt3); pixDestroy(&pixt4); boxaDestroy(&boxa2); } /* Visual output */ if (DEBUG_OUTPUT) { pixDisplayMultiple("/tmp/junk_write_display*"); pixat = pixaReadFiles("/tmp", "junk_write_display"); pixt5 = selDisplayInPix(selsplit, 31, 2); pixaAddPix(pixat, pixt5, L_INSERT); pixt6 = pixaDisplayTiledAndScaled(pixat, 32, 400, 3, 0, 35, 3); pixWrite(fileout, pixt6, IFF_PNG); pixaDestroy(&pixat); pixDestroy(&pixt6); } /* Test pixaa I/O */ pixaaWrite("/tmp/junkpixaa", pixaa); pixaa2 = pixaaRead("/tmp/junkpixaa"); pixaaWrite("/tmp/junkpixaa2", pixaa2); /* Test pixaa display */ pixd = pixaaDisplay(pixaa, w2, h2); pixWrite("/tmp/junkdisplay", pixd, IFF_PNG); pixDestroy(&pixd); /* Cleanup */ pixDestroy(&pixb2); pixDestroy(&pixt1); pixaDestroy(&pixam); pixaDestroy(&pixad); pixaaDestroy(&pixaa); pixaaDestroy(&pixaa2); boxaDestroy(&boxa); selDestroy(&selsplit); #endif #if 0 /* Use the baseline finder; not really what is needed */ numa = pixFindBaselines(pixb, &pta, 1); #endif #if 0 /* Use the textline mask function; parameters are not quite right */ pixb2 = pixReduceRankBinary2(pixb, 2, NULL); pixtlm = pixGenTextlineMask(pixb2, &pixvws, NULL, 1); pixDisplay(pixtlm, 0, 100); pixDisplay(pixvws, 500, 100); pixDestroy(&pixb2); pixDestroy(&pixtlm); pixDestroy(&pixvws); #endif #if 0 /* Use the Breuel whitespace partition method; slow and we would * still need to work to extract the fg regions. */ pixb2 = pixReduceRankBinary2(pixb, 2, NULL); boxas = pixConnComp(pixb2, NULL, 8); boxad = boxaGetWhiteblocks(boxas, NULL, L_SORT_BY_HEIGHT, 3, 0.1, 200, 0.2, 0); pixd = pixDrawBoxa(pixb2, boxad, 7, 0xe0708000); pixDisplay(pixd, 100, 500); pixDestroy(&pixb2); pixDestroy(&pixd); boxaDestroy(&boxas); boxaDestroy(&boxad); #endif #if 0 /* Use morphology to find columns and then selective * region-based morphology to get the textline mask. * This is for display; we really want to get a pixa of the * specific textline masks. */ startTimer(); pixb2 = pixReduceRankBinary2(pixb, 2, NULL); pixt1 = pixMorphCompSequence(pixb2, "c5.500", 0); /* column mask */ pixt2 = pixMorphSequenceByRegion(pixb2, pixt1, "c100.3", 8, 0, 0, &boxa); fprintf(stderr, "time = %7.3f sec\n", stopTimer()); pixDisplay(pixt1, 100, 500); pixDisplay(pixt2, 800, 500); pixDestroy(&pixb2); pixDestroy(&pixt1); pixDestroy(&pixt2); boxaDestroy(&boxa); #endif pixDestroy(&pixs); pixDestroy(&pixb); exit(0); }
/*! * pixGetRegionsBinary() * * Input: pixs (1 bpp, assumed to be 300 to 400 ppi) * &pixhm (<optional return> halftone mask) * &pixtm (<optional return> textline mask) * &pixtb (<optional return> textblock mask) * debug (flag: set to 1 for debug output) * Return: 0 if OK, 1 on error * * Notes: * (1) It is best to deskew the image before segmenting. * (2) The debug flag enables a number of outputs. These * are included to show how to generate and save/display * these results. */ l_int32 pixGetRegionsBinary(PIX *pixs, PIX **ppixhm, PIX **ppixtm, PIX **ppixtb, l_int32 debug) { char *tempname; l_int32 htfound, tlfound; PIX *pixr, *pixt1, *pixt2; PIX *pixtext; /* text pixels only */ PIX *pixhm2; /* halftone mask; 2x reduction */ PIX *pixhm; /* halftone mask; */ PIX *pixtm2; /* textline mask; 2x reduction */ PIX *pixtm; /* textline mask */ PIX *pixvws; /* vertical white space mask */ PIX *pixtb2; /* textblock mask; 2x reduction */ PIX *pixtbf2; /* textblock mask; 2x reduction; small comps filtered */ PIX *pixtb; /* textblock mask */ PROCNAME("pixGetRegionsBinary"); if (ppixhm) *ppixhm = NULL; if (ppixtm) *ppixtm = NULL; if (ppixtb) *ppixtb = NULL; if (!pixs) return ERROR_INT("pixs not defined", procName, 1); if (pixGetDepth(pixs) != 1) return ERROR_INT("pixs not 1 bpp", procName, 1); /* 2x reduce, to 150 -200 ppi */ pixr = pixReduceRankBinaryCascade(pixs, 1, 0, 0, 0); pixDisplayWrite(pixr, debug); /* Get the halftone mask */ pixhm2 = pixGenHalftoneMask(pixr, &pixtext, &htfound, debug); /* Get the textline mask from the text pixels */ pixtm2 = pixGenTextlineMask(pixtext, &pixvws, &tlfound, debug); /* Get the textblock mask from the textline mask */ pixtb2 = pixGenTextblockMask(pixtm2, pixvws, debug); pixDestroy(&pixr); pixDestroy(&pixtext); pixDestroy(&pixvws); /* Remove small components from the mask, where a small * component is defined as one with both width and height < 60 */ pixtbf2 = pixSelectBySize(pixtb2, 60, 60, 4, L_SELECT_IF_EITHER, L_SELECT_IF_GTE, NULL); pixDestroy(&pixtb2); pixDisplayWriteFormat(pixtbf2, debug, IFF_PNG); /* Expand all masks to full resolution, and do filling or * small dilations for better coverage. */ pixhm = pixExpandReplicate(pixhm2, 2); pixt1 = pixSeedfillBinary(NULL, pixhm, pixs, 8); pixOr(pixhm, pixhm, pixt1); pixDestroy(&pixt1); pixDisplayWriteFormat(pixhm, debug, IFF_PNG); pixt1 = pixExpandReplicate(pixtm2, 2); pixtm = pixDilateBrick(NULL, pixt1, 3, 3); pixDestroy(&pixt1); pixDisplayWriteFormat(pixtm, debug, IFF_PNG); pixt1 = pixExpandReplicate(pixtbf2, 2); pixtb = pixDilateBrick(NULL, pixt1, 3, 3); pixDestroy(&pixt1); pixDisplayWriteFormat(pixtb, debug, IFF_PNG); pixDestroy(&pixhm2); pixDestroy(&pixtm2); pixDestroy(&pixtbf2); /* Debug: identify objects that are neither text nor halftone image */ if (debug) { pixt1 = pixSubtract(NULL, pixs, pixtm); /* remove text pixels */ pixt2 = pixSubtract(NULL, pixt1, pixhm); /* remove halftone pixels */ pixDisplayWriteFormat(pixt2, 1, IFF_PNG); pixDestroy(&pixt1); pixDestroy(&pixt2); } /* Debug: display textline components with random colors */ if (debug) { l_int32 w, h; BOXA *boxa; PIXA *pixa; boxa = pixConnComp(pixtm, &pixa, 8); pixGetDimensions(pixtm, &w, &h, NULL); pixt1 = pixaDisplayRandomCmap(pixa, w, h); pixcmapResetColor(pixGetColormap(pixt1), 0, 255, 255, 255); pixDisplay(pixt1, 100, 100); pixDisplayWriteFormat(pixt1, 1, IFF_PNG); pixaDestroy(&pixa); boxaDestroy(&boxa); pixDestroy(&pixt1); } /* Debug: identify the outlines of each textblock */ if (debug) { PIXCMAP *cmap; PTAA *ptaa; ptaa = pixGetOuterBordersPtaa(pixtb); tempname = genTempFilename("/tmp", "tb_outlines.ptaa", 0, 0); ptaaWrite(tempname, ptaa, 1); FREE(tempname); pixt1 = pixRenderRandomCmapPtaa(pixtb, ptaa, 1, 16, 1); cmap = pixGetColormap(pixt1); pixcmapResetColor(cmap, 0, 130, 130, 130); pixDisplay(pixt1, 500, 100); pixDisplayWriteFormat(pixt1, 1, IFF_PNG); pixDestroy(&pixt1); ptaaDestroy(&ptaa); } /* Debug: get b.b. for all mask components */ if (debug) { BOXA *bahm, *batm, *batb; bahm = pixConnComp(pixhm, NULL, 4); batm = pixConnComp(pixtm, NULL, 4); batb = pixConnComp(pixtb, NULL, 4); tempname = genTempFilename("/tmp", "htmask.boxa", 0, 0); boxaWrite(tempname, bahm); FREE(tempname); tempname = genTempFilename("/tmp", "textmask.boxa", 0, 0); boxaWrite(tempname, batm); FREE(tempname); tempname = genTempFilename("/tmp", "textblock.boxa", 0, 0); boxaWrite(tempname, batb); FREE(tempname); boxaDestroy(&bahm); boxaDestroy(&batm); boxaDestroy(&batb); } if (ppixhm) *ppixhm = pixhm; else pixDestroy(&pixhm); if (ppixtm) *ppixtm = pixtm; else pixDestroy(&pixtm); if (ppixtb) *ppixtb = pixtb; else pixDestroy(&pixtb); return 0; }
void DoWatershed(L_REGPARAMS *rp, PIX *pixs) { l_uint8 *data; size_t size; l_int32 w, h, empty; l_uint32 redval, greenval; L_WSHED *wshed; PIX *pixc, *pix1, *pix2, *pix3, *pix4, *pix5, *pix6, *pix7, *pix8, *pix9; PIXA *pixa; PTA *pta; /* Find local extrema */ pixa = pixaCreate(0); pixGetDimensions(pixs, &w, &h, NULL); regTestWritePixAndCheck(rp, pixs, IFF_PNG); /* 0 */ pixSaveTiled(pixs, pixa, 1.0, 1, 10, 32); startTimer(); pixLocalExtrema(pixs, 0, 0, &pix1, &pix2); fprintf(stderr, "Time for extrema: %7.3f\n", stopTimer()); pixSetOrClearBorder(pix1, 2, 2, 2, 2, PIX_CLR); composeRGBPixel(255, 0, 0, &redval); composeRGBPixel(0, 255, 0, &greenval); pixc = pixConvertTo32(pixs); pixPaintThroughMask(pixc, pix2, 0, 0, greenval); pixPaintThroughMask(pixc, pix1, 0, 0, redval); regTestWritePixAndCheck(rp, pixc, IFF_PNG); /* 1 */ pixSaveTiled(pixc, pixa, 1.0, 0, 10, 32); regTestWritePixAndCheck(rp, pix1, IFF_PNG); /* 2 */ pixSaveTiled(pix1, pixa, 1.0, 0, 10, 32); /* Generate seeds for watershed */ pixSelectMinInConnComp(pixs, pix1, &pta, NULL); pix3 = pixGenerateFromPta(pta, w, h); regTestWritePixAndCheck(rp, pix3, IFF_PNG); /* 3 */ pixSaveTiled(pix3, pixa, 1.0, 1, 10, 32); pix4 = pixConvertTo32(pixs); pixPaintThroughMask(pix4, pix3, 0, 0, greenval); regTestWritePixAndCheck(rp, pix4, IFF_PNG); /* 4 */ pixSaveTiled(pix4, pixa, 1.0, 0, 10, 32); pix5 = pixRemoveSeededComponents(NULL, pix3, pix1, 8, 2); regTestWritePixAndCheck(rp, pix5, IFF_PNG); /* 5 */ pixSaveTiled(pix5, pixa, 1.0, 0, 10, 32); pixZero(pix5, &empty); regTestCompareValues(rp, 1, empty, 0.0); /* 6 */ /* Make and display watershed */ wshed = wshedCreate(pixs, pix3, 10, 0); startTimer(); wshedApply(wshed); fprintf(stderr, "Time for wshed: %7.3f\n", stopTimer()); pix6 = pixaDisplayRandomCmap(wshed->pixad, w, h); regTestWritePixAndCheck(rp, pix6, IFF_PNG); /* 7 */ pixSaveTiled(pix6, pixa, 1.0, 1, 10, 32); numaWriteMem(&data, &size, wshed->nalevels); regTestWriteDataAndCheck(rp, data, size, "na"); /* 8 */ pix7 = wshedRenderFill(wshed); regTestWritePixAndCheck(rp, pix7, IFF_PNG); /* 9 */ pixSaveTiled(pix7, pixa, 1.0, 0, 10, 32); pix8 = wshedRenderColors(wshed); regTestWritePixAndCheck(rp, pix8, IFF_PNG); /* 10 */ pixSaveTiled(pix8, pixa, 1.0, 0, 10, 32); wshedDestroy(&wshed); pix9 = pixaDisplay(pixa, 0, 0); regTestWritePixAndCheck(rp, pix9, IFF_PNG); /* 11 */ pixDisplayWithTitle(pix9, 100, 100, NULL, rp->display); lept_free(data); pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); pixDestroy(&pix4); pixDestroy(&pix5); pixDestroy(&pix6); pixDestroy(&pix7); pixDestroy(&pix8); pixDestroy(&pix9); pixDestroy(&pixc); pixaDestroy(&pixa); ptaDestroy(&pta); }
main(int argc, char **argv) { l_int32 w, h; BOXA *boxa; PIX *pixs, *pixt1, *pixt2, *pixg, *pixb, *pixd, *pixc; PIX *pixm, *pixm2, *pixd2, *pixs2; PIXA *pixa, *pixac; PIXCMAP *cmap, *cmapg; static char mainName[] = "misctest1"; pixac = pixaCreate(0); /* Combine two grayscale images using a mask */ pixd = pixRead("feyn.tif"); pixs = pixRead("rabi.png"); pixm = pixRead("pageseg2-seed.png"); pixd2 = pixScaleToGray2(pixd); pixs2 = pixScaleToGray2(pixs); pixSaveTiled(pixd2, pixac, 2, 1, 40, 32); pixSaveTiled(pixs2, pixac, 2, 0, 40, 0); pixSaveTiled(pixm, pixac, 2, 0, 40, 0); pixCombineMaskedGeneral(pixd2, pixs2, pixm, 100, 100); pixSaveTiled(pixd2, pixac, 2, 1, 40, 0); pixDisplayWithTitle(pixd2, 100, 100, NULL, SHOW); pixDestroy(&pixd2); pixDestroy(&pixs2); /* Combine two binary images using a mask */ pixm2 = pixExpandBinaryReplicate(pixm, 2); pixt1 = pixCopy(NULL, pixd); pixCombineMaskedGeneral(pixd, pixs, pixm2, 200, 200); pixSaveTiled(pixd, pixac, 4, 0, 40, 0); pixDisplayWithTitle(pixd, 700, 100, NULL, SHOW); pixCombineMasked(pixt1, pixs, pixm2); pixSaveTiled(pixt1, pixac, 4, 0, 40, 0); pixDestroy(&pixd); pixDestroy(&pixt1); pixDestroy(&pixs); pixDestroy(&pixm); pixDestroy(&pixm2); /* Do a restricted seedfill */ pixs = pixRead("pageseg2-seed.png"); pixm = pixRead("pageseg2-mask.png"); pixd = pixSeedfillBinaryRestricted(NULL, pixs, pixm, 8, 50, 175); pixSaveTiled(pixs, pixac, 2, 1, 40, 0); pixSaveTiled(pixm, pixac, 2, 0, 40, 0); pixSaveTiled(pixd, pixac, 2, 0, 40, 0); pixDestroy(&pixs); pixDestroy(&pixm); pixDestroy(&pixd); /* Colorize a grayscale image */ pixs = pixRead("lucasta.150.jpg"); pixGetDimensions(pixs, &w, &h, NULL); pixb = pixThresholdToBinary(pixs, 128); boxa = pixConnComp(pixb, &pixa, 8); pixSaveTiled(pixs, pixac, 1, 1, 40, 0); cmap = pixcmapGrayToColor(0x6f90c0); pixSetColormap(pixs, cmap); pixSaveTiled(pixs, pixac, 1, 0, 40, 0); pixc = pixaDisplayRandomCmap(pixa, w, h); pixcmapResetColor(pixGetColormap(pixc), 0, 255, 255, 255); pixSaveTiled(pixc, pixac, 1, 0, 40, 0); pixDestroy(&pixs); pixDestroy(&pixb); pixDestroy(&pixc); boxaDestroy(&boxa); pixaDestroy(&pixa); /* Convert color to gray */ pixs = pixRead("weasel4.16c.png"); pixSaveTiled(pixs, pixac, 1, 1, 20, 0); pixc = pixConvertTo32(pixs); pixt1 = pixConvertRGBToGray(pixc, 3., 7., 5.); pixSaveTiled(pixt1, pixac, 1, 0, 20, 0); pixt2 = pixConvertRGBToGrayFast(pixc); pixSaveTiled(pixt2, pixac, 1, 0, 20, 0); pixg = pixCopy(NULL, pixs); cmap = pixGetColormap(pixs); cmapg = pixcmapColorToGray(cmap, 4., 6., 3.); pixSetColormap(pixg, cmapg); pixSaveTiled(pixg, pixac, 1, 0, 20, 0); pixDestroy(&pixs); pixDestroy(&pixc); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixg); pixd = pixaDisplay(pixac, 0, 0); pixDisplayWithTitle(pixd, 100, 100, NULL, 1); pixWrite("junkmisc1.png", pixd, IFF_PNG); pixDestroy(&pixd); pixaDestroy(&pixac); return 0; }
main(int argc, char **argv) { l_uint8 *array1, *array2; l_int32 i, n, np, same, diff, nbytes1, nbytes2; FILE *fp; BOX *box; BOXA *boxa, *boxa2; PIX *pixs, *pixd; PIXA *pixa; PIXCMAP *cmap; static char mainName[] = "conncomp_reg"; if (argc != 1) exit(ERROR_INT(" Syntax: conncomp_reg", mainName, 1)); if ((pixs = pixRead("feyn.tif")) == NULL) exit(ERROR_INT("pixs not made", mainName, 1)); /* Test pixConnComp() with output to both boxa and pixa */ /* First, test with 4-cc */ boxa = pixConnComp(pixs, &pixa, 4); n = boxaGetCount(boxa); fprintf(stderr, "Number of 4 c.c. b.b: %d\n", n); np = pixaGetCount(pixa); fprintf(stderr, "Number of 4 c.c. pix: %d\n", np); pixd = pixaDisplay(pixa, pixGetWidth(pixs), pixGetHeight(pixs)); pixWrite("/tmp/junkout1.png", pixd, IFF_PNG); pixEqual(pixs, pixd, &same); if (same == 1) fprintf(stderr, "Source and reconstructed pix are the same.\n"); else fprintf(stderr, "Error: source and reconstructed pix differ!\n"); pixaDestroy(&pixa); boxaDestroy(&boxa); pixDestroy(&pixd); /* Test with 8-cc */ boxa = pixConnComp(pixs, &pixa, 8); n = boxaGetCount(boxa); fprintf(stderr, "Number of 8 c.c. b.b: %d\n", n); np = pixaGetCount(pixa); fprintf(stderr, "Number of 8 c.c. pix: %d\n", np); pixd = pixaDisplay(pixa, pixGetWidth(pixs), pixGetHeight(pixs)); pixWrite("/tmp/junkout2.png", pixd, IFF_PNG); pixEqual(pixs, pixd, &same); if (same == 1) fprintf(stderr, "Source and reconstructed pix are the same.\n"); else fprintf(stderr, "Error: source and reconstructed pix differ!\n"); pixaDestroy(&pixa); boxaDestroy(&boxa); pixDestroy(&pixd); /* Test i/o */ boxa = pixConnComp(pixs, NULL, 4); fp = fopen("/tmp/junk1.ba", "wb+"); boxaWriteStream(fp, boxa); fclose(fp); fp = fopen("/tmp/junk1.ba", "r"); boxa2 = boxaReadStream(fp); fclose(fp); fp = fopen("/tmp/junk2.ba", "wb+"); boxaWriteStream(fp, boxa2); fclose(fp); array1 = arrayRead("/tmp/junk1.ba", &nbytes1); array2 = arrayRead("/tmp/junk2.ba", &nbytes2); diff = strcmp((char *)array1, (char *)array2); if (nbytes1 != nbytes2 || diff) fprintf(stderr, "I/O error for boxes.\n"); else fprintf(stderr, "I/O valid for boxes.\n"); FREE(array1); FREE(array2); boxaDestroy(&boxa); boxaDestroy(&boxa2); /* Just for fun, display each component as a random color * in cmapped 8 bpp. Background is color 0; it is set to white. */ boxa = pixConnComp(pixs, &pixa, 4); pixd = pixaDisplayRandomCmap(pixa, pixGetWidth(pixs), pixGetHeight(pixs)); cmap = pixGetColormap(pixd); pixcmapResetColor(cmap, 0, 255, 255, 255); /* reset background to white */ pixDisplay(pixd, 100, 100); boxaDestroy(&boxa); pixDestroy(&pixd); pixaDestroy(&pixa); pixDestroy(&pixs); exit(0); }
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; }