/*! * numaaReadStream() * * Input: stream * Return: naa, or null on error */ NUMAA * numaaReadStream(FILE *fp) { l_int32 i, n, index, ret, version; NUMA *na; NUMAA *naa; PROCNAME("numaaReadStream"); if (!fp) return (NUMAA *)ERROR_PTR("stream not defined", procName, NULL); ret = fscanf(fp, "\nNumaa Version %d\n", &version); if (ret != 1) return (NUMAA *)ERROR_PTR("not a numa file", procName, NULL); if (version != NUMA_VERSION_NUMBER) return (NUMAA *)ERROR_PTR("invalid numaa version", procName, NULL); if (fscanf(fp, "Number of numa = %d\n\n", &n) != 1) return (NUMAA *)ERROR_PTR("invalid number of numa", procName, NULL); if ((naa = numaaCreate(n)) == NULL) return (NUMAA *)ERROR_PTR("naa not made", procName, NULL); for (i = 0; i < n; i++) { if (fscanf(fp, "Numa[%d]:", &index) != 1) return (NUMAA *)ERROR_PTR("invalid numa header", procName, NULL); if ((na = numaReadStream(fp)) == NULL) return (NUMAA *)ERROR_PTR("na not made", procName, NULL); numaaAddNuma(naa, na, L_INSERT); } return naa; }
/*! * numaaCreateFull() * * Input: nptr: size of numa ptr array to be alloc'd * n: size of individual numa arrays to be alloc'd (0 for default) * Return: naa, or null on error * * Notes: * (1) This allocates numaa and fills the array with allocated numas. * In use, after calling this function, use * numaaAddNumber(naa, index, val); * to add val to the index-th numa in naa. */ NUMAA * numaaCreateFull(l_int32 nptr, l_int32 n) { l_int32 i; NUMAA *naa; NUMA *na; naa = numaaCreate(nptr); for (i = 0; i < nptr; i++) { na = numaCreate(n); numaaAddNuma(naa, na, L_INSERT); } return naa; }
/*! * \brief boxaExtractSortedPattern() * * \param[in] boxa typ. of word bounding boxes, in textline order * \param[in] na index of textline for each box in boxa * \return naa NUMAA, where each numa represents one textline, * or NULL on error * * <pre> * Notes: * (1) The input is expected to come from pixGetWordBoxesInTextlines(). * (2) Each numa in the output consists of an average y coordinate * of the first box in the textline, followed by pairs of * x coordinates representing the left and right edges of each * of the boxes in the textline. * </pre> */ NUMAA * boxaExtractSortedPattern(BOXA *boxa, NUMA *na) { l_int32 index, nbox, row, prevrow, x, y, w, h; BOX *box; NUMA *nad; NUMAA *naa; PROCNAME("boxaExtractSortedPattern"); if (!boxa) return (NUMAA *)ERROR_PTR("boxa not defined", procName, NULL); if (!na) return (NUMAA *)ERROR_PTR("na not defined", procName, NULL); naa = numaaCreate(0); nbox = boxaGetCount(boxa); if (nbox == 0) return naa; prevrow = -1; for (index = 0; index < nbox; index++) { box = boxaGetBox(boxa, index, L_CLONE); numaGetIValue(na, index, &row); if (row > prevrow) { if (index > 0) numaaAddNuma(naa, nad, L_INSERT); nad = numaCreate(0); prevrow = row; boxGetGeometry(box, NULL, &y, NULL, &h); numaAddNumber(nad, y + h / 2); } boxGetGeometry(box, &x, NULL, &w, NULL); numaAddNumber(nad, x); numaAddNumber(nad, x + w - 1); boxDestroy(&box); } numaaAddNuma(naa, nad, L_INSERT); return naa; }
int main(int argc, char **argv) { char filename[BUF_SIZE]; char *dirin, *rootname, *fname; l_int32 i, j, w, h, firstpage, npages, nfiles, ncomp; l_int32 index, ival, rval, gval, bval; BOX *box; BOXA *boxa; BOXAA *baa; JBDATA *data; JBCLASSER *classer; NUMA *nai; NUMAA *naa; SARRAY *safiles; PIX *pixs, *pixt1, *pixt2, *pixd; PIXCMAP *cmap; static char mainName[] = "wordsinorder"; if (argc != 3 && argc != 5) return ERROR_INT( " Syntax: wordsinorder dirin rootname [firstpage, npages]", mainName, 1); dirin = argv[1]; rootname = argv[2]; if (argc == 3) { firstpage = 0; npages = 0; } else { firstpage = atoi(argv[3]); npages = atoi(argv[4]); } /* Compute the word bounding boxes at 2x reduction, along with * the textlines that they are in. */ safiles = getSortedPathnamesInDirectory(dirin, NULL, firstpage, npages); nfiles = sarrayGetCount(safiles); baa = boxaaCreate(nfiles); naa = numaaCreate(nfiles); for (i = 0; i < nfiles; i++) { fname = sarrayGetString(safiles, i, 0); if ((pixs = pixRead(fname)) == NULL) { L_WARNING("image file %d not read\n", mainName, i); continue; } pixGetWordBoxesInTextlines(pixs, 2, MIN_WORD_WIDTH, MIN_WORD_HEIGHT, MAX_WORD_WIDTH, MAX_WORD_HEIGHT, &boxa, &nai); boxaaAddBoxa(baa, boxa, L_INSERT); numaaAddNuma(naa, nai, L_INSERT); #if RENDER_PAGES /* Show the results on a 2x reduced image, where each * word is outlined and the color of the box depends on the * computed textline. */ pixt1 = pixReduceRankBinary2(pixs, 2, NULL); pixGetDimensions(pixt1, &w, &h, NULL); pixd = pixCreate(w, h, 8); cmap = pixcmapCreateRandom(8, 1, 1); /* first color is black */ pixSetColormap(pixd, cmap); pixt2 = pixUnpackBinary(pixt1, 8, 1); pixRasterop(pixd, 0, 0, w, h, PIX_SRC | PIX_DST, pixt2, 0, 0); ncomp = boxaGetCount(boxa); for (j = 0; j < ncomp; j++) { box = boxaGetBox(boxa, j, L_CLONE); numaGetIValue(nai, j, &ival); index = 1 + (ival % 254); /* omit black and white */ pixcmapGetColor(cmap, index, &rval, &gval, &bval); pixRenderBoxArb(pixd, box, 2, rval, gval, bval); boxDestroy(&box); } snprintf(filename, BUF_SIZE, "%s.%05d", rootname, i); fprintf(stderr, "filename: %s\n", filename); pixWrite(filename, pixd, IFF_PNG); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixs); pixDestroy(&pixd); #endif /* RENDER_PAGES */ } boxaaDestroy(&baa); numaaDestroy(&naa); sarrayDestroy(&safiles); return 0; }
l_int32 main(int argc, char **argv) { l_int32 ret, i, n, similar, x1, y1, val1, val2, val3, val4; l_float32 minave, minave2, maxave, fract; NUMA *na1, *na2, *na3, *na4, *na5, *na6; NUMAA *naa; PIX *pixs, *pix1, *pix2, *pix3, *pix4; L_REGPARAMS *rp; if (regTestSetup(argc, argv, &rp)) return 1; pixs = pixRead("feyn.tif"); pix1 = pixScaleToGray6(pixs); pixDisplayWithTitle(pix1, 100, 600, NULL, rp->display); /* Find averages of min and max along about 120 horizontal lines */ fprintf(stderr, "Ignore the following 12 error messages:\n"); na1 = numaCreate(0); na3 = numaCreate(0); for (y1 = 40; y1 < 575; y1 += 5) { ret = pixMinMaxNearLine(pix1, 20, y1, 400, y1, 5, L_SCAN_BOTH, NULL, NULL, &minave, &maxave); if (!ret) { numaAddNumber(na1, (l_int32)minave); numaAddNumber(na3, (l_int32)maxave); if (rp->display) fprintf(stderr, "y = %d: minave = %d, maxave = %d\n", y1, (l_int32)minave, (l_int32)maxave); } } /* Find averages along about 120 vertical lines. We've rotated * the image by 90 degrees, so the results should be nearly * identical to the first set. Also generate a single-sided * scan (L_SCAN_NEGATIVE) for comparison with the double-sided scans. */ pix2 = pixRotateOrth(pix1, 3); pixDisplayWithTitle(pix2, 600, 600, NULL, rp->display); na2 = numaCreate(0); na4 = numaCreate(0); na5 = numaCreate(0); for (x1 = 40; x1 < 575; x1 += 5) { ret = pixMinMaxNearLine(pix2, x1, 20, x1, 400, 5, L_SCAN_BOTH, NULL, NULL, &minave, &maxave); pixMinMaxNearLine(pix2, x1, 20, x1, 400, 5, L_SCAN_NEGATIVE, NULL, NULL, &minave2, NULL); if (!ret) { numaAddNumber(na2, (l_int32)minave); numaAddNumber(na4, (l_int32)maxave); numaAddNumber(na5, (l_int32)minave2); if (rp->display) fprintf(stderr, "x = %d: minave = %d, minave2 = %d, maxave = %d\n", x1, (l_int32)minave, (l_int32)minave2, (l_int32)maxave); } } numaSimilar(na1, na2, 3.0, &similar); /* should be TRUE */ regTestCompareValues(rp, similar, 1, 0); /* 0 */ numaSimilar(na3, na4, 1.0, &similar); /* should be TRUE */ regTestCompareValues(rp, similar, 1, 0); /* 1 */ numaWrite("/tmp/lept/regout/na1.na", na1); numaWrite("/tmp/lept/regout/na2.na", na2); numaWrite("/tmp/lept/regout/na3.na", na3); numaWrite("/tmp/lept/regout/na4.na", na4); numaWrite("/tmp/lept/regout/na5.na", na5); regTestCheckFile(rp, "/tmp/lept/regout/na1.na"); /* 2 */ regTestCheckFile(rp, "/tmp/lept/regout/na2.na"); /* 3 */ regTestCheckFile(rp, "/tmp/lept/regout/na3.na"); /* 4 */ regTestCheckFile(rp, "/tmp/lept/regout/na4.na"); /* 5 */ regTestCheckFile(rp, "/tmp/lept/regout/na5.na"); /* 6 */ /* Plot the average minimums for the 3 cases */ naa = numaaCreate(3); numaaAddNuma(naa, na1, L_INSERT); /* portrait, double-sided */ numaaAddNuma(naa, na2, L_INSERT); /* landscape, double-sided */ numaaAddNuma(naa, na5, L_INSERT); /* landscape, single-sided */ gplotSimpleN(naa, GPLOT_PNG, "/tmp/lept/regout/nearline", "Average minimums along lines"); #if 0 #ifndef _WIN32 sleep(1); #else Sleep(1000); #endif /* _WIN32 */ #endif pix3 = pixRead("/tmp/lept/regout/nearline.png"); regTestWritePixAndCheck(rp, pix3, IFF_PNG); /* 7 */ pixDisplayWithTitle(pix3, 100, 100, NULL, rp->display); if (rp->display) { n = numaGetCount(na3); for (i = 0; i < n; i++) { numaGetIValue(na1, i, &val1); numaGetIValue(na2, i, &val2); numaGetIValue(na3, i, &val3); numaGetIValue(na4, i, &val4); fprintf(stderr, "val1 = %d, val2 = %d, diff = %d; " "val3 = %d, val4 = %d, diff = %d\n", val1, val2, L_ABS(val1 - val2), val3, val4, L_ABS(val3 - val4)); } } numaaDestroy(&naa); numaDestroy(&na3); numaDestroy(&na4); /* Plot minima along a single line, with different distances */ pixMinMaxNearLine(pix1, 20, 200, 400, 200, 2, L_SCAN_BOTH, &na1, NULL, NULL, NULL); pixMinMaxNearLine(pix1, 20, 200, 400, 200, 5, L_SCAN_BOTH, &na2, NULL, NULL, NULL); pixMinMaxNearLine(pix1, 20, 200, 400, 200, 15, L_SCAN_BOTH, &na3, NULL, NULL, NULL); numaWrite("/tmp/lept/regout/na6.na", na1); regTestCheckFile(rp, "/tmp/lept/regout/na6.na"); /* 8 */ n = numaGetCount(na1); fract = 100.0 / n; na4 = numaTransform(na1, 0.0, fract); na5 = numaTransform(na2, 0.0, fract); na6 = numaTransform(na3, 0.0, fract); numaDestroy(&na1); numaDestroy(&na2); numaDestroy(&na3); na1 = numaUniformSampling(na4, 100); na2 = numaUniformSampling(na5, 100); na3 = numaUniformSampling(na6, 100); naa = numaaCreate(3); numaaAddNuma(naa, na1, L_INSERT); numaaAddNuma(naa, na2, L_INSERT); numaaAddNuma(naa, na3, L_INSERT); gplotSimpleN(naa, GPLOT_PNG, "/tmp/lept/regout/nearline2", "Min along line"); pix4 = pixRead("/tmp/lept/regout/nearline2.png"); regTestWritePixAndCheck(rp, pix4, IFF_PNG); /* 9 */ pixDisplayWithTitle(pix4, 800, 100, NULL, rp->display); numaaDestroy(&naa); numaDestroy(&na4); numaDestroy(&na5); numaDestroy(&na6); pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); pixDestroy(&pix4); pixDestroy(&pixs); return regTestCleanup(rp); }
int main(int argc, char **argv) { char label[512]; l_int32 rval, gval, bval, w, h, i, j, rwhite, gwhite, bwhite, count; l_uint32 pixel; GPLOT *gplot1, *gplot2; NUMA *naseq, *na; NUMAA *naa1, *naa2; PIX *pixs, *pixt, *pixt0, *pixt1, *pixt2; PIX *pixr, *pixg, *pixb; PIXA *pixa; PIXCMAP *cmap; static char mainName[] = "colorspacetest"; if (argc != 2) return ERROR_INT(" Syntax: colorspacetest filein", mainName, 1); if ((pixs = pixRead(argv[1])) == NULL) return ERROR_INT("pixs not made", mainName, 1); /* Generate colors by sampling hue with max sat and value. * This was used to make the color strip 19-colors.png. */ pixa = pixaCreate(19); for (i = 0; i < 19; i++) { convertHSVToRGB((240 * i / 18), 255, 255, &rval, &gval, &bval); composeRGBPixel(rval, gval, bval, &pixel); pixt1 = pixCreate(50, 100, 32); pixSetAllArbitrary(pixt1, pixel); pixaAddPix(pixa, pixt1, L_INSERT); } pixt2 = pixaDisplayTiledInRows(pixa, 32, 1100, 1.0, 0, 0, 0); pixDisplayWrite(pixt2, 1); pixDestroy(&pixt2); pixaDestroy(&pixa); /* Colorspace conversion in rgb */ pixDisplayWrite(pixs, 1); pixt = pixConvertRGBToHSV(NULL, pixs); pixDisplayWrite(pixt, 1); pixConvertHSVToRGB(pixt, pixt); pixDisplayWrite(pixt, 1); pixDestroy(&pixt); /* Colorspace conversion on a colormap */ pixt = pixOctreeQuantNumColors(pixs, 25, 0); pixDisplayWrite(pixt, 1); cmap = pixGetColormap(pixt); pixcmapWriteStream(stderr, cmap); pixcmapConvertRGBToHSV(cmap); pixcmapWriteStream(stderr, cmap); pixDisplayWrite(pixt, 1); pixcmapConvertHSVToRGB(cmap); pixcmapWriteStream(stderr, cmap); pixDisplayWrite(pixt, 1); pixDestroy(&pixt); /* Color content extraction */ pixColorContent(pixs, 0, 0, 0, 0, &pixr, &pixg, &pixb); pixDisplayWrite(pixr, 1); pixDisplayWrite(pixg, 1); pixDisplayWrite(pixb, 1); pixDestroy(&pixr); pixDestroy(&pixg); pixDestroy(&pixb); /* Color content measurement */ pixa = pixaCreate(20); naseq = numaMakeSequence(100, 5, 20); naa1 = numaaCreate(6); naa2 = numaaCreate(6); for (i = 0; i < 6; i++) { na = numaCreate(20); numaaAddNuma(naa1, na, L_COPY); numaaAddNuma(naa2, na, L_INSERT); } pixGetDimensions(pixs, &w, &h, NULL); for (i = 0; i < 20; i++) { rwhite = 100 + 5 * i; gwhite = 200 - 5 * i; bwhite = 150; pixt0 = pixGlobalNormRGB(NULL, pixs, rwhite, gwhite, bwhite, 255); pixaAddPix(pixa, pixt0, L_INSERT); pixt1 = pixColorMagnitude(pixs, rwhite, gwhite, bwhite, L_MAX_DIFF_FROM_AVERAGE_2); for (j = 0; j < 6; j++) { pixt2 = pixThresholdToBinary(pixt1, 30 + 10 * j); pixInvert(pixt2, pixt2); pixCountPixels(pixt2, &count, NULL); na = numaaGetNuma(naa1, j, L_CLONE); numaAddNumber(na, (l_float32)count / (l_float32)(w * h)); numaDestroy(&na); pixDestroy(&pixt2); } pixDestroy(&pixt1); pixt1 = pixColorMagnitude(pixs, rwhite, gwhite, bwhite, L_MAX_MIN_DIFF_FROM_2); for (j = 0; j < 6; j++) { pixt2 = pixThresholdToBinary(pixt1, 30 + 10 * j); pixInvert(pixt2, pixt2); pixCountPixels(pixt2, &count, NULL); na = numaaGetNuma(naa2, j, L_CLONE); numaAddNumber(na, (l_float32)count / (l_float32)(w * h)); numaDestroy(&na); pixDestroy(&pixt2); } pixDestroy(&pixt1); } gplot1 = gplotCreate("/tmp/junkplot1", GPLOT_X11, "Fraction with given color (diff from average)", "white point space for red", "amount of color"); gplot2 = gplotCreate("/tmp/junkplot2", GPLOT_X11, "Fraction with given color (min diff)", "white point space for red", "amount of color"); for (j = 0; j < 6; j++) { na = numaaGetNuma(naa1, j, L_CLONE); sprintf(label, "thresh %d", 30 + 10 * j); gplotAddPlot(gplot1, naseq, na, GPLOT_LINES, label); numaDestroy(&na); na = numaaGetNuma(naa2, j, L_CLONE); gplotAddPlot(gplot2, naseq, na, GPLOT_LINES, label); numaDestroy(&na); } gplotMakeOutput(gplot1); gplotMakeOutput(gplot2); gplotDestroy(&gplot1); gplotDestroy(&gplot2); pixt1 = pixaDisplayTiledAndScaled(pixa, 32, 250, 4, 0, 10, 2); pixWrite("/tmp/junkcolormag", pixt1, IFF_PNG); pixDisplayWithTitle(pixt1, 0, 100, "Color magnitude", 1); pixDestroy(&pixt1); pixaDestroy(&pixa); numaDestroy(&naseq); numaaDestroy(&naa1); numaaDestroy(&naa2); pixDisplayMultiple("/tmp/display/file*"); pixDestroy(&pixs); return 0; }
int main(int argc, char **argv) { char label[512]; l_int32 rval, gval, bval, w, h, i, j, rwhite, gwhite, bwhite, count; l_uint32 pixel; GPLOT *gplot1, *gplot2; NUMA *naseq, *na; NUMAA *naa1, *naa2; PIX *pixs, *pixt, *pixt0, *pixt1, *pixt2; PIX *pixr, *pixg, *pixb; /* for color content extraction */ PIXA *pixa, *pixat; PIXCMAP *cmap; L_REGPARAMS *rp; if (regTestSetup(argc, argv, &rp)) return 1; /* Generate a pdf of results when called with display */ pixa = pixaCreate(0); /* Generate colors by sampling hue with max sat and value. * This image has been saved as 19-colors.png. */ pixat = pixaCreate(19); for (i = 0; i < 19; i++) { convertHSVToRGB((240 * i / 18), 255, 255, &rval, &gval, &bval); composeRGBPixel(rval, gval, bval, &pixel); pixt1 = pixCreate(50, 100, 32); pixSetAllArbitrary(pixt1, pixel); pixaAddPix(pixat, pixt1, L_INSERT); } pixt2 = pixaDisplayTiledInRows(pixat, 32, 1100, 1.0, 0, 0, 0); regTestWritePixAndCheck(rp, pixt2, IFF_PNG); /* 0 */ pixaAddPix(pixa, pixt2, L_INSERT); pixaDestroy(&pixat); /* Colorspace conversion in rgb */ pixs = pixRead("wyom.jpg"); pixaAddPix(pixa, pixs, L_INSERT); pixt = pixConvertRGBToHSV(NULL, pixs); regTestWritePixAndCheck(rp, pixt, IFF_JFIF_JPEG); /* 1 */ pixaAddPix(pixa, pixt, L_COPY); pixConvertHSVToRGB(pixt, pixt); regTestWritePixAndCheck(rp, pixt, IFF_JFIF_JPEG); /* 2 */ pixaAddPix(pixa, pixt, L_INSERT); /* Colorspace conversion on a colormap */ pixt = pixOctreeQuantNumColors(pixs, 25, 0); regTestWritePixAndCheck(rp, pixt, IFF_JFIF_JPEG); /* 3 */ pixaAddPix(pixa, pixt, L_COPY); cmap = pixGetColormap(pixt); if (rp->display) pixcmapWriteStream(stderr, cmap); pixcmapConvertRGBToHSV(cmap); if (rp->display) pixcmapWriteStream(stderr, cmap); regTestWritePixAndCheck(rp, pixt, IFF_JFIF_JPEG); /* 4 */ pixaAddPix(pixa, pixt, L_COPY); pixcmapConvertHSVToRGB(cmap); if (rp->display) pixcmapWriteStream(stderr, cmap); regTestWritePixAndCheck(rp, pixt, IFF_JFIF_JPEG); /* 5 */ pixaAddPix(pixa, pixt, L_INSERT); /* Color content extraction */ pixColorContent(pixs, 0, 0, 0, 0, &pixr, &pixg, &pixb); regTestWritePixAndCheck(rp, pixr, IFF_JFIF_JPEG); /* 6 */ pixaAddPix(pixa, pixr, L_INSERT); regTestWritePixAndCheck(rp, pixg, IFF_JFIF_JPEG); /* 7 */ pixaAddPix(pixa, pixg, L_INSERT); regTestWritePixAndCheck(rp, pixb, IFF_JFIF_JPEG); /* 8 */ pixaAddPix(pixa, pixb, L_INSERT); /* Color content measurement. This tests the global * mapping of (r,g,b) --> (white), for 20 different * values of (r,g,b). For each mappings, we compute * the color magnitude and threshold it at six values. * For each of those six thresholds, we plot the * fraction of pixels that exceeds the threshold * color magnitude, where the red value (mapped to * white) goes between 100 and 195. */ pixat = pixaCreate(20); naseq = numaMakeSequence(100, 5, 20); naa1 = numaaCreate(6); naa2 = numaaCreate(6); for (i = 0; i < 6; i++) { na = numaCreate(20); numaaAddNuma(naa1, na, L_COPY); numaaAddNuma(naa2, na, L_INSERT); } pixGetDimensions(pixs, &w, &h, NULL); for (i = 0; i < 20; i++) { rwhite = 100 + 5 * i; gwhite = 200 - 5 * i; bwhite = 150; pixt0 = pixGlobalNormRGB(NULL, pixs, rwhite, gwhite, bwhite, 255); pixaAddPix(pixat, pixt0, L_INSERT); pixt1 = pixColorMagnitude(pixs, rwhite, gwhite, bwhite, L_MAX_DIFF_FROM_AVERAGE_2); for (j = 0; j < 6; j++) { pixt2 = pixThresholdToBinary(pixt1, 30 + 10 * j); pixInvert(pixt2, pixt2); pixCountPixels(pixt2, &count, NULL); na = numaaGetNuma(naa1, j, L_CLONE); numaAddNumber(na, (l_float32)count / (l_float32)(w * h)); numaDestroy(&na); pixDestroy(&pixt2); } pixDestroy(&pixt1); pixt1 = pixColorMagnitude(pixs, rwhite, gwhite, bwhite, L_MAX_MIN_DIFF_FROM_2); for (j = 0; j < 6; j++) { pixt2 = pixThresholdToBinary(pixt1, 30 + 10 * j); pixInvert(pixt2, pixt2); pixCountPixels(pixt2, &count, NULL); na = numaaGetNuma(naa2, j, L_CLONE); numaAddNumber(na, (l_float32)count / (l_float32)(w * h)); numaDestroy(&na); pixDestroy(&pixt2); } pixDestroy(&pixt1); } gplot1 = gplotCreate("/tmp/regout/colorspace.10", GPLOT_PNG, "Fraction with given color (diff from average)", "white point space for red", "amount of color"); gplot2 = gplotCreate("/tmp/regout/colorspace.11", GPLOT_PNG, "Fraction with given color (min diff)", "white point space for red", "amount of color"); for (j = 0; j < 6; j++) { na = numaaGetNuma(naa1, j, L_CLONE); sprintf(label, "thresh %d", 30 + 10 * j); gplotAddPlot(gplot1, naseq, na, GPLOT_LINES, label); numaDestroy(&na); na = numaaGetNuma(naa2, j, L_CLONE); gplotAddPlot(gplot2, naseq, na, GPLOT_LINES, label); numaDestroy(&na); } gplotMakeOutput(gplot1); gplotMakeOutput(gplot2); gplotDestroy(&gplot1); gplotDestroy(&gplot2); pixt1 = pixaDisplayTiledAndScaled(pixat, 32, 250, 4, 0, 10, 2); regTestWritePixAndCheck(rp, pixt1, IFF_JFIF_JPEG); /* 9 */ pixaAddPix(pixa, pixt1, L_INSERT); pixDisplayWithTitle(pixt1, 0, 100, "Color magnitude", rp->display); pixaDestroy(&pixat); numaDestroy(&naseq); numaaDestroy(&naa1); numaaDestroy(&naa2); /* Give gnuplot time to write out the files */ #ifndef _WIN32 sleep(1); #else Sleep(1000); #endif /* _WIN32 */ /* Save as golden files, or check against them */ regTestCheckFile(rp, "/tmp/regout/colorspace.10.png"); /* 10 */ regTestCheckFile(rp, "/tmp/regout/colorspace.11.png"); /* 11 */ if (rp->display) { pixt = pixRead("/tmp/regout/colorspace.10.png"); pixaAddPix(pixa, pixt, L_INSERT); pixt = pixRead("/tmp/regout/colorspace.11.png"); pixaAddPix(pixa, pixt, L_INSERT); pixaConvertToPdf(pixa, 0, 1.0, 0, 0, "colorspace tests", "/tmp/regout/colorspace.pdf"); L_INFO("Output pdf: /tmp/regout/colorspace.pdf\n", rp->testname); } pixaDestroy(&pixa); return regTestCleanup(rp); }
/*! * boxaSort2d() * * Input: boxas * &naa (<optional return> numaa with sorted indices * whose values are the indices of the input array) * delta1 (min overlap that permits aggregation of a box * onto a boxa of horizontally-aligned boxes; pass 1) * delta2 (min overlap that permits aggregation of a box * onto a boxa of horizontally-aligned boxes; pass 2) * minh1 (components less than this height either join an * existing boxa or are set aside for pass 2) * Return: boxaa (2d sorted version of boxa), or null on error * * Notes: * (1) The final result is a sort where the 'fast scan' direction is * left to right, and the 'slow scan' direction is from top * to bottom. Each boxa in the boxaa represents a sorted set * of boxes from left to right. * (2) Two passes are used to aggregate the boxas, which can corresond * to characters or words in a line of text. In pass 1, only * taller components, which correspond to xheight or larger, * are permitted to start a new boxa, whereas in pass 2, * the remaining vertically-challenged components are allowed * to join an existing boxa or start a new one. * (3) If delta1 < 0, the first pass allows aggregation when * boxes in the same boxa do not overlap vertically. * The distance by which they can miss and still be aggregated * is the absolute value |delta1|. Similar for delta2 on * the second pass. * (4) On the first pass, any component of height less than minh1 * cannot start a new boxa; it's put aside for later insertion. * (5) On the second pass, any small component that doesn't align * with an existing boxa can start a new one. * (6) This can be used to identify lines of text from * character or word bounding boxes. */ BOXAA * boxaSort2d(BOXA *boxas, NUMAA **pnaad, l_int32 delta1, l_int32 delta2, l_int32 minh1) { l_int32 i, index, h, nt, ne, n, m, ival; BOX *box; BOXA *boxa, *boxae, *boxan, *boxat1, *boxat2, *boxav, *boxavs; BOXAA *baa, *baad; NUMA *naindex, *nae, *nan, *nah, *nav, *nat1, *nat2, *nad; NUMAA *naa, *naad; PROCNAME("boxaSort2d"); if (pnaad) *pnaad = NULL; if (!boxas) return (BOXAA *)ERROR_PTR("boxas not defined", procName, NULL); /* Sort from left to right */ if ((boxa = boxaSort(boxas, L_SORT_BY_X, L_SORT_INCREASING, &naindex)) == NULL) return (BOXAA *)ERROR_PTR("boxa not made", procName, NULL); /* First pass: assign taller boxes to boxa by row */ nt = boxaGetCount(boxa); baa = boxaaCreate(0); naa = numaaCreate(0); boxae = boxaCreate(0); /* save small height boxes here */ nae = numaCreate(0); /* keep track of small height boxes */ for (i = 0; i < nt; i++) { box = boxaGetBox(boxa, i, L_CLONE); boxGetGeometry(box, NULL, NULL, NULL, &h); if (h < minh1) { /* save for 2nd pass */ boxaAddBox(boxae, box, L_INSERT); numaAddNumber(nae, i); } else { n = boxaaGetCount(baa); boxaaAlignBox(baa, box, delta1, &index); if (index < n) { /* append to an existing boxa */ boxaaAddBox(baa, index, box, L_INSERT); } else { /* doesn't align, need new boxa */ boxan = boxaCreate(0); boxaAddBox(boxan, box, L_INSERT); boxaaAddBoxa(baa, boxan, L_INSERT); nan = numaCreate(0); numaaAddNuma(naa, nan, L_INSERT); } numaGetIValue(naindex, i, &ival); numaaAddNumber(naa, index, ival); } } boxaDestroy(&boxa); numaDestroy(&naindex); /* Second pass: feed in small height boxes; * TODO: this correctly, using local y position! */ ne = boxaGetCount(boxae); for (i = 0; i < ne; i++) { box = boxaGetBox(boxae, i, L_CLONE); n = boxaaGetCount(baa); boxaaAlignBox(baa, box, delta2, &index); if (index < n) { /* append to an existing boxa */ boxaaAddBox(baa, index, box, L_INSERT); } else { /* doesn't align, need new boxa */ boxan = boxaCreate(0); boxaAddBox(boxan, box, L_INSERT); boxaaAddBoxa(baa, boxan, L_INSERT); nan = numaCreate(0); numaaAddNuma(naa, nan, L_INSERT); } numaGetIValue(nae, i, &ival); /* location in original boxas */ numaaAddNumber(naa, index, ival); } /* Sort each boxa in the boxaa */ m = boxaaGetCount(baa); for (i = 0; i < m; i++) { boxat1 = boxaaGetBoxa(baa, i, L_CLONE); boxat2 = boxaSort(boxat1, L_SORT_BY_X, L_SORT_INCREASING, &nah); boxaaReplaceBoxa(baa, i, boxat2); nat1 = numaaGetNuma(naa, i, L_CLONE); nat2 = numaSortByIndex(nat1, nah); numaaReplaceNuma(naa, i, nat2); boxaDestroy(&boxat1); numaDestroy(&nat1); numaDestroy(&nah); } /* Sort boxa vertically within boxaa, using the first box * in each boxa. */ m = boxaaGetCount(baa); boxav = boxaCreate(m); /* holds first box in each boxa in baa */ naad = numaaCreate(m); if (pnaad) *pnaad = naad; baad = boxaaCreate(m); for (i = 0; i < m; i++) { boxat1 = boxaaGetBoxa(baa, i, L_CLONE); box = boxaGetBox(boxat1, 0, L_CLONE); boxaAddBox(boxav, box, L_INSERT); boxaDestroy(&boxat1); } boxavs = boxaSort(boxav, L_SORT_BY_Y, L_SORT_INCREASING, &nav); for (i = 0; i < m; i++) { numaGetIValue(nav, i, &index); boxa = boxaaGetBoxa(baa, index, L_CLONE); boxaaAddBoxa(baad, boxa, L_INSERT); nad = numaaGetNuma(naa, index, L_CLONE); numaaAddNuma(naad, nad, L_INSERT); } /* fprintf(stderr, "box count = %d, numaa count = %d\n", nt, numaaGetNumberCount(naad)); */ boxaaDestroy(&baa); boxaDestroy(&boxav); boxaDestroy(&boxavs); boxaDestroy(&boxae); numaDestroy(&nav); numaDestroy(&nae); numaaDestroy(&naa); if (!pnaad) numaaDestroy(&naad); return baad; }