157 const char *debugfile)
159l_int32 linecount, ntop, nbot, ytop, ybot, ret;
160PIX *pixs, *pix1, *pix2, *pix3;
165 return ERROR_INT(
"dew not defined", __func__, 1);
167 dew->
debug = (debugfile) ? 1 : 0;
171 lept_rmdir(
"lept/dewmod");
172 lept_mkdir(
"lept/dewmod");
173 pixDisplayWithTitle(pixs, 0, 0,
"pixs", 1);
174 pixWriteDebug(
"/tmp/lept/dewmod/0010.png", pixs, IFF_PNG);
180 L_WARNING(
"textline centers not found; model not built\n", __func__);
184 pix1 = pixConvertTo32(pixs);
185 pta = generatePtaFilledCircle(1);
186 pix2 = pixGenerateFromPta(pta, 5, 5);
187 pix3 = pixDisplayPtaaPattern(NULL, pix1, ptaa1, pix2, 2, 2);
188 pixWriteDebug(
"/tmp/lept/dewmod/0020.png", pix3, IFF_PNG);
198 debugfile || DEBUG_SHORT_LINES);
200 pix1 = pixConvertTo32(pixs);
201 pta = generatePtaFilledCircle(1);
202 pix2 = pixGenerateFromPta(pta, 5, 5);
203 pix3 = pixDisplayPtaaPattern(NULL, pix1, ptaa2, pix2, 2, 2);
204 pixWriteDebug(
"/tmp/lept/dewmod/0030.png", pix3, IFF_PNG);
213 linecount = ptaaGetCount(ptaa2);
214 if (linecount < dew->minlines) {
216 L_WARNING(
"linecount %d < min req'd number of lines (%d) for model\n",
217 __func__, linecount, dew->
minlines);
224 &ntop, &nbot, &ytop, &ybot) == FALSE) {
226 L_WARNING(
"invalid line coverage: ntop = %d, nbot = %d;"
227 " spanning [%d ... %d] in height %d\n", __func__,
228 ntop, nbot, ytop, ybot, pixGetHeight(pixs));
236 L_WARNING(
"vertical disparity not built\n", __func__);
247 L_INFO(
"hsuccess = 1\n", __func__);
251 dewarpPopulateFullRes(dew, NULL, 0, 0);
252 pix1 = fpixRenderContours(dew->
fullvdispar, 3.0f, 0.15f);
253 pixWriteDebug(
"/tmp/lept/dewmod/0060.png", pix1, IFF_PNG);
254 pixDisplay(pix1, 1000, 0);
257 pix1 = fpixRenderContours(dew->
fullhdispar, 3.0f, 0.15f);
258 pixWriteDebug(
"/tmp/lept/dewmod/0070.png", pix1, IFF_PNG);
259 pixDisplay(pix1, 1000, 0);
262 convertFilesToPdf(
"/tmp/lept/dewmod", NULL, 135, 1.0, 0, 0,
263 "Dewarp Build Model", debugfile);
264 lept_stderr(
"pdf file: %s\n", debugfile);
305l_int32 i, j, nlines, npts, nx, ny, sampling;
306l_float32 c0, c1, c2, x, y, midy, val, medval, meddev, minval, maxval;
308NUMA *nax, *nafit, *nacurve0, *nacurve1, *nacurves;
309NUMA *namidy, *namidys, *namidysi;
310PIX *pix1, *pix2, *pixcirc, *pixdb;
311PTA *pta, *ptad, *ptacirc;
312PTAA *ptaa0, *ptaa1, *ptaa2, *ptaa3, *ptaa4, *ptaa5, *ptaat;
316 return ERROR_INT(
"dew not defined", __func__, 1);
319 return ERROR_INT(
"ptaa not defined", __func__, 1);
321 if (dew->
debug) L_INFO(
"finding vertical disparity\n", __func__);
330 nx = (rotflag) ? dew->
ny : dew->
nx;
331 ny = (rotflag) ? dew->
nx : dew->
ny;
332 nlines = ptaaGetCount(ptaa);
334 ptaa0 = ptaaCreate(nlines);
335 nacurve0 = numaCreate(nlines);
336 pixdb = (rotflag) ? pixRotateOrth(dew->
pixs, 1) : pixClone(dew->
pixs);
337 for (i = 0; i < nlines; i++) {
338 pta = ptaaGetPta(ptaa, i,
L_CLONE);
339 ptaGetQuadraticLSF(pta, &c2, &c1, &c0, NULL);
340 numaAddNumber(nacurve0, c2);
341 ptad = ptaCreate(nx);
342 for (j = 0; j < nx; j++) {
344 applyQuadraticFit(c2, c1, c0, x, &y);
345 ptaAddPt(ptad, x, y);
351 lept_mkdir(
"lept/dewarp");
352 lept_mkdir(
"lept/dewdebug");
353 lept_mkdir(
"lept/dewmod");
354 ptaat = ptaaCreate(nlines);
355 for (i = 0; i < nlines; i++) {
356 pta = ptaaGetPta(ptaa, i,
L_CLONE);
357 ptaGetArrays(pta, &nax, NULL);
358 ptaGetQuadraticLSF(pta, NULL, NULL, NULL, &nafit);
359 ptad = ptaCreateFromNuma(nax, nafit);
365 pix1 = pixConvertTo32(pixdb);
366 pta = generatePtaFilledCircle(1);
367 pixcirc = pixGenerateFromPta(pta, 5, 5);
368 pix2 = pixDisplayPtaaPattern(NULL, pix1, ptaat, pixcirc, 2, 2);
369 pixWriteDebug(
"/tmp/lept/dewmod/0041.png", pix2, IFF_PNG);
373 pixDestroy(&pixcirc);
382 numaGetMedianDevFromMedian(nacurve0, &medval, &meddev);
383 L_INFO(
"\nPage %d\n", __func__, dew->
pageno);
384 L_INFO(
"Pass 1: Curvature: medval = %f, meddev = %f\n",
385 __func__, medval, meddev);
386 ptaa1 = ptaaCreate(nlines);
387 nacurve1 = numaCreate(nlines);
388 for (i = 0; i < nlines; i++) {
389 numaGetFValue(nacurve0, i, &val);
390 if (L_ABS(val - medval) > 7.0 * meddev)
392 pta = ptaaGetPta(ptaa0, i,
L_CLONE);
394 numaAddNumber(nacurve1, val);
396 nlines = ptaaGetCount(ptaa1);
397 numaDestroy(&nacurve0);
400 numaGetMin(nacurve1, &minval, NULL);
401 numaGetMax(nacurve1, &maxval, NULL);
402 dew->
mincurv = lept_roundftoi(1000000. * minval);
403 dew->
maxcurv = lept_roundftoi(1000000. * maxval);
404 L_INFO(
"Pass 2: Min/max curvature = (%d, %d)\n", __func__,
409 namidy = numaCreate(nlines);
410 for (i = 0; i < nlines; i++) {
411 pta = ptaaGetPta(ptaa1, i,
L_CLONE);
412 npts = ptaGetCount(pta);
413 ptaGetPt(pta, npts / 2, NULL, &midy);
414 numaAddNumber(namidy, midy);
420 namidys = numaSortByIndex(namidy, namidysi);
421 nacurves = numaSortByIndex(nacurve1, namidysi);
426 ptaa2 = ptaaSortByIndex(ptaa1, namidysi);
427 numaDestroy(&namidy);
428 numaDestroy(&nacurve1);
429 numaDestroy(&namidysi);
431 numaWriteDebug(
"/tmp/lept/dewdebug/midys.na", namidys);
432 numaWriteDebug(
"/tmp/lept/dewdebug/curves.na", nacurves);
433 pix1 = pixConvertTo32(pixdb);
434 ptacirc = generatePtaFilledCircle(5);
435 pixcirc = pixGenerateFromPta(ptacirc, 11, 11);
437 pixDisplayPtaaPattern(pix1, pix1, ptaa2, pixcirc, 5, 5);
440 pix2 = (rotflag) ? pixRotateOrth(pix1, 3) : pixClone(pix1);
441 pixWriteDebug(
"/tmp/lept/dewmod/0042.png", pix2, IFF_PNG);
442 pixDisplay(pix2, 0, 0);
443 ptaDestroy(&ptacirc);
444 pixDestroy(&pixcirc);
454 ptaa3 = ptaaCreate(nlines);
455 for (i = 0; i < nlines; i++) {
456 pta = ptaaGetPta(ptaa2, i,
L_CLONE);
457 numaGetFValue(namidys, i, &midy);
458 ptad = ptaCreate(nx);
459 for (j = 0; j < nx; j++) {
460 ptaGetPt(pta, j, &x, &y);
461 ptaAddPt(ptad, x, midy - y);
467 ptaaWriteDebug(
"/tmp/lept/dewdebug/ptaa3.ptaa", ptaa3, 0);
476 ptaa4 = ptaaCreate(nx);
477 famidys = numaGetFArray(namidys,
L_NOCOPY);
478 for (j = 0; j < nx; j++) {
479 pta = ptaCreate(nlines);
480 for (i = 0; i < nlines; i++) {
482 ptaaGetPt(ptaa3, i, j, NULL, &val);
483 ptaAddPt(pta, y, val);
488 ptaaWriteDebug(
"/tmp/lept/dewdebug/ptaa4.ptaa", ptaa4, 0);
499 ptaa5 = ptaaCreate(nx);
500 for (j = 0; j < nx; j++) {
501 pta = ptaaGetPta(ptaa4, j,
L_CLONE);
502 ptaGetQuadraticLSF(pta, &c2, &c1, &c0, NULL);
503 ptad = ptaCreate(ny);
504 for (i = 0; i < ny; i++) {
506 applyQuadraticFit(c2, c1, c0, y, &val);
507 ptaAddPt(ptad, y, val);
513 ptaaWriteDebug(
"/tmp/lept/dewdebug/ptaa5.ptaa", ptaa5, 0);
514 convertFilesToPdf(
"/tmp/lept/dewmod",
"004", 135, 1.0, 0, 0,
515 "Dewarp Vert Disparity",
516 "/tmp/lept/dewarp/vert_disparity.pdf");
517 lept_stderr(
"pdf file: /tmp/lept/dewarp/vert_disparity.pdf\n");
521 fpix = fpixCreate(nx, ny);
522 for (i = 0; i < ny; i++) {
523 for (j = 0; j < nx; j++) {
524 ptaaGetPt(ptaa5, j, i, NULL, &val);
525 fpixSetPixel(fpix, j, i, val);
566l_int32 i, j, h, nx, ny, sampling, ret, linear_edgefit;
567l_float32 c0, c1, cl0, cl1, cl2, cr0, cr1, cr2;
568l_float32 x, y, refl, refr;
569l_float32 val, mederr;
575PTA *pta, *ptat, *pta1, *pta2;
580 return ERROR_INT(
"dew not defined", __func__, 1);
583 return ERROR_INT(
"ptaa not defined", __func__, 1);
585 if (dew->
debug) L_INFO(
"finding horizontal disparity\n", __func__);
588 h = pixGetHeight(dew->
pixs);
591 L_INFO(
"Horiz disparity not built\n", __func__);
595 lept_mkdir(
"lept/dewdebug");
596 lept_mkdir(
"lept/dewarp");
597 ptaWriteDebug(
"/tmp/lept/dewdebug/endpts_left1.pta", ptal1, 1);
598 ptaWriteDebug(
"/tmp/lept/dewdebug/endpts_right1.pta", ptar1, 1);
613 L_INFO(
"Not enough filtered end points\n", __func__);
632 if (linear_edgefit) {
635 dew->
leftslope = lept_roundftoi(1000. * cl1);
637 L_INFO(
"Left linear LSF median error = %5.2f\n", __func__, mederr);
638 L_INFO(
"Left edge slope = %d\n", __func__, dew->
leftslope);
639 ptal3 = ptaCreate(ny);
640 for (i = 0; i < ny; i++) {
642 applyLinearFit(cl1, cl0, y, &x);
643 ptaAddPt(ptal3, x, y);
648 dew->
rightslope = lept_roundftoi(1000.0 * cr1);
650 L_INFO(
"Right linear LSF median error = %5.2f\n", __func__, mederr);
651 L_INFO(
"Right edge slope = %d\n", __func__, dew->
rightslope);
652 ptar3 = ptaCreate(ny);
653 for (i = 0; i < ny; i++) {
655 applyLinearFit(cr1, cr0, y, &x);
656 ptaAddPt(ptar3, x, y);
661 dew->
leftslope = lept_roundftoi(1000. * cl1);
662 dew->
leftcurv = lept_roundftoi(1000000. * cl2);
663 L_INFO(
"Left quad LSF median error = %5.2f\n", __func__, mederr);
664 L_INFO(
"Left edge slope = %d\n", __func__, dew->
leftslope);
665 L_INFO(
"Left edge curvature = %d\n", __func__, dew->
leftcurv);
666 ptal3 = ptaCreate(ny);
667 for (i = 0; i < ny; i++) {
669 applyQuadraticFit(cl2, cl1, cl0, y, &x);
670 ptaAddPt(ptal3, x, y);
675 dew->
rightslope = lept_roundftoi(1000.0 * cr1);
676 dew->
rightcurv = lept_roundftoi(1000000. * cr2);
677 L_INFO(
"Right quad LSF median error = %5.2f\n", __func__, mederr);
678 L_INFO(
"Right edge slope = %d\n", __func__, dew->
rightslope);
679 L_INFO(
"Right edge curvature = %d\n", __func__, dew->
rightcurv);
680 ptar3 = ptaCreate(ny);
681 for (i = 0; i < ny; i++) {
683 applyQuadraticFit(cr2, cr1, cr0, y, &x);
684 ptaAddPt(ptar3, x, y);
689 PTA *ptalft, *ptarft;
690 h = pixGetHeight(dew->
pixs);
693 if (linear_edgefit) {
694 for (i = 0; i < h; i++) {
695 applyLinearFit(cl1, cl0, i, &x);
696 ptaAddPt(pta1, x, i);
697 applyLinearFit(cr1, cr0, i, &x);
698 ptaAddPt(pta2, x, i);
701 for (i = 0; i < h; i++) {
702 applyQuadraticFit(cl2, cl1, cl0, i, &x);
703 ptaAddPt(pta1, x, i);
704 applyQuadraticFit(cr2, cr1, cr0, i, &x);
705 ptaAddPt(pta2, x, i);
708 pix1 = pixDisplayPta(NULL, dew->
pixs, pta1);
709 pixDisplayPta(pix1, pix1, pta2);
711 pixDisplay(pix1, 600, 800);
712 pixWriteDebug(
"/tmp/lept/dewmod/0051.png", pix1, IFF_PNG);
715 pix1 = pixDisplayPta(NULL, dew->
pixs, pta1);
716 pixDisplayPta(pix1, pix1, pta2);
717 ptalft = ptaTranspose(ptal3);
718 ptarft = ptaTranspose(ptar3);
720 pixDisplay(pix1, 800, 800);
721 pixWriteDebug(
"/tmp/lept/dewmod/0052.png", pix1, IFF_PNG);
722 convertFilesToPdf(
"/tmp/lept/dewmod",
"005", 135, 1.0, 0, 0,
723 "Dewarp Horiz Disparity",
724 "/tmp/lept/dewarp/horiz_disparity.pdf");
725 lept_stderr(
"pdf file: /tmp/lept/dewarp/horiz_disparity.pdf\n");
739 ptaGetPt(ptal3, ny / 2, &refl, NULL);
740 ptaGetPt(ptar3, ny / 2, &refr, NULL);
741 nald = numaCreate(ny);
742 nard = numaCreate(ny);
743 for (i = 0; i < ny; i++) {
744 ptaGetPt(ptal3, i, &x, NULL);
745 numaAddNumber(nald, refl - x);
746 ptaGetPt(ptar3, i, &x, NULL);
747 numaAddNumber(nard, refr - x);
753 ptaah = ptaaCreate(ny);
754 for (i = 0; i < ny; i++) {
756 numaGetFValue(nald, i, &val);
757 ptaAddPt(pta, refl, val);
758 numaGetFValue(nard, i, &val);
759 ptaAddPt(pta, refr, val);
760 ptaGetLinearLSF(pta, &c1, &c0, NULL);
761 ptat = ptaCreate(nx);
762 for (j = 0; j < nx; j++) {
764 applyLinearFit(c1, c0, x, &val);
765 ptaAddPt(ptat, x, val);
774 fpix = fpixCreate(nx, ny);
775 for (i = 0; i < ny; i++) {
776 for (j = 0; j < nx; j++) {
777 ptaaGetPt(ptaah, i, j, NULL, &val);
778 fpixSetPixel(fpix, j, i, val);
812l_int32 i, w, h, bx, by, nsegs, csize1, csize2;
819 if (!pixs || pixGetDepth(pixs) != 1)
820 return (
PTAA *)ERROR_PTR(
"pixs undefined or not 1 bpp", __func__, NULL);
821 pixGetDimensions(pixs, &w, &h, NULL);
823 if (debugflag) L_INFO(
"finding text line centers\n", __func__);
832 csize1 = L_MAX(15, w / 80);
833 csize2 = L_MAX(40, w / 30);
834 snprintf(buf,
sizeof(buf),
"o1.3 + c%d.1 + o%d.1 + c%d.1",
835 csize1, csize1, csize2);
836 pix1 = pixMorphSequence(pixs, buf, 0);
842 pix2 = pixMorphSequence(pix1,
"e1.50", 0);
843 pixSeedfillBinary(pix2, pix2, pix1, 8);
844 pixXor(pix2, pix2, pix1);
847 lept_mkdir(
"lept/dewmod");
848 pixWriteDebug(
"/tmp/lept/dewmod/0011.tif", pix1, IFF_TIFF_G4);
849 pixDisplayWithTitle(pix1, 0, 600,
"pix1", 1);
850 pixWriteDebug(
"/tmp/lept/dewmod/0012.tif", pix2, IFF_TIFF_G4);
851 pixDisplayWithTitle(pix2, 0, 800,
"pix2", 1);
856 boxa = pixConnComp(pix2, &pixa1, 8);
859 if (pixaGetCount(pixa1) == 0) {
867 if ((nsegs = pixaGetCount(pixa2)) == 0) {
873 pix2 = pixaDisplay(pixa2, w, h);
874 pixWriteDebug(
"/tmp/lept/dewmod/0013.tif", pix2, IFF_TIFF_G4);
875 pixDisplayWithTitle(pix2, 0, 1000,
"pix2", 1);
882 ptaa = ptaaCreate(nsegs);
883 for (i = 0; i < nsegs; i++) {
884 pixaGetBoxGeometry(pixa2, i, &bx, &by, NULL, NULL);
885 pix2 = pixaGetPix(pixa2, i,
L_CLONE);
891 pix1 = pixCreateTemplate(pixs);
892 pix2 = pixDisplayPtaa(pix1, ptaa);
893 pixWriteDebug(
"/tmp/lept/dewmod/0014.tif", pix2, IFF_PNG);
894 pixDisplayWithTitle(pix2, 0, 1200,
"pix3", 1);
963l_int32 w, n, i, index, maxlen, len;
970 if (!pixs || pixGetDepth(pixs) != 1)
971 return (
PTAA *)ERROR_PTR(
"pixs undefined or not 1 bpp", __func__, NULL);
973 return (
PTAA *)ERROR_PTR(
"ptaas undefined", __func__, NULL);
975 pixGetDimensions(pixs, &w, NULL, NULL);
976 n = ptaaGetCount(ptaas);
977 ptaad = ptaaCreate(n);
979 for (i = 0; i < n; i++) {
980 pta = ptaaGetPta(ptaas, i,
L_CLONE);
981 ptaGetRange(pta, &minx, &maxx, NULL, NULL);
982 numaAddNumber(na, maxx - minx + 1);
988 numaGetIValue(naindex, 0, &index);
989 numaGetIValue(na, index, &maxlen);
990 if (maxlen < 0.5 * w)
991 L_WARNING(
"lines are relatively short\n", __func__);
992 pta = ptaaGetPta(ptaas, index,
L_CLONE);
994 for (i = 1; i < n; i++) {
995 numaGetIValue(naindex, i, &index);
996 numaGetIValue(na, index, &len);
997 if (len < fract * maxlen)
break;
998 pta = ptaaGetPta(ptaas, index,
L_CLONE);
1003 pix1 = pixCopy(NULL, pixs);
1004 pix2 = pixDisplayPtaa(pix1, ptaad);
1005 pixDisplayWithTitle(pix2, 0, 200,
"pix4", 1);
1011 numaDestroy(&naindex);
1123l_float32 ymin, ymax, xvall, xvalr, yvall, yvalr;
1124PTA *ptal1, *ptar1, *ptal2, *ptar2;
1127 return ERROR_INT(
"ptal or ptar not defined", __func__, 1);
1128 *pptalf = *pptarf = NULL;
1131 w = pixGetWidth(dew->
pixs);
1132 ptaGetMinMax(ptal, NULL, &ymin, NULL, NULL);
1133 ptaGetMinMax(ptar, NULL, NULL, NULL, &ymax);
1134 n = ptaGetCount(ptal);
1135 ptal1 = ptaCreate(n);
1136 ptar1 = ptaCreate(n);
1137 for (i = 0; i < n; i++) {
1138 ptaGetPt(ptal, i, &xvall, &yvall);
1139 ptaGetPt(ptar, i, &xvalr, &yvalr);
1140 if (yvall < ymin + 0.20 * (w - ymin) &&
1141 yvalr > 0.80 * ymax) {
1142 ptaAddPt(ptal1, xvall, yvall);
1143 ptaAddPt(ptar1, xvalr, yvalr);
1147 ptaWriteDebug(
"/tmp/lept/dewdebug/endpts_left2.pta", ptal1, 1);
1148 ptaWriteDebug(
"/tmp/lept/dewdebug/endpts_right2.pta", ptar1, 1);
1151 n = L_MIN(ptaGetCount(ptal1), ptaGetCount(ptar1));
1152 if (n < MinLinesForHoriz1 - 2) {
1155 L_INFO(
"First filter: only %d endpoints; needed 8\n", __func__, n);
1164 if (!ptal2 || !ptar2) {
1167 L_INFO(
"Second filter: too few endpoints left after outliers removed\n",
1172 ptaWriteDebug(
"/tmp/lept/dewdebug/endpts_left3.pta", ptal2, 1);
1173 ptaWriteDebug(
"/tmp/lept/dewdebug/endpts_right3.pta", ptar2, 1);
1204l_int32 i, n, nu, nd;
1205l_float32 rval, xval, yval, delta;
1206PTA *ptau1, *ptau2, *ptad1, *ptad2;
1209 return (
PTA *)ERROR_PTR(
"ptas not defined", __func__, NULL);
1211 delta = AllowedWidthFract * w;
1212 n = ptaGetCount(ptas);
1215 ptau1 = ptaSelectRange(ptas, 0, n / 2);
1216 ptaGetRankValue(ptau1, 0.5, NULL,
L_SORT_BY_Y, &rval);
1217 nu = ptaGetCount(ptau1);
1218 ptau2 = ptaCreate(nu);
1219 for (i = 0; i < nu; i++) {
1220 ptaGetPt(ptau1, i, &xval, &yval);
1221 if (L_ABS(rval - yval) <= delta)
1222 ptaAddPt(ptau2, xval, yval);
1225 if (ptaGetCount(ptau2) < MinLinesForHoriz2) {
1227 L_INFO(
"Second filter: upper set is too small after outliers removed\n",
1233 ptad1 = ptaSelectRange(ptas, n / 2 + 1, -1);
1234 ptaGetRankValue(ptad1, 0.5, NULL,
L_SORT_BY_Y, &rval);
1235 nd = ptaGetCount(ptad1);
1236 ptad2 = ptaCreate(nd);
1237 for (i = 0; i < nd; i++) {
1238 ptaGetPt(ptad1, i, &xval, &yval);
1239 if (L_ABS(rval - yval) <= delta)
1240 ptaAddPt(ptad2, xval, yval);
1243 if (ptaGetCount(ptad2) < MinLinesForHoriz2) {
1246 L_INFO(
"Second filter: lower set is too small after outliers removed\n",
1251 ptaJoin(ptau2, ptad2, 0, -1);
1484 l_float32 fractthresh,
1487l_int32 i, j, x, n1, n2, nb, ne, count, w, h, ival, prev;
1488l_int32 istart, iend, first, last, x0, x1, nx, ny;
1489l_float32 fract, delta, sum, aveval, fval, del, denom;
1490l_float32 ca, cb, cc, cd, ce, y;
1494NUMA *na1, *na2, *na3, *na4, *nasum;
1500 return ERROR_INT(
"dew not defined", __func__, 1);
1502 return ERROR_INT(
"invalid vert or horiz disparity model", __func__, 1);
1503 if (!pixb || pixGetDepth(pixb) != 1)
1504 return ERROR_INT(
"pixb not defined or not 1 bpp", __func__, 1);
1506 if (dew->
debug) L_INFO(
"finding slope horizontal disparity\n", __func__);
1509 pix1 = pixMorphSequence(pixb,
"o1.10", 0);
1510 pixDisplay(pix1, 100, 100);
1511 boxa1 = pixConnCompBB(pix1, 4);
1514 nb = boxaGetCount(boxa2);
1515 lept_stderr(
"number of components: %d\n", nb);
1516 boxaDestroy(&boxa1);
1519 na1 = numaCreate(0);
1520 numaSetParameters(na1, 0, 25);
1521 pixGetDimensions(pixb, &w, &h, NULL);
1522 for (x = 0; x + 50 < w; x += 25) {
1523 box = boxCreate(x, 0, 50, h);
1524 boxaContainedInBoxCount(boxa2, box, &count);
1525 numaAddNumber(na1, count);
1529 lept_mkdir(
"lept/dew");
1530 gplotSimple1(na1, GPLOT_PNG,
"/tmp/lept/dew/0091", NULL);
1531 lept_mv(
"/tmp/lept/dew/0091.png",
"lept/dewmod", NULL, NULL);
1532 pixWriteDebug(
"/tmp/lept/dewmod/0090.png", pix1, IFF_PNG);
1535 boxaDestroy(&boxa2);
1539 n1 = numaGetCount(na1);
1543 for (i = 0; i < n1; i++) {
1544 numaGetIValue(na1, i, &ival);
1557 for (i = n1 - 1; i >= 0; i--) {
1558 numaGetIValue(na1, i, &ival);
1568 na2 = numaClipToInterval(na1, istart, iend);
1570 n2 = numaGetCount(na2);
1571 delta = (parity == 0) ? last - first : first - last;
1572 denom = L_MAX(1.0, (l_float32)(L_MIN(first, last)));
1573 fract = (l_float32)delta / denom;
1575 L_INFO(
"Slope-disparity: first = %d, last = %d, fract = %7.3f\n",
1576 __func__, first, last, fract);
1577 gplotSimple1(na2, GPLOT_PNG,
"/tmp/lept/dew/0092", NULL);
1578 lept_mv(
"/tmp/lept/dew/0092.png",
"lept/dewmod", NULL, NULL);
1580 if (fract < fractthresh) {
1581 L_INFO(
"Small slope-disparity: first = %d, last = %d, fract = %7.3f\n",
1582 __func__, first, last, fract);
1590 numaGetSumOnInterval(na2, 0, ne / 2 - 1, &sum);
1592 numaGetSumOnInterval(na2, ne / 2, ne - 1, &sum);
1593 denom = L_MAX(1.0, (l_float32)(ne / 2));
1594 aveval = sum / denom;
1595 na3 = numaMakeConstant(aveval, n2);
1596 numaArithOp(na2, na2, na3, L_ARITH_DIVIDE);
1599 L_INFO(
"Average background density: %5.1f\n", __func__, aveval);
1600 gplotSimple1(na2, GPLOT_PNG,
"/tmp/lept/dew/0093", NULL);
1601 lept_mv(
"/tmp/lept/dew/0093.png",
"lept/dewmod", NULL, NULL);
1605 pta1 = numaConvertToPta1(na2);
1606 ptaWriteStream(stderr, pta1, 0);
1608 ptaGetQuarticLSF(pta1, &ca, &cb, &cc, &cd, &ce, &na3);
1609 ptaGetArrays(pta1, &na4, NULL);
1611 gplot = gplotSimpleXY1(na4, na3, GPLOT_LINES, GPLOT_PNG,
1612 "/tmp/lept/dew/0094", NULL);
1613 gplotDestroy(&gplot);
1614 lept_mv(
"/tmp/lept/dew/0094.png",
"lept/dewmod", NULL, NULL);
1620 nasum = numaMakeConstant(0, w);
1622 for (i = n2 - 1; i >= 0; i--) {
1623 numaGetFValue(na3, i, &fval);
1624 if (fval < 1.0)
break;
1626 numaGetIValue(na4, i + 1, &x0);
1627 numaGetIValue(na4, n2 - 1, &x1);
1628 numaSetParameters(nasum, x0, 1);
1630 for (x = x0; x < x1; x++) {
1631 applyQuarticFit(ca, cb, cc, cd, ce, (l_float32)x, &y);
1633 numaReplaceNumber(nasum, x, sum);
1635 for (x = x1; x < w; x++)
1636 numaReplaceNumber(nasum, x, sum);
1638 for (i = 0; i < n2; i++) {
1639 numaGetFValue(na3, i, &fval);
1640 if (fval < 1.0)
break;
1642 numaGetIValue(na4, 0, &x0);
1643 numaGetIValue(na4, i - 1, &x1);
1644 numaSetParameters(nasum, x0, 1);
1646 for (x = x1; x >= x0; x--) {
1647 applyQuarticFit(ca, cb, cc, cd, ce, (l_float32)x, &y);
1649 numaReplaceNumber(nasum, x, sum);
1651 for (x = x0; x >= 0; x--)
1652 numaReplaceNumber(nasum, x, sum);
1658 fpix = fpixCreate(nx, ny);
1659 del = (l_float32)w / (l_float32)nx;
1660 for (i = 0; i < ny; i++) {
1661 for (j = 0; j < nx; j++) {
1663 numaGetFValue(nasum, x, &fval);
1664 fpixSetPixel(fpix, j, i, fval);
1673 numaDestroy(&nasum);
1712 const char *debugfile)
1715l_int32 i, j, bx, by, ret, nlines;
1717PIX *pixs, *pixh, *pixv, *pix, *pix1, *pix2;
1723 return ERROR_INT(
"dew not defined", __func__, 1);
1725 L_WARNING(
"opensize should be >= 3; setting to 8\n", __func__);
1729 dew->
debug = (debugfile) ? 1 : 0;
1733 lept_rmdir(
"lept/dewline");
1734 lept_mkdir(
"lept/dewline");
1735 lept_rmdir(
"lept/dewmod");
1736 lept_mkdir(
"lept/dewmod");
1737 lept_mkdir(
"lept/dewarp");
1738 pixDisplayWithTitle(pixs, 0, 0,
"pixs", 1);
1739 pixWriteDebug(
"/tmp/lept/dewline/001.png", pixs, IFF_PNG);
1753 snprintf(buf,
sizeof(buf),
"d1.3 + c%d.1 + o%d.1", opensize - 2, opensize);
1754 pixh = pixMorphSequence(pixs, buf, 0);
1755 snprintf(buf,
sizeof(buf),
"d3.1 + c1.%d + o1.%d", opensize - 2, opensize);
1756 pix1 = pixMorphSequence(pixs, buf, 0);
1757 pixv = pixRotateOrth(pix1, 1);
1758 pixa1 = pixaCreate(2);
1766 for (i = 0; i < 2; i++) {
1767 pix = pixaGetPix(pixa1, i,
L_CLONE);
1768 pixDisplay(pix, 0, 900);
1769 boxa = pixConnComp(pix, &pixa2, 8);
1770 nlines = boxaGetCount(boxa);
1772 if (nlines < dew->minlines) {
1773 L_WARNING(
"only found %d lines\n", __func__, nlines);
1775 pixaDestroy(&pixa1);
1780 ptaa1 = ptaaCreate(nlines);
1781 for (j = 0; j < nlines; j++) {
1782 pixaGetBoxGeometry(pixa2, j, &bx, &by, NULL, NULL);
1783 pix1 = pixaGetPix(pixa2, j,
L_CLONE);
1788 pixaDestroy(&pixa2);
1790 pix1 = pixConvertTo32(pix);
1791 pix2 = pixDisplayPtaa(pix1, ptaa1);
1792 snprintf(buf,
sizeof(buf),
"/tmp/lept/dewline/%03d.png", 2 + 2 * i);
1793 pixWriteDebug(buf, pix2, IFF_PNG);
1802 pix1 = pixConvertTo32(pix);
1803 pix2 = pixDisplayPtaa(pix1, ptaa2);
1804 snprintf(buf,
sizeof(buf),
"/tmp/lept/dewline/%03d.png", 3 + 2 * i);
1805 pixWriteDebug(buf, pix2, IFF_PNG);
1809 ptaaDestroy(&ptaa1);
1810 nlines = ptaaGetCount(ptaa2);
1811 if (nlines < dew->minlines) {
1813 ptaaDestroy(&ptaa2);
1814 L_WARNING(
"%d lines: too few to build model\n", __func__, nlines);
1828 L_WARNING(
"horizontal disparity not built\n", __func__);
1830 L_INFO(
"hsuccess = 1\n", __func__);
1834 lept_mv(
"/tmp/lept/dewarp/vert_disparity.pdf",
1835 "lept/dewarp",
"horiz_disparity.pdf", NULL);
1841 L_WARNING(
"vertical disparity not built\n", __func__);
1843 L_INFO(
"vsuccess = 1\n", __func__);
1845 ptaaDestroy(&ptaa2);
1848 pixaDestroy(&pixa1);
1853 dewarpPopulateFullRes(dew, NULL, 0, 0);
1854 pix1 = fpixRenderContours(dew->
fullvdispar, 3.0f, 0.15f);
1855 pixWriteDebug(
"/tmp/lept/dewline/006.png", pix1, IFF_PNG);
1856 pixDisplay(pix1, 1000, 0);
1860 pix1 = fpixRenderContours(dew->
fullhdispar, 3.0f, 0.15f);
1861 pixWriteDebug(
"/tmp/lept/dewline/007.png", pix1, IFF_PNG);
1862 pixDisplay(pix1, 1000, 0);
1865 convertFilesToPdf(
"/tmp/lept/dewline", NULL, 135, 1.0, 0, 0,
1866 "Dewarp Build Line Model", debugfile);
1867 lept_stderr(
"pdf file: %s\n", debugfile);