From 3f9e01f8c303802cb8326d88d415d1ccc8221174 Mon Sep 17 00:00:00 2001 From: Robert Theis Date: Fri, 1 Aug 2014 22:05:19 -0700 Subject: [PATCH] Add pixContrastNorm() for adaptive contrast normalization --- .../utilities.cpp | 16 ++++++ .../leptonica/android/AdaptiveMap.java | 53 +++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/tess-two/jni/com_googlecode_leptonica_android/utilities.cpp b/tess-two/jni/com_googlecode_leptonica_android/utilities.cpp index ec7a5ff2e..793d5f42c 100644 --- a/tess-two/jni/com_googlecode_leptonica_android/utilities.cpp +++ b/tess-two/jni/com_googlecode_leptonica_android/utilities.cpp @@ -42,6 +42,22 @@ jint Java_com_googlecode_leptonica_android_AdaptiveMap_nativeBackgroundNormMorph return (jint) pixd; } +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; +} + /************ * Binarize * ************/ diff --git a/tess-two/src/com/googlecode/leptonica/android/AdaptiveMap.java b/tess-two/src/com/googlecode/leptonica/android/AdaptiveMap.java index 3e7c8c208..bb08440db 100644 --- a/tess-two/src/com/googlecode/leptonica/android/AdaptiveMap.java +++ b/tess-two/src/com/googlecode/leptonica/android/AdaptiveMap.java @@ -97,10 +97,63 @@ public static Pix backgroundNormMorph( return new Pix(nativePix); } + /** + * Adaptively attempts to expand the contrast to the full dynamic range in + * each tile. + *

+ * Notes: + *

    + *
  1. If the contrast in a tile is smaller than minDiff, it uses the min + * and max pixel values from neighboring tiles. It also can use + * convolution to smooth the min and max values from neighboring tiles. + * After all that processing, it is possible that the actual pixel values + * in the tile are outside the computed [min ... max] range for local + * contrast normalization. Such pixels are taken to be at either 0 (if + * below the min) or 255 (if above the max). + *
  2. sizeX and sizeY give the tile size; they are typically at least 20. + *
  3. minDiff is used to eliminate results for tiles where it is likely + * that either fg or bg is missing. A value around 50 or more is + * reasonable. + *
  4. The full width and height of the convolution kernel are (2 * smoothx + * + 1) and (2 * smoothy + 1). Some smoothing is typically useful, and we + * limit the smoothing half-widths to the range from 0 to 8. Use 0 for no + * smoothing. + *
  5. A linear TRC (gamma = 1.0) is applied to increase the contrast in + * each tile. The result can subsequently be globally corrected, by + * applying pixGammaTRC() with arbitrary values of gamma and the 0 and 255 + * points of the mapping. + *
+ * + * @param pixs A source pix image + * @param sizeX Tile width + * @param sizeY Tile height + * @param minDiff Minimum difference to accept as valid + * @param smoothX Half-width of convolution kernel applied to min and max + * arrays + * @param smoothY Half-height of convolution kernel applied to min and max + * arrays + */ + public static Pix pixContrastNorm( + Pix pixs, int sizeX, int sizeY, int minDiff, int smoothX, int smoothY) { + if (pixs == null) + throw new IllegalArgumentException("Source pix must be non-null"); + + int nativePix = nativePixContrastNorm( + pixs.mNativePix, sizeX, sizeY, minDiff, smoothX, smoothY); + + if (nativePix == 0) + throw new RuntimeException("Failed to normalize image contrast"); + + return new Pix(nativePix); + } + // *************** // * NATIVE CODE * // *************** private static native int nativeBackgroundNormMorph( int nativePix, int reduction, int size, int bgval); + + private static native int nativePixContrastNorm( + int nativePix, int sizeX, int sizeY, int minDiff, int smoothX, int smoothY); }