/*! * dewarpSinglePageInit() * * Input: pixs (with text, any depth) * thresh (for global thresholding to 1 bpp; ignored otherwise) * adaptive (1 for adaptive thresholding; 0 for global threshold) * use_both (1 for horizontal and vertical; 0 for vertical only) * &pixb (<return> 1 bpp image) * &dewa (<return> initialized dewa) * Return: 0 if OK, 1 on error (list of page numbers), or null on error * * Notes: * (1) This binarizes the input pixs if necessary, returning the * binarized image. It also initializes the dewa to default values * for the model parameters. * (2) If pixs is 1 bpp, the parameters @adaptive and @thresh are ignored. * (3) To change the model parameters, call dewarpaSetCurvatures() * before running dewarpSinglePageRun(). For DC: * dewarpSinglePageInit(pixs, 0, 1, 1, &pixb, &dewa); * dewarpaSetCurvatures(dewa, 250, -1, -1, 80, 70, 150); * dewarpSinglePageRun(pixs, pixb, dewa, &pixd, 0); * dewarpaDestroy(&dewa); * pixDestroy(&pixb); */ l_int32 dewarpSinglePageInit(PIX *pixs, l_int32 thresh, l_int32 adaptive, l_int32 use_both, PIX **ppixb, L_DEWARPA **pdewa) { PIX *pix1; PROCNAME("dewarpSinglePageInit"); if (ppixb) *ppixb = NULL; if (pdewa) *pdewa = NULL; if (!ppixb || !pdewa) return ERROR_INT("&pixb and &dewa not both defined", procName, 1); if (!pixs) return ERROR_INT("pixs not defined", procName, 1); *pdewa = dewarpaCreate(1, 0, 1, 0, -1); dewarpaUseBothArrays(*pdewa, use_both); /* Generate a binary image, if necessary */ if (pixGetDepth(pixs) > 1) { pix1 = pixConvertTo8(pixs, 0); if (adaptive) *ppixb = pixAdaptThresholdToBinary(pix1, NULL, 1.0); else *ppixb = pixThresholdToBinary(pix1, thresh); pixDestroy(&pix1); } else { *ppixb = pixClone(pixs); } 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 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) { 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; }