/* load PNG, TIFF, JPG, GIF or BMP to PIX datastructure. The actual supported * formats depends on how the leptonica was compiled */ PIX *loadimage(char *filename){ PIX *pix, *pixt; int format, bpp; format=fileformat(filename); // In later versions of leptonica you will have to do this // pixReadHeader(filename, format,NULL,NULL,NULL,bpp,NULL); if(format!=IFF_PNG && format!=IFF_JFIF_JPEG && format!=IFF_TIFF && format!= IFF_GIF && format!=7 && format!=8){ dfprintf(stderr,"Not recognised file format %i", format); return NULL; } if ((pix = pixRead(filename)) == NULL) return NULL; /* TODO: convert image to 1-bpp 300dpi regardless of scan */ bpp=pixGetDepth(pix); if(bpp>1){ /* printf("Bits per pixel=%i",bpp); exit(1); */ //pixThresholdForFgBg(pix,5,100,NULL,NULL); //pixContrastTRC(pix, pix, 1000); pixt = pixContrastNorm(NULL, pix, 10, 10, 40, 2, 2); pixDestroy(&pix); pix = pixGammaTRC(NULL, pixt, 1.5, 50, 235); pixt=pixThresholdToBinary(pix, 200); //pixt=pixThreshold8(pix,1,1,0); pixDestroy(&pix); pix=pixt; } return pix; }
jint Java_com_googlecode_leptonica_android_AdaptiveMap_nativePixContrastNorm(JNIEnv *env, jclass clazz, jint nativePix, jint sizeX, jint sizeY, jint minDiff, jint smoothX, jint smoothY) { PIX *pixs = (PIX *) nativePix; PIX *pixd = pixContrastNorm(NULL, pixs, (l_int32) sizeX, (l_int32) sizeY, (l_int32) minDiff, (l_int32) smoothX, (l_int32) smoothY); return (jint) pixd; }
main(int argc, char **argv) { PIX *pixs, *pixt1, *pixt2; L_REGPARAMS *rp; if (regTestSetup(argc, argv, &rp)) return 1; pixs = pixRead("w91frag.jpg"); PixTest3(pixs, 3, 0.20, 2, 3, 0, rp); PixTest3(pixs, 6, 0.20, 100, 100, 1, rp); PixTest3(pixs, 10, 0.40, 10, 10, 2, rp); PixTest3(pixs, 10, 0.40, 20, 20, 3, rp); PixTest3(pixs, 20, 0.34, 30, 30, 4, rp); pixt1 = PixTest1(pixs, 7, 0.34, rp); pixt2 = PixTest2(pixs, 7, 0.34, 4, 4, rp); regTestComparePix(rp, pixt1, pixt2); pixDestroy(&pixt1); pixDestroy(&pixt2); /* Do combination of contrast norm and sauvola */ pixt1 = pixContrastNorm(NULL, pixs, 100, 100, 55, 1, 1); pixSauvolaBinarizeTiled(pixt1, 8, 0.34, 1, 1, NULL, &pixt2); regTestWritePixAndCheck(rp, pixt1, IFF_PNG); regTestWritePixAndCheck(rp, pixt2, IFF_PNG); pixDisplayWithTitle(pixt1, 100, 500, NULL, rp->display); pixDisplayWithTitle(pixt2, 700, 500, NULL, rp->display); pixDestroy(&pixt1); pixDestroy(&pixt2); regTestCleanup(rp); pixDestroy(&pixs); return 0; }
int main(int argc, char **argv) { char *infile; l_int32 w, d, threshval, ival, newval; l_uint32 val; PIX *pixs, *pixg, *pixg2; PIX *pix1, *pix2; PIXA *pixa; static char mainName[] = "binarize_set"; if (argc != 2) return ERROR_INT(" Syntax: binarize_set infile", mainName, 1); infile = argv[1]; pixa = pixaCreate(5); pixs = pixRead(infile); pixGetDimensions(pixs, &w, NULL, &d); pixSaveTiled(pixs, pixa, 1.0, 1, 50, 32); pixDisplay(pixs, 100, 0); #if ALL /* 1. Standard background normalization with a global threshold. */ pixg = pixConvertTo8(pixs, 0); pix1 = pixBackgroundNorm(pixg, NULL, NULL, 10, 15, 100, 50, 255, 2, 2); pix2 = pixThresholdToBinary(pix1, 160); pixWrite("/tmp/binar1.png", pix2, IFF_PNG); pixDisplay(pix2, 100, 0); pixSaveTiled(pix2, pixa, 1.0, 1, 50, 32); pixDestroy(&pixg); pixDestroy(&pix1); pixDestroy(&pix2); #endif #if ALL /* 2. Background normalization followed by Otsu thresholding. Otsu * binarization attempts to split the image into two roughly equal * sets of pixels, and it does a very poor job when there are large * amounts of dark background. By doing a background normalization * first (to get the background near 255), we remove this problem. * Then we use a modified Otsu to estimate the best global * threshold on the normalized image. */ pixg = pixConvertTo8(pixs, 0); pix1 = pixOtsuThreshOnBackgroundNorm(pixg, NULL, 10, 15, 100, 50, 255, 2, 2, 0.10, &threshval); fprintf(stderr, "thresh val = %d\n", threshval); pixSaveTiled(pix1, pixa, 1.0, 1, 50, 32); pixWrite("/tmp/binar2.png", pix1, IFF_PNG); pixDisplay(pix1, 100, 200); pixDestroy(&pixg); pixDestroy(&pix1); #endif #if ALL /* 3. Background normalization with Otsu threshold estimation and * masking for threshold selection. */ pixg = pixConvertTo8(pixs, 0); pix1 = pixMaskedThreshOnBackgroundNorm(pixg, NULL, 10, 15, 100, 50, 2, 2, 0.10, &threshval); fprintf(stderr, "thresh val = %d\n", threshval); pixSaveTiled(pix1, pixa, 1.0, 1, 50, 32); pixWrite("/tmp/binar3.png", pix1, IFF_PNG); pixDisplay(pix1, 100, 400); pixDestroy(&pixg); pixDestroy(&pix1); #endif #if ALL /* 4. Background normalization followed by Sauvola binarization */ if (d == 32) pixg = pixConvertRGBToGray(pixs, 0.2, 0.7, 0.1); else pixg = pixConvertTo8(pixs, 0); pixg2 = pixContrastNorm(NULL, pixg, 20, 20, 130, 2, 2); pixSauvolaBinarizeTiled(pixg2, 25, 0.40, 1, 1, NULL, &pix1); pixSaveTiled(pix1, pixa, 1.0, 1, 50, 32); pixWrite("/tmp/binar4.png", pix1, IFF_PNG); pixDisplay(pix1, 100, 600); pixDestroy(&pixg); pixDestroy(&pixg2); pixDestroy(&pix1); #endif #if ALL /* 5. Contrast normalization followed by background normalization, and * thresholding. */ if (d == 32) pixg = pixConvertRGBToGray(pixs, 0.2, 0.7, 0.1); else pixg = pixConvertTo8(pixs, 0); pixOtsuAdaptiveThreshold(pixg, 5000, 5000, 0, 0, 0.1, &pix1, NULL); pixGetPixel(pix1, 0, 0, &val); ival = (l_int32) val; newval = ival + (l_int32)(0.6 * (110 - ival)); fprintf(stderr, "th1 = %d, th2 = %d\n", ival, newval); pixDestroy(&pix1); pixContrastNorm(pixg, pixg, 50, 50, 130, 2, 2); pixg2 = pixBackgroundNorm(pixg, NULL, NULL, 20, 20, 70, 40, 200, 2, 2); ival = L_MIN(ival, 110); pix1 = pixThresholdToBinary(pixg2, ival); pixSaveTiled(pix1, pixa, 1.0, 1, 50, 32); pixWrite("/tmp/binar5.png", pix1, IFF_PNG); pixDisplay(pix1, 100, 800); pixDestroy(&pixg); pixDestroy(&pixg2); pixDestroy(&pix1); #endif pix1 = pixaDisplayTiledInRows(pixa, 32, w + 100, 1.0, 0, 30, 2); pixWrite("/tmp/binar6.png", pix1, IFF_PNG); pixDisplay(pix1, 1000, 0); pixDestroy(&pix1); pixaDestroy(&pixa); pixDestroy(&pixs); return 0; }