l_int32 main(int argc, char **argv) { l_int32 pageno; L_DEWARP *dew1; L_DEWARPA *dewa; PIX *pixs, *pixn, *pixg, *pixb; static char mainName[] = "dewarptest2"; if (argc != 1 && argc != 3) return ERROR_INT("Syntax: dewarptest2 [image pageno]", mainName, 1); if (argc == 1) { pixs = pixRead("cat-35.jpg"); pageno = 35; } else { pixs = pixRead(argv[1]); pageno = atoi(argv[2]); } if (!pixs) return ERROR_INT("image not read", mainName, 1); dewa = dewarpaCreate(40, 30, 1, 6, 50); #if NORMALIZE /* 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); #else /* Don't normalize; just threshold and clean edges */ pixg = pixConvertTo8(pixs, 0); pixb = pixThresholdToBinary(pixg, 100); pixSetOrClearBorder(pixb, 30, 30, 40, 40, PIX_CLR); #endif /* Run the basic functions */ dew1 = dewarpCreate(pixb, pageno); dewarpaInsertDewarp(dewa, dew1); dewarpBuildModel(dew1, "/tmp/dewarp_model1.pdf"); dewarpaApplyDisparity(dewa, pageno, pixg, "/tmp/dewarp_apply1.pdf"); dewarpaDestroy(&dewa); pixDestroy(&pixs); pixDestroy(&pixg); pixDestroy(&pixb); return 0; }
/*! * dewarpSinglePageRun() * * Input: pixs (any depth) * pixb (1 bpp) * dewa (initialized) * &pixd (<return> dewarped result) * debug (1 for debugging output, 0 otherwise) * Return: 0 if OK, 1 on error (list of page numbers), or null on error * * Notes: * (1) Dewarps pixs and returns the result in &pixd. * (2) The model parameters must be set before calling this. * (3) If a model cannot be built, this returns a copy of pixs in &pixd. */ l_int32 dewarpSinglePageRun(PIX *pixs, PIX *pixb, L_DEWARPA *dewa, PIX **ppixd, l_int32 debug) { const char *debugfile; l_int32 vsuccess, ret; L_DEWARP *dew; PROCNAME("dewarpSinglePageRun"); if (!ppixd) return ERROR_INT("&pixd not defined", procName, 1); *ppixd = NULL; if (!pixs) return ERROR_INT("pixs not defined", procName, 1); if (!pixb) return ERROR_INT("pixs not defined", procName, 1); if (!dewa) return ERROR_INT("dewa not defined", procName, 1); /* Generate the page model */ lept_mkdir("lept"); dew = dewarpCreate(pixb, 0); dewarpaInsertDewarp(dewa, dew); debugfile = (debug) ? "/tmp/lept/singlepage_model.pdf" : NULL; dewarpBuildPageModel(dew, debugfile); dewarpaModelStatus(dewa, 0, &vsuccess, NULL); if (vsuccess == 0) { L_ERROR("failure to build model for vertical disparity\n", procName); *ppixd = pixCopy(NULL, pixs); return 0; } /* Apply the page model */ debugfile = (debug) ? "/tmp/lept/singlepage_apply.pdf" : NULL; ret = dewarpaApplyDisparity(dewa, 0, pixs, 255, 0, 0, ppixd, debugfile); if (ret) L_ERROR("invalid model; failure to apply disparity\n", procName); 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; }
l_int32 main(int argc, char **argv) { l_int32 method, pageno; L_DEWARP *dew1; L_DEWARPA *dewa; PIX *pixs, *pixn, *pixg, *pixb, *pixd; static char mainName[] = "dewarptest2"; if (argc != 2 && argc != 4) return ERROR_INT("Syntax: dewarptest2 method [image pageno]", mainName, 1); if (argc == 2) { pixs = pixRead("cat-35.jpg"); pageno = 35; } else { pixs = pixRead(argv[2]); pageno = atoi(argv[3]); } if (!pixs) return ERROR_INT("image not read", mainName, 1); method = atoi(argv[1]); lept_mkdir("lept"); if (method == 1) { /* Use single page dewarp function */ dewarpSinglePage(pixs, 1, 100, 1, &pixd, NULL, 1); pixDisplay(pixd, 100, 100); } else { /* Break down into multiple steps; require min of only 6 lines */ dewa = dewarpaCreate(40, 30, 1, 6, 50); dewarpaUseBothArrays(dewa, 1); #if NORMALIZE /* 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); #else /* Don't normalize; just threshold and clean edges */ pixg = pixConvertTo8(pixs, 0); pixb = pixThresholdToBinary(pixg, 100); pixSetOrClearBorder(pixb, 30, 30, 40, 40, PIX_CLR); #endif /* Run the basic functions */ dew1 = dewarpCreate(pixb, pageno); dewarpaInsertDewarp(dewa, dew1); dewarpBuildPageModel(dew1, "/tmp/lept/test2_model.pdf"); dewarpaApplyDisparity(dewa, pageno, pixg, -1, 0, 0, &pixd, "/tmp/lept/test2_apply.pdf"); dewarpaInfo(stderr, dewa); dewarpaDestroy(&dewa); pixDestroy(&pixg); pixDestroy(&pixb); } pixDestroy(&pixs); pixDestroy(&pixd); return 0; }
l_int32 main(int argc, char **argv) { char buf[64]; BOXA *boxa1, *boxa2, *boxa3, *boxa4; L_DEWARP *dew; L_DEWARPA *dewa; PIX *pixs, *pixn, *pixg, *pixb, *pix2, *pix3, *pix4, *pix5, *pix6; lept_mkdir("lept"); snprintf(buf, sizeof(buf), "cat.%03d.jpg", pageno); pixs = pixRead(buf); dewa = dewarpaCreate(40, 30, 1, 15, 10); dewarpaUseBothArrays(dewa, 1); /* Normalize for varying background and binarize */ pixn = pixBackgroundNormSimple(pixs, NULL, NULL); pixg = pixConvertRGBToGray(pixn, 0.5, 0.3, 0.2); pixb = pixThresholdToBinary(pixg, 130); pixDisplay(pixb, 0, 100); /* Build the model */ dew = dewarpCreate(pixb, pageno); dewarpaInsertDewarp(dewa, dew); if (build_output) { snprintf(buf, sizeof(buf), "/tmp/lept/dewarp_build_%d.pdf", pageno); dewarpBuildPageModel(dew, buf); } else { dewarpBuildPageModel(dew, NULL); } /* Apply the model */ dewarpPopulateFullRes(dew, pixg, 0, 0); if (apply_output) { snprintf(buf, sizeof(buf), "/tmp/lept/dewarp_apply_%d.pdf", pageno); dewarpaApplyDisparity(dewa, pageno, pixb, 200, 0, 0, &pix2, buf); } else { dewarpaApplyDisparity(dewa, pageno, pixb, 200, 0, 0, &pix2, NULL); } pixDisplay(pix2, 200, 100); /* Reverse direction: get the word boxes for the dewarped pix ... */ pixGetWordBoxesInTextlines(pix2, 5, 5, 500, 100, &boxa1, NULL); pix3 = pixConvertTo32(pix2); pixRenderBoxaArb(pix3, boxa1, 2, 255, 0, 0); pixDisplay(pix3, 400, 100); /* ... and map to the word boxes for the input image */ if (map_output) { snprintf(buf, sizeof(buf), "/tmp/lept/dewarp_map1_%d.pdf", pageno); dewarpaApplyDisparityBoxa(dewa, pageno, pix2, boxa1, 0, 0, 0, &boxa2, buf); } else { dewarpaApplyDisparityBoxa(dewa, pageno, pix2, boxa1, 0, 0, 0, &boxa2, NULL); } pix4 = pixConvertTo32(pixb); pixRenderBoxaArb(pix4, boxa2, 2, 0, 255, 0); pixDisplay(pix4, 600, 100); /* Forward direction: get the word boxes for the input pix ... */ pixGetWordBoxesInTextlines(pixb, 5, 5, 500, 100, &boxa3, NULL); pix5 = pixConvertTo32(pixb); pixRenderBoxaArb(pix5, boxa3, 2, 255, 0, 0); pixDisplay(pix5, 800, 100); /* ... and map to the word boxes for the dewarped image */ if (map_output) { snprintf(buf, sizeof(buf), "/tmp/lept/dewarp_map2_%d.pdf", pageno); dewarpaApplyDisparityBoxa(dewa, pageno, pixb, boxa3, 1, 0, 0, &boxa4, buf); } else { dewarpaApplyDisparityBoxa(dewa, pageno, pixb, boxa3, 1, 0, 0, &boxa4, NULL); } pix6 = pixConvertTo32(pix2); pixRenderBoxaArb(pix6, boxa4, 2, 0, 255, 0); pixDisplay(pix6, 1000, 100); dewarpaDestroy(&dewa); pixDestroy(&pixs); pixDestroy(&pixn); pixDestroy(&pixg); pixDestroy(&pixb); pixDestroy(&pix2); pixDestroy(&pix3); pixDestroy(&pix4); pixDestroy(&pix5); pixDestroy(&pix6); boxaDestroy(&boxa1); boxaDestroy(&boxa2); boxaDestroy(&boxa3); boxaDestroy(&boxa4); return 0; }
l_int32 main(int argc, char **argv) { l_int32 i, n, ignore; l_float32 a, b, c, d, e; L_DEWARP *dew1, *dew2; L_DEWARPA *dewa; FILE *fp; FPIX *fpix; NUMA *nax, *nay, *nafit; PIX *pixs, *pixn, *pixg, *pixb, *pixt1, *pixt2, *pixt3; PIX *pixs2, *pixn2, *pixg2, *pixb2; PTA *pta, *ptad; PTAA *ptaa1, *ptaa2; /* pixs = pixRead("1555-7.jpg"); */ pixs = pixRead("cat-35.jpg"); /* pixs = pixRead("cat-10.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); /* Run the basic functions */ dewa = dewarpaCreate(2, 30, 1, 10, 30); dew1 = dewarpCreate(pixb, 10); dewarpaInsertDewarp(dewa, dew1); dewarpBuildModel(dew1, "/tmp/dewarp_model1.pdf"); dewarpaApplyDisparity(dewa, 10, pixg, "/tmp/dewarp_apply1.pdf"); /* Write out some of the files to be imaged */ lept_rmdir("dewtest"); lept_mkdir("dewtest"); pixWrite("/tmp/dewtest/001.jpg", pixs, IFF_JFIF_JPEG); pixWrite("/tmp/dewtest/002.jpg", pixn, IFF_JFIF_JPEG); pixWrite("/tmp/dewtest/003.jpg", pixg, IFF_JFIF_JPEG); pixWrite("/tmp/dewtest/004.png", pixb, IFF_TIFF_G4); pixt1 = pixRead("/tmp/dewmod/002.png"); pixWrite("/tmp/dewtest/006.png", pixt1, IFF_PNG); pixDestroy(&pixt1); pixt1 = pixRead("/tmp/dewmod/003.png"); pixWrite("/tmp/dewtest/007.png", pixt1, IFF_PNG); pixDestroy(&pixt1); pixt1 = pixRead("/tmp/dewmod/006.png"); pixWrite("/tmp/dewtest/008.png", pixt1, IFF_PNG); pixDestroy(&pixt1); pixt1 = pixRead("/tmp/dewmod/007.png"); pixWrite("/tmp/dewtest/009.png", pixt1, IFF_PNG); pixDestroy(&pixt1); pixt1 = pixRead("/tmp/dewapply/002.png"); pixWrite("/tmp/dewtest/010.png", pixt1, IFF_PNG); pixDestroy(&pixt1); pixt1 = pixRead("/tmp/dewapply/003.png"); pixWrite("/tmp/dewtest/011.png", pixt1, IFF_PNG); pixt2 = pixThresholdToBinary(pixt1, 130); pixWrite("/tmp/dewtest/012.png", pixt2, IFF_TIFF_G4); pixDestroy(&pixt1); pixDestroy(&pixt2); pixt1 = pixRead("/tmp/dewmod/004a.png"); pixWrite("/tmp/dewtest/013.png", pixt1, IFF_PNG); pixDestroy(&pixt1); pixt1 = pixRead("/tmp/dewmod/004b.png"); pixWrite("/tmp/dewtest/014.png", pixt1, IFF_PNG); pixDestroy(&pixt1); pixt1 = pixRead("/tmp/dewmod/005a.png"); pixWrite("/tmp/dewtest/015.png", pixt1, IFF_PNG); pixDestroy(&pixt1); pixt1 = pixRead("/tmp/dewmod/005b.png"); pixWrite("/tmp/dewtest/016.png", pixt1, IFF_PNG); pixDestroy(&pixt1); /* Normalize another image, that may not have enough textlines * to build an accurate model */ /* pixs2 = pixRead("1555-3.jpg"); */ pixs2 = pixRead("cat-7.jpg"); /* pixs2 = pixRead("cat-14.jpg"); */ pixn2 = pixBackgroundNormSimple(pixs2, NULL, NULL); pixg2 = pixConvertRGBToGray(pixn2, 0.5, 0.3, 0.2); pixb2 = pixThresholdToBinary(pixg2, 130); /* Apply the previous disparity model to this image */ dew2 = dewarpCreate(pixb2, 14); dewarpaInsertDewarp(dewa, dew2); dewarpaInsertRefModels(dewa, 1); /* dewarpaInfo(stderr, dewa); */ dewarpaApplyDisparity(dewa, 14, pixg2, "/tmp/dewarp_apply2.pdf"); dewarpaDestroy(&dewa); /* Write out files for the second image */ pixWrite("/tmp/dewtest/017.jpg", pixs2, IFF_JFIF_JPEG); pixWrite("/tmp/dewtest/018.jpg", pixg2, IFF_JFIF_JPEG); pixWrite("/tmp/dewtest/019.png", pixb2, IFF_TIFF_G4); pixt1 = pixRead("/tmp/dewmod/006.png"); pixWrite("/tmp/dewtest/020.png", pixt1, IFF_PNG); pixDestroy(&pixt1); pixt1 = pixRead("/tmp/dewapply/002.png"); pixWrite("/tmp/dewtest/021.png", pixt1, IFF_PNG); pixt2 = pixThresholdToBinary(pixt1, 130); pixWrite("/tmp/dewtest/022.png", pixt2, IFF_TIFF_G4); pixDestroy(&pixt1); pixDestroy(&pixt2); pixt1 = pixRead("/tmp/dewmod/007.png"); pixWrite("/tmp/dewtest/023.png", pixt1, IFF_PNG); pixDestroy(&pixt1); pixt1 = pixRead("/tmp/dewapply/003.png"); pixWrite("/tmp/dewtest/024.png", pixt1, IFF_PNG); pixt2 = pixThresholdToBinary(pixt1, 130); pixWrite("/tmp/dewtest/025.png", pixt2, IFF_TIFF_G4); pixDestroy(&pixt1); pixDestroy(&pixt2); /* Generate the big pdf file */ convertFilesToPdf("/tmp/dewtest", NULL, 135, 1.0, 0, 0, "Dewarp Test", "/tmp/dewarp.pdf"); fprintf(stderr, "pdf file made: /tmp/dewarp.pdf\n"); pixDestroy(&pixs); pixDestroy(&pixn); pixDestroy(&pixg); pixDestroy(&pixb); pixDestroy(&pixs2); pixDestroy(&pixn2); pixDestroy(&pixg2); pixDestroy(&pixb2); return 0; }
l_int32 main(int argc, char **argv) { L_DEWARP *dew1, *dew2, *dew3; L_DEWARPA *dewa1, *dewa2, *dewa3; PIX *pixs, *pixn, *pixg, *pixb, *pixd; PIX *pixs2, *pixn2, *pixg2, *pixb2, *pixd2; PIX *pixd3, *pixc1, *pixc2; /* pixs = pixRead("1555-7.jpg"); */ pixs = pixRead("cat-35.jpg"); dewa1 = dewarpaCreate(40, 30, 1, 15, 10); dewarpaUseBothArrays(dewa1, 1); /* Normalize for varying background and binarize */ pixn = pixBackgroundNormSimple(pixs, NULL, NULL); pixg = pixConvertRGBToGray(pixn, 0.5, 0.3, 0.2); pixb = pixThresholdToBinary(pixg, 130); /* Run the basic functions */ dew1 = dewarpCreate(pixb, 35); dewarpaInsertDewarp(dewa1, dew1); dewarpBuildPageModel(dew1, "/tmp/dewarp_junk35.pdf"); /* debug output */ dewarpPopulateFullRes(dew1, pixg, 0, 0); dewarpaApplyDisparity(dewa1, 35, pixg, 200, 0, 0, &pixd, "/tmp/dewarp_debug_35.pdf"); /* Normalize another image. */ /* pixs2 = pixRead("1555-3.jpg"); */ pixs2 = pixRead("cat-7.jpg"); pixn2 = pixBackgroundNormSimple(pixs2, NULL, NULL); pixg2 = pixConvertRGBToGray(pixn2, 0.5, 0.3, 0.2); pixb2 = pixThresholdToBinary(pixg2, 130); /* Run the basic functions */ dew2 = dewarpCreate(pixb2, 7); dewarpaInsertDewarp(dewa1, dew2); dewarpBuildPageModel(dew2, "/tmp/dewarp_junk7.pdf"); dewarpaApplyDisparity(dewa1, 7, pixg, 200, 0, 0, &pixd2, "/tmp/dewarp_debug_7.pdf"); /* Serialize and deserialize dewarpa */ dewarpaWrite("/tmp/dewarpa1.dewa", dewa1); dewa2 = dewarpaRead("/tmp/dewarpa1.dewa"); dewarpaWrite("/tmp/dewarpa2.dewa", dewa2); dewa3 = dewarpaRead("/tmp/dewarpa2.dewa"); dewarpDebug(dewa3->dewarp[7], "dew1", 7); dewarpaWrite("/tmp/dewarpa3.dewa", dewa3); /* Repopulate and show the vertical disparity arrays */ dewarpPopulateFullRes(dew1, NULL, 0, 0); pixc1 = fpixRenderContours(dew1->fullvdispar, 2.0, 0.2); pixDisplay(pixc1, 1400, 900); dew3 = dewarpaGetDewarp(dewa2, 35); dewarpPopulateFullRes(dew3, pixs, 0, 0); pixc2 = fpixRenderContours(dew3->fullvdispar, 2.0, 0.2); pixDisplay(pixc2, 1400, 900); dewarpaApplyDisparity(dewa2, 35, pixb, 200, 0, 0, &pixd3, "/tmp/dewarp_debug_35b.pdf"); pixDisplay(pixd, 0, 1000); pixDisplay(pixd2, 600, 1000); pixDisplay(pixd3, 1200, 1000); pixDestroy(&pixd3); dewarpaDestroy(&dewa1); dewarpaDestroy(&dewa2); dewarpaDestroy(&dewa3); pixDestroy(&pixs); pixDestroy(&pixn); pixDestroy(&pixg); pixDestroy(&pixb); pixDestroy(&pixd); pixDestroy(&pixs2); pixDestroy(&pixn2); pixDestroy(&pixg2); pixDestroy(&pixb2); pixDestroy(&pixd2); pixDestroy(&pixc1); pixDestroy(&pixc2); return 0; }