58 #include "allheaders.h" 64 #define DEBUG_INVALID_MODELS 0 68 static const l_int32 GRAYIN_VALUE = 200;
101 l_int32 check_columns,
109 PROCNAME(
"dewarpSinglePage");
112 return ERROR_INT(
"&pixd not defined", procName, 1);
114 if (pdewa) *pdewa = NULL;
116 return ERROR_INT(
"pixs not defined", procName, 1);
119 check_columns, &pixb, &
dewa);
122 return ERROR_INT(
"pixb not made", procName, 1);
169 l_int32 check_columns,
175 PROCNAME(
"dewarpSinglePageInit");
177 if (ppixb) *ppixb = NULL;
178 if (pdewa) *pdewa = NULL;
179 if (!ppixb || !pdewa)
180 return ERROR_INT(
"&pixb and &dewa not both defined", procName, 1);
182 return ERROR_INT(
"pixs not defined", procName, 1);
189 if (pixGetDepth(
pixs) > 1) {
229 const char *debugfile;
233 PROCNAME(
"dewarpSinglePageRun");
236 return ERROR_INT(
"&pixd not defined", procName, 1);
239 return ERROR_INT(
"pixs not defined", procName, 1);
241 return ERROR_INT(
"pixb not defined", procName, 1);
243 return ERROR_INT(
"dewa not defined", procName, 1);
251 debugfile = (
debug) ?
"/tmp/lept/dewarp/singlepage_model.pdf" : NULL;
255 L_ERROR(
"failure to build model for vertical disparity\n", procName);
261 debugfile = (
debug) ?
"/tmp/lept/dewarp/singlepage_apply.pdf" : NULL;
264 L_ERROR(
"invalid model; failure to apply disparity\n", procName);
293 NUMA *namodels, *napages;
295 PROCNAME(
"dewarpaListPages");
298 return ERROR_INT(
"dewa not defined", procName, 1);
345 l_int32 i, n,
maxcurv, diffcurv, diffedge;
348 PROCNAME(
"dewarpaSetValidModels");
351 return ERROR_INT(
"dewa not defined", procName, 1);
354 for (i = 0; i < n; i++) {
360 L_INFO(
"page %d: has only a ref model\n", procName, i);
362 L_INFO(
"page %d: no model successfully built\n",
364 }
else if (!notests) {
368 L_INFO(
"page %d: useboth, but no horiz disparity\n",
371 L_INFO(
"page %d: max curvature %d > max_linecurv\n",
372 procName, i, diffcurv);
373 if (diffcurv < dewa->min_diff_linecurv)
374 L_INFO(
"page %d: diff curv %d < min_diff_linecurv\n",
375 procName, i, diffcurv);
377 L_INFO(
"page %d: abs diff curv %d > max_diff_linecurv\n",
378 procName, i, diffcurv);
381 L_INFO(
"page %d: abs left slope %d > max_edgeslope\n",
384 L_INFO(
"page %d: abs right slope %d > max_edgeslope\n",
388 L_INFO(
"page %d: left curvature %d > max_edgecurv\n",
391 L_INFO(
"page %d: right curvature %d > max_edgecurv\n",
394 L_INFO(
"page %d: abs diff left-right curv %d > " 395 "max_diff_edgecurv\n", procName, i, diffedge);
451 l_int32 i, j, n, val, min, distdown, distup;
455 PROCNAME(
"dewarpaInsertRefModels");
458 return ERROR_INT(
"dewa not defined", procName, 1);
460 L_INFO(
"maxdist < 2; no ref models can be used\n", procName);
466 for (i = 0; i < n; i++) {
478 for (i = 0; i < n; i++) {
480 if (val == 1)
continue;
488 for (j = i - 2; j >= 0 && distdown >
dewa->
maxdist; j -= 2) {
490 if (val == 1) distdown = i - j;
493 for (j = i + 2; j < n && distup >
dewa->
maxdist; j += 2) {
495 if (val == 1) distup = j - i;
497 min = L_MIN(distdown, distup);
499 if (distdown <= distup)
514 for (i = 0; i < n; i++) {
519 for (i = 0; i < n; i++) {
521 if (val == 1)
continue;
523 distdown = distup = 100000;
524 for (j = i - 2; j >= 0; j -= 2) {
531 for (j = i + 2; j < n; j += 2) {
538 min = L_MIN(distdown, distup);
544 L_ERROR(
"dew is null for page %d!\n", procName, i);
551 if (distdown <= distup)
583 PROCNAME(
"dewarpaStripRefModels");
586 return ERROR_INT(
"dewa not defined", procName, 1);
624 PROCNAME(
"dewarpaRestoreModels");
627 return ERROR_INT(
"dewa not defined", procName, 1);
637 L_ERROR(
"dew in both cache and main array!: page %d\n",
667 l_int32 i, n,
pageno, nnone, nvsuccess, nvvalid, nhsuccess, nhvalid, nref;
670 PROCNAME(
"dewarpaInfo");
673 return ERROR_INT(
"dewa not defined", procName, 1);
675 return ERROR_INT(
"dewa not defined", procName, 1);
677 fprintf(fp,
"\nDewarpaInfo: %p\n",
dewa);
679 fprintf(fp,
"sampling = %d, redfactor = %d, minlines = %d\n",
681 fprintf(fp,
"maxdist = %d, useboth = %d\n",
685 &nhsuccess, &nhvalid, &nref);
687 fprintf(stderr,
"Total number of pages with a dew = %d\n", n);
688 fprintf(stderr,
"Number of pages without any models = %d\n", nnone);
689 fprintf(stderr,
"Number of pages with a vert model = %d\n", nvsuccess);
690 fprintf(stderr,
"Number of pages with a valid vert model = %d\n", nvvalid);
691 fprintf(stderr,
"Number of pages with both models = %d\n", nhsuccess);
692 fprintf(stderr,
"Number of pages with both models valid = %d\n", nhvalid);
693 fprintf(stderr,
"Number of pages with a ref model = %d\n", nref);
695 for (i = 0; i < n; i++) {
699 fprintf(stderr,
"Page: %d\n", dew->
pageno);
700 fprintf(stderr,
" hasref = %d, refpage = %d\n",
702 fprintf(stderr,
" nlines = %d\n", dew->
nlines);
703 fprintf(stderr,
" w = %d, h = %d, nx = %d, ny = %d\n",
704 dew->
w, dew->
h, dew->
nx, dew->
ny);
706 fprintf(stderr,
" Vertical disparity builds:\n" 707 " (min,max,abs-diff) line curvature = (%d,%d,%d)\n",
710 fprintf(stderr,
" Horizontal disparity builds:\n" 711 " left edge slope = %d, right edge slope = %d\n" 712 " (left,right,abs-diff) edge curvature = (%d,%d,%d)\n",
764 l_int32 i, n,
pageno, nnone, nvsuccess, nvvalid, nhsuccess, nhvalid, nref;
767 PROCNAME(
"dewarpaModelStats");
770 return ERROR_INT(
"dewa not defined", procName, 1);
774 nnone = nref = nvsuccess = nvvalid = nhsuccess = nhvalid = 0;
775 for (i = 0; i < n; i++) {
795 if (pnnone) *pnnone = nnone;
796 if (pnref) *pnref = nref;
797 if (pnvsuccess) *pnvsuccess = nvsuccess;
798 if (pnvvalid) *pnvvalid = nvvalid;
799 if (pnhsuccess) *pnhsuccess = nhsuccess;
800 if (pnhvalid) *pnhvalid = nhvalid;
828 l_int32
maxcurv, diffcurv, diffedge;
830 PROCNAME(
"dewarpaTestForValidModel");
833 return ERROR_INT(
"dewa and dew not both defined", procName, 1);
845 if (dew->
hasref == 1)
return 0;
852 if (maxcurv <= dewa->max_linecurv &&
854 diffcurv <= dewa->max_diff_linecurv) {
857 L_INFO(
"invalid vert model for page %d:\n", procName, dew->
pageno);
858 #if DEBUG_INVALID_MODELS 859 fprintf(stderr,
" max line curv = %d, max allowed = %d\n",
861 fprintf(stderr,
" diff line curv = %d, max allowed = %d\n",
873 diffedge <= dewa->max_diff_edgecurv) {
876 L_INFO(
"invalid horiz model for page %d:\n", procName, dew->
pageno);
877 #if DEBUG_INVALID_MODELS 878 fprintf(stderr,
" left edge slope = %d, max allowed = %d\n",
880 fprintf(stderr,
" right edge slope = %d, max allowed = %d\n",
882 fprintf(stderr,
" left edge curv = %d, max allowed = %d\n",
884 fprintf(stderr,
" right edge curv = %d, max allowed = %d\n",
886 fprintf(stderr,
" diff edge curv = %d, max allowed = %d\n",
921 PIX *pixv, *pixvs, *pixh, *pixhs, *pixt, *pixd;
924 PROCNAME(
"dewarpaShowArrays");
927 return ERROR_INT(
"dew not defined", procName, 1);
929 return ERROR_INT(
"first out of bounds", procName, 1);
932 return ERROR_INT(
"last < first", procName, 1);
937 L_ERROR(
"bmf not made; page info not displayed", procName);
939 fprintf(stderr,
"Generating contour plots\n");
940 for (i = first; i <= last; i++) {
941 if (i && ((i % 10) == 0))
942 fprintf(stderr,
" .. %d", i);
945 if (dew->
hasref == 1)
continue;
950 L_ERROR(
"sampvdispar not made for page %d!\n", procName, i);
972 snprintf(buf,
sizeof(buf),
"Page %d", i);
975 snprintf(buf,
sizeof(buf),
"/tmp/lept/dewarp1/arrays_%04d.png", i);
976 pixWriteDebug(buf, pixd, IFF_PNG);
982 fprintf(stderr,
"\n");
984 fprintf(stderr,
"Generating pdf of contour plots\n");
986 0,
"Disparity arrays",
"/tmp/lept/disparity_arrays.pdf");
987 fprintf(stderr,
"Output written to: /tmp/lept/disparity_arrays.pdf\n");
1009 const char *subdirs,
1017 PROCNAME(
"dewarpDebug");
1020 return ERROR_INT(
"dew not defined", procName, 1);
1022 return ERROR_INT(
"subdirs not defined", procName, 1);
1024 fprintf(stderr,
"pageno = %d, hasref = %d, refpage = %d\n",
1026 fprintf(stderr,
"sampling = %d, redfactor = %d, minlines = %d\n",
1032 fprintf(stderr,
"sampv = %d, samph = %d\n", svd, shd);
1033 fprintf(stderr,
"w = %d, h = %d\n", dew->
w, dew->
h);
1034 fprintf(stderr,
"nx = %d, ny = %d\n", dew->
nx, dew->
ny);
1035 fprintf(stderr,
"nlines = %d\n", dew->
nlines);
1037 fprintf(stderr,
"(min,max,abs-diff) line curvature = (%d,%d,%d)\n",
1041 fprintf(stderr,
"(left edge slope = %d, right edge slope = %d\n",
1043 fprintf(stderr,
"(left,right,abs-diff) edge curvature = " 1049 fprintf(stderr,
"No disparity arrays\n");
1055 outdir =
pathJoin(
"/tmp", subdirs);
1058 snprintf(fname,
sizeof(fname),
"%s/pixv_%d.png", outdir, index);
1059 pixWriteDebug(fname, pixv, IFF_PNG);
1064 snprintf(fname,
sizeof(fname),
"%s/pixh_%d.png", outdir, index);
1065 pixWriteDebug(fname, pixh, IFF_PNG);
1102 l_int32 i, modelpage;
1106 PIX *
pixs, *pixc, *pixd, *pixt1, *pixt2;
1109 PROCNAME(
"dewarpShowResults");
1112 return ERROR_INT(
"dewa not defined", procName, 1);
1114 return ERROR_INT(
"sa not defined", procName, 1);
1116 return ERROR_INT(
"pdfout not defined", procName, 1);
1117 if (firstpage > lastpage)
1118 return ERROR_INT(
"invalid first/last page numbers", procName, 1);
1124 fprintf(stderr,
"Dewarping and generating s/by/s view\n");
1125 for (i = firstpage; i <= lastpage; i++) {
1126 if (i && (i % 10 == 0)) fprintf(stderr,
".. %d ", i);
1139 GRAYIN_VALUE, 0, 0, &pixd, NULL);
1149 snprintf(bufstr,
sizeof(bufstr),
"Page %d; using %d\n",
1153 snprintf(bufstr,
sizeof(bufstr),
"Page %d; no dewarp\n", i);
1156 snprintf(bufstr,
sizeof(bufstr),
"/tmp/lept/dewarp_pdfout/%05d", i);
1157 pixWriteDebug(bufstr, pixt2, IFF_JFIF_JPEG);
1163 fprintf(stderr,
"\n");
1165 fprintf(stderr,
"Generating pdf of result\n");
1167 0,
"Dewarp sequence", pdfout);
1168 fprintf(stderr,
"Output written to: %s\n", pdfout);
struct FPix * sampvdispar
struct L_Dewarp ** dewarp
struct L_Dewarp ** dewarpcache
struct FPix * samphdispar
void bmfDestroy(L_BMF **pbmf)
bmfDestroy()
l_ok dewarpSinglePageRun(PIX *pixs, PIX *pixb, L_DEWARPA *dewa, PIX **ppixd, l_int32 debug)
dewarpSinglePageRun()
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_int32 lept_mkdir(const char *subdir)
lept_mkdir()
l_int32 max_diff_linecurv
l_ok dewarpShowResults(L_DEWARPA *dewa, SARRAY *sa, BOXA *boxa, l_int32 firstpage, l_int32 lastpage, const char *pdfout)
dewarpShowResults()
l_ok numaAddNumber(NUMA *na, l_float32 val)
numaAddNumber()
PIXA * pixaCreate(l_int32 n)
pixaCreate()
L_DEWARP * dewarpCreate(PIX *pixs, l_int32 pageno)
dewarpCreate()
l_int32 max_diff_edgecurv
l_ok dewarpaApplyDisparity(L_DEWARPA *dewa, l_int32 pageno, PIX *pixs, l_int32 grayin, l_int32 x, l_int32 y, PIX **ppixd, const char *debugfile)
dewarpaApplyDisparity()
PIX * pixReadIndexed(SARRAY *sa, l_int32 index)
pixReadIndexed()
PIX * pixConvertTo8(PIX *pixs, l_int32 cmapflag)
pixConvertTo8()
NUMA * numaMakeConstant(l_float32 val, l_int32 size)
numaMakeConstant()
l_ok dewarpaShowArrays(L_DEWARPA *dewa, l_float32 scalefact, l_int32 first, l_int32 last)
dewarpaShowArrays()
void dewarpDestroy(L_DEWARP **pdew)
dewarpDestroy()
NUMA * numaCreate(l_int32 n)
numaCreate()
PIX * pixThresholdToBinary(PIX *pixs, l_int32 thresh)
pixThresholdToBinary()
PIX * pixScaleBySampling(PIX *pixs, l_float32 scalex, l_float32 scaley)
pixScaleBySampling()
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 convertFilesToPdf(const char *dirname, const char *substr, l_int32 res, l_float32 scalefactor, l_int32 type, l_int32 quality, const char *title, const char *fileout)
convertFilesToPdf()
PIX * pixClipRectangle(PIX *pixs, BOX *box, BOX **pboxc)
pixClipRectangle()
void dewarpaDestroy(L_DEWARPA **pdewa)
dewarpaDestroy()
l_ok dewarpBuildPageModel(L_DEWARP *dew, const char *debugfile)
dewarpBuildPageModel()
l_ok dewarpaListPages(L_DEWARPA *dewa)
dewarpaListPages()
l_ok numaGetIValue(NUMA *na, l_int32 index, l_int32 *pival)
numaGetIValue()
l_ok pixaAddPix(PIXA *pixa, PIX *pix, l_int32 copyflag)
pixaAddPix()
l_int32 min_diff_linecurv
l_ok dewarpaRestoreModels(L_DEWARPA *dewa)
dewarpaRestoreModels()
l_int32 numaGetCount(NUMA *na)
numaGetCount()
l_ok dewarpMinimize(L_DEWARP *dew)
dewarpMinimize()
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 dewarpaModelStatus(L_DEWARPA *dewa, l_int32 pageno, l_int32 *pvsuccess, l_int32 *phsuccess)
dewarpaModelStatus()
PIX * pixaDisplayTiledInRows(PIXA *pixa, l_int32 outdepth, l_int32 maxwidth, l_float32 scalefactor, l_int32 background, l_int32 spacing, l_int32 border)
pixaDisplayTiledInRows()
l_ok dewarpPopulateFullRes(L_DEWARP *dew, PIX *pix, l_int32 x, l_int32 y)
dewarpPopulateFullRes()
L_DEWARPA * dewarpaCreate(l_int32 nptrs, l_int32 sampling, l_int32 redfactor, l_int32 minlines, l_int32 maxdist)
dewarpaCreate()
PIX * pixClone(PIX *pixs)
pixClone()
PIX * pixaDisplayTiledAndScaled(PIXA *pixa, l_int32 outdepth, l_int32 tilewidth, l_int32 ncols, l_int32 background, l_int32 spacing, l_int32 border)
pixaDisplayTiledAndScaled()
void pixDestroy(PIX **ppix)
pixDestroy()
BOX * boxaGetBox(BOXA *boxa, l_int32 index, l_int32 accessflag)
boxaGetBox()
l_ok dewarpaStripRefModels(L_DEWARPA *dewa)
dewarpaStripRefModels()
void numaDestroy(NUMA **pna)
numaDestroy()
l_ok dewarpaUseBothArrays(L_DEWARPA *dewa, l_int32 useboth)
dewarpaUseBothArrays()
PIX * pixAdaptThresholdToBinary(PIX *pixs, PIX *pixm, l_float32 gamma)
pixAdaptThresholdToBinary()
l_ok dewarpaInsertDewarp(L_DEWARPA *dewa, L_DEWARP *dew)
dewarpaInsertDewarp()
char * pathJoin(const char *dir, const char *fname)
pathJoin()
l_ok dewarpaSetValidModels(L_DEWARPA *dewa, l_int32 notests, l_int32 debug)
dewarpaSetValidModels()
struct FPix * fullvdispar
l_ok dewarpaSetCheckColumns(L_DEWARPA *dewa, l_int32 check_columns)
dewarpaSetCheckColumns()
static l_int32 dewarpaTestForValidModel(L_DEWARPA *dewa, L_DEWARP *dew, l_int32 notests)
dewarpaTestForValidModel()
PIX * pixCopy(PIX *pixd, PIX *pixs)
pixCopy()
l_ok numaReplaceNumber(NUMA *na, l_int32 index, l_float32 val)
numaReplaceNumber()
void boxDestroy(BOX **pbox)
boxDestroy()
struct FPix * fullhdispar
l_int32 lept_rmdir(const char *subdir)
lept_rmdir()
l_ok dewarpaInfo(FILE *fp, L_DEWARPA *dewa)
dewarpaInfo()
void pixaDestroy(PIXA **ppixa)
pixaDestroy()
PIX * pixAddSingleTextblock(PIX *pixs, L_BMF *bmf, const char *textstr, l_uint32 val, l_int32 location, l_int32 *poverflow)
pixAddSingleTextblock()
l_ok dewarpaInsertRefModels(L_DEWARPA *dewa, l_int32 notests, l_int32 debug)
dewarpaInsertRefModels()
l_ok dewarpDebug(L_DEWARP *dew, const char *subdirs, l_int32 index)
dewarpDebug()
L_DEWARP * dewarpCreateRef(l_int32 pageno, l_int32 refpage)
dewarpCreateRef()
L_DEWARP * dewarpaGetDewarp(L_DEWARPA *dewa, l_int32 index)
dewarpaGetDewarp()
PIX * fpixRenderContours(FPIX *fpixs, l_float32 incr, l_float32 proxim)
fpixRenderContours()
L_BMF * bmfCreate(const char *dir, l_int32 fontsize)
bmfCreate()