88 #include <config_auto.h>
92 #include "allheaders.h"
162 l_float32 scorefract,
166 l_int32 w, h, nx, ny, i, j, thresh;
168 PIX *pixt, *pixb, *pixthresh, *pixth, *pixd;
171 if (!ppixth && !ppixd)
172 return ERROR_INT(
"neither &pixth nor &pixd defined", __func__, 1);
173 if (ppixth) *ppixth = NULL;
174 if (ppixd) *ppixd = NULL;
175 if (!pixs || pixGetDepth(pixs) != 8)
176 return ERROR_INT(
"pixs not defined or not 8 bpp", __func__, 1);
177 if (sx < 16 || sy < 16)
178 return ERROR_INT(
"sx and sy must be >= 16", __func__, 1);
182 nx = L_MAX(1, w / sx);
183 ny = L_MAX(1, h / sy);
184 smoothx = L_MIN(smoothx, (nx - 1) / 2);
185 smoothy = L_MIN(smoothy, (ny - 1) / 2);
188 for (i = 0; i < ny; i++) {
189 for (j = 0; j < nx; j++) {
199 if (smoothx > 0 || smoothy > 0)
208 pixCopyResolution(pixd, pixs);
209 for (i = 0; i < ny; i++) {
210 for (j = 0; j < nx; j++) {
280 l_float32 scorefract,
285 PIX *pixn, *pixt, *pixd;
287 if (pthresh) *pthresh = 0;
288 if (!pixs || pixGetDepth(pixs) != 8)
289 return (
PIX *)ERROR_PTR(
"pixs undefined or not 8 bpp", __func__, NULL);
290 if (pixGetColormap(pixs))
291 return (
PIX *)ERROR_PTR(
"pixs is colormapped", __func__, NULL);
292 if (sx < 4 || sy < 4)
293 return (
PIX *)ERROR_PTR(
"sx and sy must be >= 4", __func__, NULL);
294 if (mincount > sx * sy) {
295 L_WARNING(
"mincount too large for tile size\n", __func__);
296 mincount = (sx * sy) / 3;
300 mincount, bgval, smoothx, smoothy);
302 return (
PIX *)ERROR_PTR(
"pixn not made", __func__, NULL);
310 if (pixt && pthresh) {
317 return (
PIX *)ERROR_PTR(
"pixd not made", __func__, NULL);
375 l_float32 scorefract,
378 l_int32 w, h, highthresh;
380 PIX *pixn, *pixm, *pixd, *pix1, *pix2, *pix3, *pix4;
382 if (pthresh) *pthresh = 0;
383 if (!pixs || pixGetDepth(pixs) != 8)
384 return (
PIX *)ERROR_PTR(
"pixs undefined or not 8 bpp", __func__, NULL);
385 if (pixGetColormap(pixs))
386 return (
PIX *)ERROR_PTR(
"pixs is colormapped", __func__, NULL);
387 if (sx < 4 || sy < 4)
388 return (
PIX *)ERROR_PTR(
"sx and sy must be >= 4", __func__, NULL);
389 if (mincount > sx * sy) {
390 L_WARNING(
"mincount too large for tile size\n", __func__);
391 mincount = (sx * sy) / 3;
396 mincount, 255, smoothx, smoothy);
398 return (
PIX *)ERROR_PTR(
"pixn not made", __func__, NULL);
418 if (pthresh) *pthresh = val;
430 highthresh = L_MIN(256, val + 30);
439 return (
PIX *)ERROR_PTR(
"pixd not made", __func__, NULL);
486 l_int32 i, j, w, h, xrat, yrat;
487 PIX *pixth, *pixd, *tileth, *tiled, *pixt;
488 PIX **ptileth, **ptiled;
491 if (!ppixth && !ppixd)
492 return ERROR_INT(
"no outputs", __func__, 1);
493 if (ppixth) *ppixth = NULL;
494 if (ppixd) *ppixd = NULL;
495 if (!pixs || pixGetDepth(pixs) != 8)
496 return ERROR_INT(
"pixs undefined or not 8 bpp", __func__, 1);
497 if (pixGetColormap(pixs))
498 return ERROR_INT(
"pixs is cmapped", __func__, 1);
501 return ERROR_INT(
"whsize must be >= 2", __func__, 1);
502 if (w < 2 * whsize + 3 || h < 2 * whsize + 3)
503 return ERROR_INT(
"whsize too large for image", __func__, 1);
505 return ERROR_INT(
"factor must be >= 0", __func__, 1);
507 if (nx <= 1 && ny <= 1)
516 if (xrat < whsize + 2) {
517 nx = w / (whsize + 2);
518 L_WARNING(
"tile width too small; nx reduced to %d\n", __func__, nx);
520 if (yrat < whsize + 2) {
521 ny = h / (whsize + 2);
522 L_WARNING(
"tile height too small; ny reduced to %d\n", __func__, ny);
524 if (nx <= 1 && ny <= 1)
540 for (i = 0; i < ny; i++) {
541 for (j = 0; j < nx; j++) {
543 ptileth = (ppixth) ? &tileth : NULL;
544 ptiled = (ppixd) ? &tiled : NULL;
613 PIX *pixg, *pixsc, *pixm, *pixms, *pixth, *pixd;
615 if (ppixm) *ppixm = NULL;
616 if (ppixsd) *ppixsd = NULL;
617 if (ppixth) *ppixth = NULL;
618 if (ppixd) *ppixd = NULL;
619 if (!ppixm && !ppixsd && !ppixth && !ppixd)
620 return ERROR_INT(
"no outputs", __func__, 1);
621 if (!pixs || pixGetDepth(pixs) != 8)
622 return ERROR_INT(
"pixs undefined or not 8 bpp", __func__, 1);
623 if (pixGetColormap(pixs))
624 return ERROR_INT(
"pixs is cmapped", __func__, 1);
627 return ERROR_INT(
"whsize must be >= 2", __func__, 1);
628 if (w < 2 * whsize + 3 || h < 2 * whsize + 3)
629 return ERROR_INT(
"whsize too large for image", __func__, 1);
631 return ERROR_INT(
"factor must be >= 0", __func__, 1);
635 whsize + 1, whsize + 1);
642 return ERROR_INT(
"pixg and pixsc not made", __func__, 1);
645 if (ppixm || ppixth || ppixd)
647 if (ppixsd || ppixth || ppixd)
653 pixCopyResolution(pixd, pixs);
716 l_int32 i, j, w, h, tabsize, wplm, wplms, wplsd, wpld, usetab;
717 l_int32 mv, ms, var, thresh;
718 l_uint32 *datam, *datams, *datasd, *datad;
719 l_uint32 *linem, *linems, *linesd, *lined;
724 if (ppixsd) *ppixsd = NULL;
725 if (!pixm || pixGetDepth(pixm) != 8)
726 return (
PIX *)ERROR_PTR(
"pixm undefined or not 8 bpp", __func__, NULL);
727 if (pixGetColormap(pixm))
728 return (
PIX *)ERROR_PTR(
"pixm is colormapped", __func__, NULL);
729 if (!pixms || pixGetDepth(pixms) != 32)
730 return (
PIX *)ERROR_PTR(
"pixms undefined or not 32 bpp",
733 return (
PIX *)ERROR_PTR(
"factor must be >= 0", __func__, NULL);
738 usetab = (w * h > 100000) ? 1 : 0;
741 tab = (l_float32 *)LEPT_CALLOC(tabsize,
sizeof(l_float32));
742 for (i = 0; i < tabsize; i++)
743 tab[i] = sqrtf((l_float32)i);
755 wplm = pixGetWpl(pixm);
756 wplms = pixGetWpl(pixms);
757 if (ppixsd) wplsd = pixGetWpl(pixsd);
758 wpld = pixGetWpl(pixd);
759 for (i = 0; i < h; i++) {
760 linem = datam + i * wplm;
761 linems = datams + i * wplms;
762 if (ppixsd) linesd = datasd + i * wplsd;
763 lined = datad + i * wpld;
764 for (j = 0; j < w; j++) {
771 sd = sqrtf((l_float32)var);
773 thresh = (l_int32)(mv * (1.0 - factor * (1.0 - sd / 128.)));
778 if (usetab) LEPT_FREE(tab);
794 l_int32 i, j, w, h, wpls, wplt, wpld, vals, valt;
795 l_uint32 *datas, *datat, *datad, *lines, *linet, *lined;
798 if (!pixs || pixGetDepth(pixs) != 8)
799 return (
PIX *)ERROR_PTR(
"pixs undefined or not 8 bpp", __func__, NULL);
800 if (pixGetColormap(pixs))
801 return (
PIX *)ERROR_PTR(
"pixs is colormapped", __func__, NULL);
802 if (!pixth || pixGetDepth(pixth) != 8)
803 return (
PIX *)ERROR_PTR(
"pixth undefined or not 8 bpp", __func__, NULL);
810 wpls = pixGetWpl(pixs);
811 wplt = pixGetWpl(pixth);
812 wpld = pixGetWpl(pixd);
813 for (i = 0; i < h; i++) {
814 lines = datas + i * wpls;
815 linet = datat + i * wplt;
816 lined = datad + i * wpld;
817 for (j = 0; j < w; j++) {
855 l_int32 w, h, d, nx, ny;
856 PIX *pixg, *pix1, *pixd;
858 if (ppixn) *ppixn = NULL;
859 if (ppixth) *ppixth = NULL;
860 if (!pixs || (d = pixGetDepth(pixs)) < 8)
861 return (
PIX *)ERROR_PTR(
"pixs undefined or d < 8 bpp", __func__, NULL);
871 nx = L_MAX(1, (w + 125) / 250);
872 ny = L_MAX(1, (h + 125) / 250);
909 PIX *pixg, *pix1, *pixd;
911 if (!pixs || (d = pixGetDepth(pixs)) < 8)
912 return (
PIX *)ERROR_PTR(
"pixs undefined or d < 8 bpp", __func__, NULL);
922 ival = L_MIN(ival, 110);
1001 l_float32 threshdiff,
1002 l_int32 *pglobthresh,
1006 l_int32 i, thresh, n, n4, n8, mincounts, found, globthresh;
1007 l_float32 count4, count8, firstcount4, prevcount4, diff48, diff4;
1010 PIX *pix1, *pix2, *pix3;
1012 if (pglobthresh) *pglobthresh = 0;
1013 if (ppixd) *ppixd = NULL;
1014 if (!pixs || pixGetDepth(pixs) == 1)
1015 return ERROR_INT(
"pixs undefined or 1 bpp", __func__, 1);
1016 if (pixm && pixGetDepth(pixm) != 1)
1017 return ERROR_INT(
"pixm must be 1 bpp", __func__, 1);
1020 if (start <= 0) start = 80;
1021 if (end <= 0) end = 200;
1022 if (incr <= 0) incr = 10;
1023 if (thresh48 <= 0.0) thresh48 = 0.01;
1024 if (threshdiff <= 0.0) threshdiff = 0.01;
1026 return ERROR_INT(
"invalid start,end", __func__, 1);
1029 if (pixGetColormap(pixs))
1033 if (pixGetDepth(pix1) == 32)
1049 if (n4 < mincounts) {
1050 L_INFO(
"Insufficient component count: %d\n", __func__, n4);
1060 for (thresh = start, i = 0; thresh <= end; thresh += incr, i++) {
1070 gplot =
gplotCreate(
"/tmp/lept/binarize", GPLOT_PNG,
1071 "number of cc vs. threshold",
1072 "threshold",
"number of cc");
1073 gplotAddPlot(gplot, NULL, na4, GPLOT_LINES,
"plot 4cc");
1074 gplotAddPlot(gplot, NULL, na8, GPLOT_LINES,
"plot 8cc");
1081 for (i = 0; i < n; i++) {
1084 prevcount4 = firstcount4;
1088 diff48 = (count4 - count8) / firstcount4;
1089 diff4 = L_ABS(prevcount4 - count4) / firstcount4;
1094 if (diff48 < thresh48 && diff4 < threshdiff) {
1098 prevcount4 = count4;
1105 globthresh = start + i * incr;
1106 if (pglobthresh) *pglobthresh = globthresh;
1109 pixCopyResolution(*ppixd, pixs);
1111 if (debugflag)
lept_stderr(
"global threshold = %d\n", globthresh);
1116 if (debugflag)
lept_stderr(
"no global threshold found\n");
1160 l_float32 maxval, fract;
1161 NUMA *na1, *na2, *na3;
1163 if (ppixd) *ppixd = NULL;
1164 if (pnahisto) *pnahisto = NULL;
1165 if (ppixhisto) *ppixhisto = NULL;
1167 return ERROR_INT(
"&thresh not defined", __func__, 1);
1169 if (!pixs || pixGetDepth(pixs) != 8)
1170 return ERROR_INT(
"pixs undefined or not 8 bpp", __func__, 1);
1171 if (pixGetColormap(pixs))
1172 return ERROR_INT(
"pixs has colormap", __func__, 1);
1174 return ERROR_INT(
"sampling must be >= 1", __func__, 1);
1175 if (halfw <= 0) halfw = 20;
1176 if (skip <= 0) skip = 20;
1189 return ERROR_INT(
"failure to find threshold", __func__, 1);
1191 L_INFO(
"fractional area under first peak: %5.3f\n", __func__, fract);
1195 gplotSimple1(na3, GPLOT_PNG,
"/tmp/lept/histo/histo", NULL);
1196 *ppixhisto =
pixRead(
"/tmp/lept/histo/histo.png");
1202 if (*pthresh > 0 && ppixd)
PIX * pixBackgroundNormSimple(PIX *pixs, PIX *pixim, PIX *pixg)
pixBackgroundNormSimple()
PIX * pixContrastNorm(PIX *pixd, PIX *pixs, l_int32 sx, l_int32 sy, l_int32 mindiff, l_int32 smoothx, l_int32 smoothy)
pixContrastNorm()
PIX * pixBackgroundNorm(PIX *pixs, PIX *pixim, PIX *pixg, l_int32 sx, l_int32 sy, l_int32 thresh, l_int32 mincount, l_int32 bgval, l_int32 smoothx, l_int32 smoothy)
pixBackgroundNorm()
PIX * pixBackgroundNormFlex(PIX *pixs, l_int32 sx, l_int32 sy, l_int32 smoothx, l_int32 smoothy, l_int32 delta)
pixBackgroundNormFlex()
#define SET_DATA_BIT(pdata, n)
#define GET_DATA_BYTE(pdata, n)
#define SET_DATA_BYTE(pdata, n, val)
static PIX * pixApplyLocalThreshold(PIX *pixs, PIX *pixth)
pixApplyLocalThreshold()
l_ok pixSauvolaBinarizeTiled(PIX *pixs, l_int32 whsize, l_float32 factor, l_int32 nx, l_int32 ny, PIX **ppixth, PIX **ppixd)
pixSauvolaBinarizeTiled()
l_ok pixThresholdByConnComp(PIX *pixs, PIX *pixm, l_int32 start, l_int32 end, l_int32 incr, l_float32 thresh48, l_float32 threshdiff, l_int32 *pglobthresh, PIX **ppixd, l_int32 debugflag)
pixThresholdByConnComp()
static PIX * pixSauvolaGetThreshold(PIX *pixm, PIX *pixms, l_float32 factor, PIX **ppixsd)
pixSauvolaGetThreshold()
PIX * pixMaskedThreshOnBackgroundNorm(PIX *pixs, PIX *pixim, l_int32 sx, l_int32 sy, l_int32 thresh, l_int32 mincount, l_int32 smoothx, l_int32 smoothy, l_float32 scorefract, l_int32 *pthresh)
pixMaskedThreshOnBackgroundNorm()
PIX * pixSauvolaOnContrastNorm(PIX *pixs, l_int32 mindiff, PIX **ppixn, PIX **ppixth)
pixSauvolaOnContrastNorm()
l_ok pixSauvolaBinarize(PIX *pixs, l_int32 whsize, l_float32 factor, l_int32 addborder, PIX **ppixm, PIX **ppixsd, PIX **ppixth, PIX **ppixd)
pixSauvolaBinarize()
PIX * pixOtsuThreshOnBackgroundNorm(PIX *pixs, PIX *pixim, l_int32 sx, l_int32 sy, l_int32 thresh, l_int32 mincount, l_int32 bgval, l_int32 smoothx, l_int32 smoothy, l_float32 scorefract, l_int32 *pthresh)
pixOtsuThreshOnBackgroundNorm()
PIX * pixThreshOnDoubleNorm(PIX *pixs, l_int32 mindiff)
pixThreshOnDoubleNorm()
l_ok pixOtsuAdaptiveThreshold(PIX *pixs, l_int32 sx, l_int32 sy, l_int32 smoothx, l_int32 smoothy, l_float32 scorefract, PIX **ppixth, PIX **ppixd)
pixOtsuAdaptiveThreshold()
l_ok pixThresholdByHisto(PIX *pixs, l_int32 factor, l_int32 halfw, l_int32 skip, l_int32 *pthresh, PIX **ppixd, NUMA **pnahisto, PIX **ppixhisto)
pixThresholdByHisto()
l_ok pixCountConnComp(PIX *pixs, l_int32 connectivity, l_int32 *pcount)
pixCountConnComp()
PIX * pixWindowedMeanSquare(PIX *pixs, l_int32 wc, l_int32 hc, l_int32 hasborder)
pixWindowedMeanSquare()
PIX * pixBlockconv(PIX *pix, l_int32 wc, l_int32 hc)
pixBlockconv()
PIX * pixWindowedMean(PIX *pixs, l_int32 wc, l_int32 hc, l_int32 hasborder, l_int32 normflag)
pixWindowedMean()
l_ok gplotAddPlot(GPLOT *gplot, NUMA *nax, NUMA *nay, l_int32 plotstyle, const char *plotlabel)
gplotAddPlot()
l_ok gplotMakeOutput(GPLOT *gplot)
gplotMakeOutput()
GPLOT * gplotCreate(const char *rootname, l_int32 outformat, const char *title, const char *xlabel, const char *ylabel)
gplotCreate()
void gplotDestroy(GPLOT **pgplot)
gplotDestroy()
l_ok gplotSimple1(NUMA *na, l_int32 outformat, const char *outroot, const char *title)
gplotSimple1()
PIX * pixThresholdToBinary(PIX *pixs, l_int32 thresh)
pixThresholdToBinary()
PIX * pixMorphSequence(PIX *pixs, const char *sequence, l_int32 dispsep)
pixMorphSequence()
l_ok numaAddNumber(NUMA *na, l_float32 val)
numaAddNumber()
l_ok numaGetFValue(NUMA *na, l_int32 index, l_float32 *pval)
numaGetFValue()
NUMA * numaCreate(l_int32 n)
numaCreate()
void numaDestroy(NUMA **pna)
numaDestroy()
l_int32 numaGetCount(NUMA *na)
numaGetCount()
l_ok numaSetParameters(NUMA *na, l_float32 startx, l_float32 delx)
numaSetParameters()
l_ok numaGetMax(NUMA *na, l_float32 *pmaxval, l_int32 *pimaxloc)
numaGetMax()
NUMA * numaWindowedMean(NUMA *nas, l_int32 wc)
numaWindowedMean()
NUMA * numaTransform(NUMA *nas, l_float32 shift, l_float32 scale)
numaTransform()
l_ok numaFindLocForThreshold(NUMA *na, l_int32 skip, l_int32 *pthresh, l_float32 *pfract)
numaFindLocForThreshold()
l_uint32 * pixGetData(PIX *pix)
pixGetData()
void pixDestroy(PIX **ppix)
pixDestroy()
l_ok pixGetDimensions(const PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
PIX * pixCreate(l_int32 width, l_int32 height, l_int32 depth)
pixCreate()
PIX * pixClone(PIX *pixs)
pixClone()
l_ok pixSetPixel(PIX *pix, l_int32 x, l_int32 y, l_uint32 val)
pixSetPixel()
l_ok pixGetPixel(PIX *pix, l_int32 x, l_int32 y, l_uint32 *pval)
pixGetPixel()
PIX * pixRemoveBorder(PIX *pixs, l_int32 npix)
pixRemoveBorder()
PIX * pixAddMirroredBorder(PIX *pixs, l_int32 left, l_int32 right, l_int32 top, l_int32 bot)
pixAddMirroredBorder()
PIX * pixInvert(PIX *pixd, PIX *pixs)
pixInvert()
l_ok pixSetMasked(PIX *pixd, PIX *pixm, l_uint32 val)
pixSetMasked()
l_ok pixCombineMasked(PIX *pixd, PIX *pixs, PIX *pixm)
pixCombineMasked()
NUMA * pixGetGrayHistogram(PIX *pixs, l_int32 factor)
pixGetGrayHistogram()
l_ok pixSplitDistributionFgBg(PIX *pixs, l_float32 scorefract, l_int32 factor, l_int32 *pthresh, l_int32 *pfgval, l_int32 *pbgval, PIX **ppixdb)
pixSplitDistributionFgBg()
@ REMOVE_CMAP_BASED_ON_SRC
PIX * pixConvertRGBToGrayMinMax(PIX *pixs, l_int32 type)
pixConvertRGBToGrayMinMax()
PIX * pixConvertRGBToGray(PIX *pixs, l_float32 rwt, l_float32 gwt, l_float32 bwt)
pixConvertRGBToGray()
PIX * pixRemoveColormap(PIX *pixs, l_int32 type)
pixRemoveColormap()
PIX * pixConvertTo1(PIX *pixs, l_int32 threshold)
pixConvertTo1()
PIX * pixConvertTo8(PIX *pixs, l_int32 cmapflag)
pixConvertTo8()
l_ok pixTilingNoStripOnPaint(PIXTILING *pt)
pixTilingNoStripOnPaint()
PIX * pixTilingGetTile(PIXTILING *pt, l_int32 i, l_int32 j)
pixTilingGetTile()
void pixTilingDestroy(PIXTILING **ppt)
pixTilingDestroy()
l_ok pixTilingPaintTile(PIX *pixd, l_int32 i, l_int32 j, PIX *pixs, PIXTILING *pt)
pixTilingPaintTile()
PIXTILING * pixTilingCreate(PIX *pixs, l_int32 nx, l_int32 ny, l_int32 w, l_int32 h, l_int32 xoverlap, l_int32 yoverlap)
pixTilingCreate()
PIX * pixRead(const char *filename)
pixRead()
void lept_stderr(const char *fmt,...)
lept_stderr()
l_int32 lept_mkdir(const char *subdir)
lept_mkdir()