/*! * dewarpaShowArrays() * * Input: dewa * scalefact (on contour images; typ. 0.5) * first (first page model to render) * last (last page model to render; use 0 to go to end) * fontdir (for text bitmap fonts) * Return: 0 if OK, 1 on error * * Notes: * (1) Generates a pdf of contour plots of the disparity arrays. * (2) This only shows actual models; not ref models */ l_int32 dewarpaShowArrays(L_DEWARPA *dewa, l_float32 scalefact, l_int32 first, l_int32 last, const char *fontdir) { char buf[256]; char *pathname; l_int32 i, svd, shd; L_BMF *bmf; L_DEWARP *dew; PIX *pixv, *pixvs, *pixh, *pixhs, *pixt, *pixd; PIXA *pixa; PROCNAME("dewarpaShowArrays"); if (!dewa) return ERROR_INT("dew not defined", procName, 1); if (first < 0 || first > dewa->maxpage) return ERROR_INT("first out of bounds", procName, 1); if (last <= 0 || last > dewa->maxpage) last = dewa->maxpage; if (last < first) return ERROR_INT("last < first", procName, 1); lept_rmdir("lept"); lept_mkdir("lept"); if ((bmf = bmfCreate(fontdir, 8)) == NULL) L_ERROR("bmf not made; page info not displayed", procName); fprintf(stderr, "Generating contour plots\n"); for (i = first; i <= last; i++) { if (i && ((i % 10) == 0)) fprintf(stderr, " .. %d", i); dew = dewarpaGetDewarp(dewa, i); if (!dew) continue; if (dew->hasref == 1) continue; svd = shd = 0; if (dew->sampvdispar) svd = 1; if (dew->samphdispar) shd = 1; if (!svd) { L_ERROR("sampvdispar not made for page %d!\n", procName, i); continue; } /* Generate contour plots at reduced resolution */ dewarpPopulateFullRes(dew, NULL, 0, 0); pixv = fpixRenderContours(dew->fullvdispar, 3.0, 0.15); pixvs = pixScaleBySampling(pixv, scalefact, scalefact); pixDestroy(&pixv); if (shd) { pixh = fpixRenderContours(dew->fullhdispar, 3.0, 0.15); pixhs = pixScaleBySampling(pixh, scalefact, scalefact); pixDestroy(&pixh); } dewarpMinimize(dew); /* Save side-by-side */ pixa = pixaCreate(2); pixaAddPix(pixa, pixvs, L_INSERT); if (shd) pixaAddPix(pixa, pixhs, L_INSERT); pixt = pixaDisplayTiledInRows(pixa, 32, 1500, 1.0, 0, 30, 2); snprintf(buf, sizeof(buf), "Page %d", i); pixd = pixAddSingleTextblock(pixt, bmf, buf, 0x0000ff00, L_ADD_BELOW, NULL); snprintf(buf, sizeof(buf), "arrays_%04d.png", i); pathname = genPathname("/tmp/lept", buf); pixWrite(pathname, pixd, IFF_PNG); pixaDestroy(&pixa); pixDestroy(&pixt); pixDestroy(&pixd); FREE(pathname); } bmfDestroy(&bmf); fprintf(stderr, "\n"); fprintf(stderr, "Generating pdf of contour plots\n"); convertFilesToPdf("/tmp/lept", "arrays_", 90, 1.0, L_FLATE_ENCODE, 0, "Disparity arrays", "/tmp/lept/disparity_arrays.pdf"); fprintf(stderr, "Output written to: /tmp/lept/disparity_arrays.pdf\n"); return 0; }
/*! * dewarpShowResults() * * Input: dewa * sarray (of indexed input images) * boxa (crop boxes for input images; can be null) * firstpage, lastpage * fontdir (for text bitmap fonts) * pdfout (filename) * Return: 0 if OK, 1 on error * * Notes: * (1) This generates a pdf of image pairs (before, after) for * the designated set of input pages. * (2) If the boxa exists, its elements are aligned with numbers * in the filenames in @sa. It is used to crop the input images. * It is assumed that the dewa was generated from the cropped * images. No undercropping is applied before rendering. */ l_int32 dewarpShowResults(L_DEWARPA *dewa, SARRAY *sa, BOXA *boxa, l_int32 firstpage, l_int32 lastpage, const char *fontdir, const char *pdfout) { char bufstr[256]; char *outpath; l_int32 i, modelpage; L_BMF *bmf; BOX *box; L_DEWARP *dew; PIX *pixs, *pixc, *pixd, *pixt1, *pixt2; PIXA *pixa; PROCNAME("dewarpShowResults"); if (!dewa) return ERROR_INT("dewa not defined", procName, 1); if (!sa) return ERROR_INT("sa not defined", procName, 1); if (!pdfout) return ERROR_INT("pdfout not defined", procName, 1); if (firstpage > lastpage) return ERROR_INT("invalid first/last page numbers", procName, 1); lept_rmdir("dewarp_pdfout"); lept_mkdir("dewarp_pdfout"); if ((bmf = bmfCreate(fontdir, 6)) == NULL) L_ERROR("bmf not made; page info not displayed", procName); fprintf(stderr, "Dewarping and generating s/by/s view\n"); for (i = firstpage; i <= lastpage; i++) { if (i && (i % 10 == 0)) fprintf(stderr, ".. %d ", i); pixs = pixReadIndexed(sa, i); if (boxa) { box = boxaGetBox(boxa, i, L_CLONE); pixc = pixClipRectangle(pixs, box, NULL); boxDestroy(&box); } else pixc = pixClone(pixs); dew = dewarpaGetDewarp(dewa, i); pixd = NULL; if (dew) { dewarpaApplyDisparity(dewa, dew->pageno, pixc, GRAYIN_VALUE, 0, 0, &pixd, NULL); dewarpMinimize(dew); } pixa = pixaCreate(2); pixaAddPix(pixa, pixc, L_INSERT); if (pixd) pixaAddPix(pixa, pixd, L_INSERT); pixt1 = pixaDisplayTiledAndScaled(pixa, 32, 500, 2, 0, 35, 2); if (dew) { modelpage = (dew->hasref) ? dew->refpage : dew->pageno; snprintf(bufstr, sizeof(bufstr), "Page %d; using %d\n", i, modelpage); } else snprintf(bufstr, sizeof(bufstr), "Page %d; no dewarp\n", i); pixt2 = pixAddSingleTextblock(pixt1, bmf, bufstr, 0x0000ff00, L_ADD_BELOW, 0); snprintf(bufstr, sizeof(bufstr), "/tmp/dewarp_pdfout/%05d", i); pixWrite(bufstr, pixt2, IFF_JFIF_JPEG); pixaDestroy(&pixa); pixDestroy(&pixs); pixDestroy(&pixt1); pixDestroy(&pixt2); } fprintf(stderr, "\n"); fprintf(stderr, "Generating pdf of result\n"); convertFilesToPdf("/tmp/dewarp_pdfout", NULL, 100, 1.0, L_JPEG_ENCODE, 0, "Dewarp sequence", pdfout); outpath = genPathname(pdfout, NULL); fprintf(stderr, "Output written to: %s\n", outpath); FREE(outpath); bmfDestroy(&bmf); return 0; }
/*! * dewarpaApplyDisparity() * * Input: dewa * pageno (of page model to be used; may be a ref model) * pixs (image to be modified; can be 1, 8 or 32 bpp) * grayin (gray value, from 0 to 255, for pixels brought in; * use -1 to use pixels on the boundary of pixs) * x, y (origin for generation of disparity arrays) * &pixd (<return> disparity corrected image) * debugfile (use null to skip writing this) * Return: 0 if OK, 1 on error (no models or ref models available) * * Notes: * (1) This applies the disparity arrays to the specified image. * (2) Specify gray color for pixels brought in from the outside: * 0 is black, 255 is white. Use -1 to select pixels from the * boundary of the source image. * (3) If the models and ref models have not been validated, this * will do so by calling dewarpaInsertRefModels(). * (4) This works with both stripped and full resolution page models. * If the full res disparity array(s) are missing, they are remade. * (5) The caller must handle errors that are returned because there * are no valid models or ref models for the page -- typically * by using the input pixs. * (6) If there is no model for @pageno, this will use the model for * 'refpage' and put the result in the dew for @pageno. * (7) This populates the full resolution disparity arrays if * necessary. If x and/or y are positive, they are used, * in conjunction with pixs, to determine the required * slope-based extension of the full resolution disparity * arrays in each direction. When (x,y) == (0,0), all * extension is to the right and down. Nonzero values of (x,y) * are useful for dewarping when pixs is deliberately undercropped. * (8) Important: when applying disparity to a number of images, * after calling this function and saving the resulting pixd, * you should call dewarpMinimize(dew) on the dew for @pageno. * This will remove pixs and pixd (or their clones) stored in dew, * as well as the full resolution disparity arrays. Together, * these hold approximately 16 bytes for each pixel in pixs. */ l_int32 dewarpaApplyDisparity(L_DEWARPA *dewa, l_int32 pageno, PIX *pixs, l_int32 grayin, l_int32 x, l_int32 y, PIX **ppixd, const char *debugfile) { L_DEWARP *dew1, *dew; PIX *pixv, *pixh; PROCNAME("dewarpaApplyDisparity"); /* Initialize the output with the input, so we'll have that * in case we can't apply the page model. */ if (!ppixd) return ERROR_INT("&pixd not defined", procName, 1); *ppixd = pixClone(pixs); if (grayin > 255) { L_WARNING("invalid grayin = %d; clipping at 255\n", procName, grayin); grayin = 255; } /* Find the appropriate dew to use and fully populate its array(s) */ if (dewarpaApplyInit(dewa, pageno, pixs, x, y, &dew, debugfile)) return ERROR_INT("no model available", procName, 1); /* Correct for vertical disparity and save the result */ if ((pixv = pixApplyVertDisparity(dew, pixs, grayin)) == NULL) { dewarpMinimize(dew); return ERROR_INT("pixv not made", procName, 1); } pixDestroy(ppixd); *ppixd = pixv; if (debugfile) { pixDisplayWithTitle(pixv, 300, 0, "pixv", 1); lept_rmdir("lept/dewapply"); /* remove previous images */ lept_mkdir("lept/dewapply"); pixWrite("/tmp/lept/dewapply/001.png", pixs, IFF_PNG); pixWrite("/tmp/lept/dewapply/002.png", pixv, IFF_PNG); } /* Optionally, correct for horizontal disparity */ if (dewa->useboth && dew->hsuccess) { if (dew->hvalid == FALSE) { L_INFO("invalid horiz model for page %d\n", procName, pageno); } else { if ((pixh = pixApplyHorizDisparity(dew, pixv, grayin)) != NULL) { pixDestroy(ppixd); *ppixd = pixh; if (debugfile) { pixDisplayWithTitle(pixh, 600, 0, "pixh", 1); pixWrite("/tmp/lept/dewapply/003.png", pixh, IFF_PNG); } } else { L_ERROR("horiz disparity failed on page %d\n", procName, pageno); } } } if (debugfile) { dew1 = dewarpaGetDewarp(dewa, pageno); dewarpDebug(dew1, "lept/dewapply", 0); convertFilesToPdf("/tmp/lept/dewapply", NULL, 135, 1.0, 0, 0, "Dewarp Apply Disparity", debugfile); fprintf(stderr, "pdf file: %s\n", debugfile); } /* Get rid of the large full res disparity arrays */ dewarpMinimize(dew); return 0; }
/*! * dewarpaApplyDisparityBoxa() * * Input: dewa * pageno (of page model to be used; may be a ref model) * pixs (initial pix reference; for alignment and debugging) * boxas (boxa to be mapped) * mapdir (1 if mapping forward from original to dewarped; * 0 if backward) * x, y (origin for generation of disparity arrays with * respect to the source region) * &boxad (<return> disparity corrected boxa) * debugfile (use null to skip writing this) * Return: 0 if OK, 1 on error (no models or ref models available) * * Notes: * (1) This applies the disparity arrays in one of two mapping directions * to the specified boxa. It can be used in the backward direction * to locate a box in the original coordinates that would have * been dewarped to to the specified image. * (2) If there is no model for @pageno, this will use the model for * 'refpage' and put the result in the dew for @pageno. * (3) This works with both stripped and full resolution page models. * If the full res disparity array(s) are missing, they are remade. * (4) If an error occurs, a copy of the input boxa is returned. */ l_int32 dewarpaApplyDisparityBoxa(L_DEWARPA *dewa, l_int32 pageno, PIX *pixs, BOXA *boxas, l_int32 mapdir, l_int32 x, l_int32 y, BOXA **pboxad, const char *debugfile) { l_int32 debug_out; L_DEWARP *dew1, *dew; BOXA *boxav, *boxah; PIX *pixv, *pixh; PROCNAME("dewarpaApplyDisparityBoxa"); /* Initialize the output with the input, so we'll have that * in case we can't apply the page model. */ if (!pboxad) return ERROR_INT("&boxad not defined", procName, 1); *pboxad = boxaCopy(boxas, L_CLONE); /* Find the appropriate dew to use and fully populate its array(s) */ if (dewarpaApplyInit(dewa, pageno, pixs, x, y, &dew, debugfile)) return ERROR_INT("no model available", procName, 1); /* Correct for vertical disparity and save the result */ if ((boxav = boxaApplyDisparity(dew, boxas, L_VERT, mapdir)) == NULL) { dewarpMinimize(dew); return ERROR_INT("boxa1 not made", procName, 1); } boxaDestroy(pboxad); *pboxad = boxav; pixv = NULL; pixh = NULL; if (debugfile && mapdir != 1) L_INFO("Reverse map direction; no debug output\n", procName); debug_out = debugfile && (mapdir == 1); if (debug_out) { PIX *pix1; lept_rmdir("lept/dewboxa"); /* remove previous images */ lept_mkdir("lept/dewboxa"); pix1 = pixConvertTo32(pixs); pixRenderBoxaArb(pix1, boxas, 2, 255, 0, 0); pixWrite("/tmp/lept/dewboxa/01.png", pix1, IFF_PNG); pixDestroy(&pix1); pixv = pixApplyVertDisparity(dew, pixs, 255); pix1 = pixConvertTo32(pixv); pixRenderBoxaArb(pix1, boxav, 2, 0, 255, 0); pixWrite("/tmp/lept/dewboxa/02.png", pix1, IFF_PNG); pixDestroy(&pix1); } /* Optionally, correct for horizontal disparity */ if (dewa->useboth && dew->hsuccess) { if (dew->hvalid == FALSE) { L_INFO("invalid horiz model for page %d\n", procName, pageno); } else { boxah = boxaApplyDisparity(dew, boxav, L_HORIZ, mapdir); if (!boxah) { L_ERROR("horiz disparity fails on page %d\n", procName, pageno); } else { boxaDestroy(pboxad); *pboxad = boxah; if (debug_out) { PIX *pix1; pixh = pixApplyHorizDisparity(dew, pixv, 255); pix1 = pixConvertTo32(pixh); pixRenderBoxaArb(pix1, boxah, 2, 0, 0, 255); pixWrite("/tmp/lept/dewboxa/03.png", pix1, IFF_PNG); pixDestroy(&pixh); pixDestroy(&pix1); } } } } if (debug_out) { pixDestroy(&pixv); dew1 = dewarpaGetDewarp(dewa, pageno); dewarpDebug(dew1, "lept/dewapply", 0); convertFilesToPdf("/tmp/lept/dewboxa", NULL, 135, 1.0, 0, 0, "Dewarp Apply Disparity Boxa", debugfile); fprintf(stderr, "Dewarp Apply Disparity Boxa pdf file: %s\n", debugfile); } /* Get rid of the large full res disparity arrays */ dewarpMinimize(dew); return 0; }
l_int32 main(int argc, char **argv) { l_int32 i, n; l_float32 a, b, c; L_DEWARP *dew, *dew2; DPIX *dpix1, *dpix2, *dpix3; FPIX *fpix1, *fpix2, *fpix3; NUMA *nax, *nafit; PIX *pixs, *pixn, *pixg, *pixb, *pixt1, *pixt2; PIX *pixs2, *pixn2, *pixg2, *pixb2; PTA *pta, *ptad; PTAA *ptaa1, *ptaa2; L_REGPARAMS *rp; if (regTestSetup(argc, argv, &rp)) return 1; pixs = pixRead("1555-7.jpg"); /* Normalize for varying background and binarize */ pixn = pixBackgroundNormSimple(pixs, NULL, NULL); pixg = pixConvertRGBToGray(pixn, 0.5, 0.3, 0.2); pixb = pixThresholdToBinary(pixg, 130); pixDestroy(&pixn); pixDestroy(&pixg); regTestWritePixAndCheck(rp, pixb, IFF_PNG); /* 0 */ pixDisplayWithTitle(pixb, 0, 0, "binarized input", rp->display); /* Get the textline centers */ ptaa1 = pixGetTextlineCenters(pixb, 0); pixt1 = pixCreateTemplate(pixs); pixt2 = pixDisplayPtaa(pixt1, ptaa1); regTestWritePixAndCheck(rp, pixt2, IFF_PNG); /* 1 */ pixDisplayWithTitle(pixt2, 0, 500, "textline centers", rp->display); pixDestroy(&pixt1); /* Remove short lines */ ptaa2 = ptaaRemoveShortLines(pixb, ptaa1, 0.8, 0); /* Fit to quadratic */ n = ptaaGetCount(ptaa2); for (i = 0; i < n; i++) { pta = ptaaGetPta(ptaa2, i, L_CLONE); ptaGetArrays(pta, &nax, NULL); ptaGetQuadraticLSF(pta, &a, &b, &c, &nafit); ptad = ptaCreateFromNuma(nax, nafit); pixDisplayPta(pixt2, pixt2, ptad); ptaDestroy(&pta); ptaDestroy(&ptad); numaDestroy(&nax); numaDestroy(&nafit); } regTestWritePixAndCheck(rp, pixt2, IFF_PNG); /* 2 */ pixDisplayWithTitle(pixt2, 300, 500, "fitted lines superimposed", rp->display); ptaaDestroy(&ptaa1); ptaaDestroy(&ptaa2); pixDestroy(&pixt2); /* Run with only vertical disparity correction */ if ((dew = dewarpCreate(pixb, 7, 30, 15, 0)) == NULL) return ERROR_INT("\n\n\n FAILURE !!! \n\n\n", rp->testname, 1); dewarpBuildModel(dew, 0); dewarpApplyDisparity(dew, pixb, 0); regTestWritePixAndCheck(rp, dew->pixd, IFF_PNG); /* 3 */ pixDisplayWithTitle(dew->pixd, 400, 0, "fixed for vert disparity", rp->display); dewarpDestroy(&dew); /* Run with both vertical and horizontal disparity correction */ if ((dew = dewarpCreate(pixb, 7, 30, 15, 1)) == NULL) return ERROR_INT("\n\n\n FAILURE !!! \n\n\n", rp->testname, 1); dewarpBuildModel(dew, 0); dewarpApplyDisparity(dew, pixb, 0); regTestWritePixAndCheck(rp, dew->pixd, IFF_PNG); /* 4 */ pixDisplayWithTitle(dew->pixd, 800, 0, "fixed for both disparities", rp->display); /* Read another image, normalize background and binarize */ pixs2 = pixRead("1555-3.jpg"); pixn2 = pixBackgroundNormSimple(pixs2, NULL, NULL); pixg2 = pixConvertRGBToGray(pixn2, 0.5, 0.3, 0.2); pixb2 = pixThresholdToBinary(pixg2, 130); pixDestroy(&pixn2); pixDestroy(&pixg2); regTestWritePixAndCheck(rp, pixb, IFF_PNG); /* 5 */ pixDisplayWithTitle(pixb, 0, 400, "binarized input (2)", rp->display); /* Minimize and re-apply previous disparity to this image */ dewarpMinimize(dew); dewarpApplyDisparity(dew, pixb2, 0); regTestWritePixAndCheck(rp, dew->pixd, IFF_PNG); /* 6 */ pixDisplayWithTitle(dew->pixd, 400, 400, "fixed (2) for both disparities", rp->display); /* Write and read back minimized dewarp struct */ dewarpWrite("/tmp/dewarp.7.dew", dew); regTestCheckFile(rp, "/tmp/dewarp.7.dew"); /* 7 */ dew2 = dewarpRead("/tmp/dewarp.7.dew"); dewarpWrite("/tmp/dewarp.8.dew", dew2); regTestCheckFile(rp, "/tmp/dewarp.8.dew"); /* 8 */ regTestCompareFiles(rp, 7, 8); /* 9 */ /* Apply dew2 to pixb2 */ dewarpApplyDisparity(dew2, pixb2, 0); regTestWritePixAndCheck(rp, dew2->pixd, IFF_PNG); /* 10 */ pixDisplayWithTitle(dew->pixd, 800, 400, "fixed (3) for both disparities", rp->display); /* Minimize, repopulate disparity arrays, and apply again */ dewarpMinimize(dew2); dewarpApplyDisparity(dew2, pixb2, 0); regTestWritePixAndCheck(rp, dew2->pixd, IFF_PNG); /* 11 */ regTestCompareFiles(rp, 10, 11); /* 12 */ pixDisplayWithTitle(dew->pixd, 900, 400, "fixed (4) for both disparities", rp->display); /* Test a few of the fpix functions */ fpix1 = fpixClone(dew->sampvdispar); fpixWrite("/tmp/sampv.13.fpix", fpix1); regTestCheckFile(rp, "/tmp/sampv.13.fpix"); /* 13 */ fpix2 = fpixRead("/tmp/sampv.13.fpix"); fpixWrite("/tmp/sampv.14.fpix", fpix2); regTestCheckFile(rp, "/tmp/sampv.14.fpix"); /* 14 */ regTestCompareFiles(rp, 13, 14); /* 15 */ fpix3 = fpixScaleByInteger(fpix2, 30); pixt1 = fpixRenderContours(fpix3, -2., 2.0, 0.2); regTestWritePixAndCheck(rp, pixt1, IFF_PNG); /* 16 */ pixDisplayWithTitle(pixt1, 0, 800, "v. disparity contours", rp->display); fpixDestroy(&fpix1); fpixDestroy(&fpix2); fpixDestroy(&fpix3); pixDestroy(&pixt1); /* Test a few of the dpix functions */ dpix1 = fpixConvertToDPix(dew->sampvdispar); dpixWrite("/tmp/sampv.17.dpix", dpix1); regTestCheckFile(rp, "/tmp/sampv.17.dpix"); /* 17 */ dpix2 = dpixRead("/tmp/sampv.17.dpix"); dpixWrite("/tmp/sampv.18.dpix", dpix2); regTestCheckFile(rp, "/tmp/sampv.18.dpix"); /* 18 */ regTestCompareFiles(rp, 17, 18); /* 19 */ dpix3 = dpixScaleByInteger(dpix2, 30); fpix3 = dpixConvertToFPix(dpix3); pixt1 = fpixRenderContours(fpix3, -2., 2.0, 0.2); regTestWritePixAndCheck(rp, pixt1, IFF_PNG); /* 20 */ pixDisplayWithTitle(pixt1, 400, 800, "v. disparity contours", rp->display); regTestCompareFiles(rp, 16, 20); /* 21 */ dpixDestroy(&dpix1); dpixDestroy(&dpix2); dpixDestroy(&dpix3); fpixDestroy(&fpix3); pixDestroy(&pixt1); dewarpDestroy(&dew); dewarpDestroy(&dew2); pixDestroy(&pixs); pixDestroy(&pixb); pixDestroy(&pixs2); pixDestroy(&pixb2); regTestCleanup(rp); return 0; }