104 l_int32 check_columns,
113 return ERROR_INT(
"&pixd not defined", __func__, 1);
115 if (pdewa) *pdewa = NULL;
117 return ERROR_INT(
"pixs not defined", __func__, 1);
120 check_columns, &pixb, &dewa);
122 dewarpaDestroy(&dewa);
123 return ERROR_INT(
"pixb not made", __func__, 1);
131 dewarpaDestroy(&dewa);
170 l_int32 check_columns,
176 if (ppixb) *ppixb = NULL;
177 if (pdewa) *pdewa = NULL;
178 if (!ppixb || !pdewa)
179 return ERROR_INT(
"&pixb and &dewa not both defined", __func__, 1);
181 return ERROR_INT(
"pixs not defined", __func__, 1);
184 if (pixGetDepth(pixs) > 1) {
185 if ((pix1 = pixConvertTo8(pixs, 0)) == NULL)
186 return ERROR_INT(
"pix1 not made", __func__, 1);
188 pix2 = pixAdaptThresholdToBinary(pix1, NULL, 1.0);
190 pix2 = pixThresholdToBinary(pix1, thresh);
193 return ERROR_INT(
"pix2 not made", __func__, 1);
196 *ppixb = pixClone(pixs);
199 *pdewa = dewarpaCreate(1, 0, 1, 0, -1);
200 dewarpaUseBothArrays(*pdewa, useboth);
201 dewarpaSetCheckColumns(*pdewa, check_columns);
232const char *debugfile;
233l_int32 vsuccess, ret;
237 return ERROR_INT(
"&pixd not defined", __func__, 1);
240 return ERROR_INT(
"pixs not defined", __func__, 1);
242 return ERROR_INT(
"pixb not defined", __func__, 1);
244 return ERROR_INT(
"dewa not defined", __func__, 1);
247 lept_mkdir(
"lept/dewarp");
250 dew = dewarpCreate(pixb, 0);
251 dewarpaInsertDewarp(dewa, dew);
252 debugfile = (debug) ?
"/tmp/lept/dewarp/singlepage_model.pdf" : NULL;
253 dewarpBuildPageModel(dew, debugfile);
254 dewarpaModelStatus(dewa, 0, &vsuccess, NULL);
256 L_ERROR(
"failure to build model for vertical disparity\n", __func__);
257 *ppixd = pixCopy(NULL, pixs);
262 debugfile = (debug) ?
"/tmp/lept/dewarp/singlepage_apply.pdf" : NULL;
263 ret = dewarpaApplyDisparity(dewa, 0, pixs, 255, 0, 0, ppixd, debugfile);
265 L_ERROR(
"invalid model; failure to apply disparity\n", __func__);
344l_int32 i, n, maxcurv, diffcurv, diffedge;
348 return ERROR_INT(
"dewa not defined", __func__, 1);
351 for (i = 0; i < n; i++) {
352 if ((dew = dewarpaGetDewarp(dewa, i)) == NULL)
357 L_INFO(
"page %d: has only a ref model\n", __func__, i);
359 L_INFO(
"page %d: no model successfully built\n",
361 }
else if (!notests) {
365 L_INFO(
"page %d: useboth, but no horiz disparity\n",
368 L_INFO(
"page %d: max curvature %d > max_linecurv\n",
369 __func__, i, diffcurv);
370 if (diffcurv < dewa->min_diff_linecurv)
371 L_INFO(
"page %d: diff curv %d < min_diff_linecurv\n",
372 __func__, i, diffcurv);
374 L_INFO(
"page %d: abs diff curv %d > max_diff_linecurv\n",
375 __func__, i, diffcurv);
378 L_INFO(
"page %d: abs left slope %d > max_edgeslope\n",
381 L_INFO(
"page %d: abs right slope %d > max_edgeslope\n",
385 L_INFO(
"page %d: left curvature %d > max_edgecurv\n",
388 L_INFO(
"page %d: right curvature %d > max_edgecurv\n",
391 L_INFO(
"page %d: abs diff left-right curv %d > "
392 "max_diff_edgecurv\n", __func__, i, diffedge);
448l_int32 i, j, n, val, min, distdown, distup;
453 return ERROR_INT(
"dewa not defined", __func__, 1);
455 L_INFO(
"maxdist < 2; no ref models can be used\n", __func__);
460 na = numaMakeConstant(0, n);
461 for (i = 0; i < n; i++) {
462 dew = dewarpaGetDewarp(dewa, i);
464 numaReplaceNumber(na, i, 1);
473 for (i = 0; i < n; i++) {
474 numaGetIValue(na, i, &val);
475 if (val == 1)
continue;
476 if ((dew = dewa->
dewarp[i]) != NULL) {
480 if (dewa->
maxdist < 2)
continue;
482 distdown = distup = dewa->
maxdist + 1;
483 for (j = i - 2; j >= 0 && distdown > dewa->
maxdist; j -= 2) {
484 numaGetIValue(na, j, &val);
485 if (val == 1) distdown = i - j;
488 for (j = i + 2; j < n && distup > dewa->
maxdist; j += 2) {
489 numaGetIValue(na, j, &val);
490 if (val == 1) distup = j - i;
492 min = L_MIN(distdown, distup);
493 if (min > dewa->
maxdist)
continue;
494 if (distdown <= distup)
495 dewarpaInsertDewarp(dewa, dewarpCreateRef(i, i - distdown));
497 dewarpaInsertDewarp(dewa, dewarpCreateRef(i, i + distup));
508 nah = numaMakeConstant(0, n);
509 for (i = 0; i < n; i++) {
510 dew = dewarpaGetDewarp(dewa, i);
512 numaReplaceNumber(nah, i, 1);
514 for (i = 0; i < n; i++) {
515 numaGetIValue(nah, i, &val);
516 if (val == 1)
continue;
517 if (dewa->
maxdist < 2)
continue;
518 distdown = distup = 100000;
519 for (j = i - 2; j >= 0; j -= 2) {
520 numaGetIValue(nah, j, &val);
526 for (j = i + 2; j < n; j += 2) {
527 numaGetIValue(nah, j, &val);
533 min = L_MIN(distdown, distup);
534 if (min > dewa->
maxdist)
continue;
538 if ((dew = dewarpaGetDewarp(dewa, i)) == NULL) {
539 L_ERROR(
"dew is null for page %d!\n", __func__, i);
546 if (distdown <= distup)
547 dewarpaInsertDewarp(dewa, dewarpCreateRef(i, i - distdown));
549 dewarpaInsertDewarp(dewa, dewarpCreateRef(i, i + distup));
658l_int32 i, n, pageno, nnone, nvsuccess, nvvalid, nhsuccess, nhvalid, nref;
662 return ERROR_INT(
"dewa not defined", __func__, 1);
664 return ERROR_INT(
"dewa not defined", __func__, 1);
666 fprintf(fp,
"\nDewarpaInfo: %p\n", dewa);
667 fprintf(fp,
"nalloc = %d, maxpage = %d\n", dewa->
nalloc, dewa->
maxpage);
668 fprintf(fp,
"sampling = %d, redfactor = %d, minlines = %d\n",
670 fprintf(fp,
"maxdist = %d, useboth = %d\n",
674 &nhsuccess, &nhvalid, &nref);
675 n = numaGetCount(dewa->
napages);
676 lept_stderr(
"Total number of pages with a dew = %d\n", n);
677 lept_stderr(
"Number of pages without any models = %d\n", nnone);
678 lept_stderr(
"Number of pages with a vert model = %d\n", nvsuccess);
679 lept_stderr(
"Number of pages with a valid vert model = %d\n", nvvalid);
680 lept_stderr(
"Number of pages with both models = %d\n", nhsuccess);
681 lept_stderr(
"Number of pages with both models valid = %d\n", nhvalid);
682 lept_stderr(
"Number of pages with a ref model = %d\n", nref);
684 for (i = 0; i < n; i++) {
685 numaGetIValue(dewa->
napages, i, &pageno);
686 if ((dew = dewarpaGetDewarp(dewa, pageno)) == NULL)
688 lept_stderr(
"Page: %d\n", dew->
pageno);
689 lept_stderr(
" hasref = %d, refpage = %d\n",
691 lept_stderr(
" nlines = %d\n", dew->
nlines);
692 lept_stderr(
" w = %d, h = %d, nx = %d, ny = %d\n",
693 dew->
w, dew->
h, dew->
nx, dew->
ny);
695 lept_stderr(
" Vertical disparity builds:\n"
696 " (min,max,abs-diff) line curvature = (%d,%d,%d)\n",
699 lept_stderr(
" Horizontal disparity builds:\n"
700 " left edge slope = %d, right edge slope = %d\n"
701 " (left,right,abs-diff) edge curvature = (%d,%d,%d)\n",
753l_int32 i, n, pageno, nnone, nvsuccess, nvvalid, nhsuccess, nhvalid, nref;
757 return ERROR_INT(
"dewa not defined", __func__, 1);
760 n = numaGetCount(dewa->
napages);
761 nnone = nref = nvsuccess = nvvalid = nhsuccess = nhvalid = 0;
762 for (i = 0; i < n; i++) {
763 numaGetIValue(dewa->
napages, i, &pageno);
764 dew = dewarpaGetDewarp(dewa, pageno);
782 if (pnnone) *pnnone = nnone;
783 if (pnref) *pnref = nref;
784 if (pnvsuccess) *pnvsuccess = nvsuccess;
785 if (pnvvalid) *pnvvalid = nvvalid;
786 if (pnhsuccess) *pnhsuccess = nhsuccess;
787 if (pnhvalid) *pnhvalid = nhvalid;
906PIX *pixv, *pixvs, *pixh, *pixhs = NULL, *pixt, *pixd;
910 return ERROR_INT(
"dew not defined", __func__, 1);
911 if (first < 0 || first > dewa->
maxpage)
912 return ERROR_INT(
"first out of bounds", __func__, 1);
915 return ERROR_INT(
"last < first", __func__, 1);
917 lept_rmdir(
"lept/dewarp1");
918 lept_mkdir(
"lept/dewarp1");
919 if ((bmf = bmfCreate(NULL, 8)) == NULL)
920 L_ERROR(
"bmf not made; page info not displayed", __func__);
922 lept_stderr(
"Generating contour plots\n");
923 for (i = first; i <= last; i++) {
924 if (i && ((i % 10) == 0))
925 lept_stderr(
" .. %d", i);
926 dew = dewarpaGetDewarp(dewa, i);
928 if (dew->
hasref == 1)
continue;
933 L_ERROR(
"sampvdispar not made for page %d!\n", __func__, i);
938 dewarpPopulateFullRes(dew, NULL, 0, 0);
939 pixv = fpixRenderContours(dew->
fullvdispar, 3.0f, 0.15f);
940 pixvs = pixScaleBySampling(pixv, scalefact, scalefact);
943 pixh = fpixRenderContours(dew->
fullhdispar, 3.0f, 0.15f);
944 pixhs = pixScaleBySampling(pixh, scalefact, scalefact);
950 pixa = pixaCreate(2);
954 pixt = pixaDisplayTiledInRows(pixa, 32, 1500, 1.0, 0, 30, 2);
955 snprintf(buf,
sizeof(buf),
"Page %d", i);
956 pixd = pixAddSingleTextblock(pixt, bmf, buf, 0x0000ff00,
958 snprintf(buf,
sizeof(buf),
"/tmp/lept/dewarp1/arrays_%04d.png", i);
959 pixWriteDebug(buf, pixd, IFF_PNG);
967 lept_stderr(
"Generating pdf of contour plots\n");
968 convertFilesToPdf(
"/tmp/lept/dewarp1",
"arrays_", 90, 1.0,
L_FLATE_ENCODE,
969 0,
"Disparity arrays",
"/tmp/lept/disparity_arrays.pdf");
970 lept_stderr(
"Output written to: /tmp/lept/disparity_arrays.pdf\n");
1001 return ERROR_INT(
"dew not defined", __func__, 1);
1003 return ERROR_INT(
"subdirs not defined", __func__, 1);
1005 lept_stderr(
"pageno = %d, hasref = %d, refpage = %d\n",
1007 lept_stderr(
"sampling = %d, redfactor = %d, minlines = %d\n",
1013 lept_stderr(
"sampv = %d, samph = %d\n", svd, shd);
1014 lept_stderr(
"w = %d, h = %d\n", dew->
w, dew->
h);
1015 lept_stderr(
"nx = %d, ny = %d\n", dew->
nx, dew->
ny);
1016 lept_stderr(
"nlines = %d\n", dew->
nlines);
1018 lept_stderr(
"(min,max,abs-diff) line curvature = (%d,%d,%d)\n",
1022 lept_stderr(
"(left edge slope = %d, right edge slope = %d\n",
1024 lept_stderr(
"(left,right,abs-diff) edge curvature = "
1030 lept_stderr(
"No disparity arrays\n");
1034 dewarpPopulateFullRes(dew, NULL, 0, 0);
1035 lept_mkdir(subdirs);
1036 outdir = pathJoin(
"/tmp", subdirs);
1038 pixv = fpixRenderContours(dew->
fullvdispar, 3.0f, 0.15f);
1039 snprintf(fname,
sizeof(fname),
"%s/pixv_%d.png", outdir, index);
1040 pixWriteDebug(fname, pixv, IFF_PNG);
1044 pixh = fpixRenderContours(dew->
fullhdispar, 3.0f, 0.15f);
1045 snprintf(fname,
sizeof(fname),
"%s/pixh_%d.png", outdir, index);
1046 pixWriteDebug(fname, pixh, IFF_PNG);
1084l_int32 i, modelpage;
1088PIX *pixs, *pixc, *pixd, *pixt1, *pixt2;
1092 return ERROR_INT(
"dewa not defined", __func__, 1);
1094 return ERROR_INT(
"sa not defined", __func__, 1);
1096 return ERROR_INT(
"pdfout not defined", __func__, 1);
1097 if (firstpage > lastpage)
1098 return ERROR_INT(
"invalid first/last page numbers", __func__, 1);
1100 lept_rmdir(
"lept/dewarp_pdfout");
1101 lept_mkdir(
"lept/dewarp_pdfout");
1102 bmf = bmfCreate(NULL, 6);
1104 lept_stderr(
"Dewarping and generating s/by/s view\n");
1105 for (i = firstpage; i <= lastpage; i++) {
1106 if (i && (i % 10 == 0)) lept_stderr(
".. %d ", i);
1107 pixs = pixReadIndexed(sa, i);
1109 box = boxaGetBox(boxa, i,
L_CLONE);
1110 pixc = pixClipRectangle(pixs, box, NULL);
1114 pixc = pixClone(pixs);
1115 dew = dewarpaGetDewarp(dewa, i);
1118 dewarpaApplyDisparity(dewa, dew->
pageno, pixc,
1119 GrayInValue, 0, 0, &pixd, NULL);
1120 dewarpMinimize(dew);
1122 pixa = pixaCreate(2);
1126 pixt1 = pixaDisplayTiledAndScaled(pixa, 32, 500, 2, 0, 35, 2);
1129 snprintf(bufstr,
sizeof(bufstr),
"Page %d; using %d\n",
1133 snprintf(bufstr,
sizeof(bufstr),
"Page %d; no dewarp\n", i);
1134 pixt2 = pixAddSingleTextblock(pixt1, bmf, bufstr, 0x0000ff00,
1136 snprintf(bufstr,
sizeof(bufstr),
"/tmp/lept/dewarp_pdfout/%05d", i);
1137 pixWriteDebug(bufstr, pixt2, IFF_JFIF_JPEG);
1145 lept_stderr(
"Generating pdf of result\n");
1146 convertFilesToPdf(
"/tmp/lept/dewarp_pdfout", NULL, 100, 1.0,
L_JPEG_ENCODE,
1147 0,
"Dewarp sequence", pdfout);
1148 lept_stderr(
"Output written to: %s\n", pdfout);
l_ok dewarpaModelStats(L_DEWARPA *dewa, l_int32 *pnnone, l_int32 *pnvsuccess, l_int32 *pnvvalid, l_int32 *pnhsuccess, l_int32 *pnhvalid, l_int32 *pnref)
dewarpaModelStats()
l_ok dewarpSinglePage(PIX *pixs, l_int32 thresh, l_int32 adaptive, l_int32 useboth, l_int32 check_columns, PIX **ppixd, L_DEWARPA **pdewa, l_int32 debug)
dewarpSinglePage()
l_ok dewarpSinglePageInit(PIX *pixs, l_int32 thresh, l_int32 adaptive, l_int32 useboth, l_int32 check_columns, PIX **ppixb, L_DEWARPA **pdewa)
dewarpSinglePageInit()
l_ok dewarpShowResults(L_DEWARPA *dewa, SARRAY *sa, BOXA *boxa, l_int32 firstpage, l_int32 lastpage, const char *pdfout)
dewarpShowResults()