/ocr/ocrservice/src/com/googlecode/eyesfree/textdetect/HydrogenTextDetector.java
Java | 260 lines | 141 code | 76 blank | 43 comment | 4 complexity | 08598ac417933171ef94c14bb102489c MD5 | raw file
1/* 2 * Copyright (C) 2011 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17package com.googlecode.eyesfree.textdetect; 18 19import android.os.Environment; 20 21import com.googlecode.leptonica.android.Pix; 22import com.googlecode.leptonica.android.Pixa; 23 24/** 25 * @author alanv@google.com (Alan Viverette) 26 */ 27public class HydrogenTextDetector { 28 private final int mNative; 29 30 static { 31 System.loadLibrary("lept"); 32 System.loadLibrary("hydrogen"); 33 } 34 35 private Parameters mParams; 36 37 public HydrogenTextDetector() { 38 mNative = nativeConstructor(); 39 40 mParams = new Parameters(); 41 setParameters(mParams); 42 } 43 44 public void setSize(int width, int height) { 45 // TODO(alanv): Set up native buffers 46 } 47 48 @Override 49 protected void finalize() throws Throwable { 50 try { 51 nativeDestructor(mNative); 52 } finally { 53 super.finalize(); 54 } 55 } 56 57 public void setParameters(Parameters params) { 58 mParams = params; 59 60 nativeSetParameters(mNative, mParams); 61 } 62 63 public Parameters getParameters() { 64 return mParams; 65 } 66 67 public Pixa getTextAreas() { 68 int nativePixa = nativeGetTextAreas(mNative); 69 70 if (nativePixa == 0) { 71 return null; 72 } 73 74 int width = nativeGetSourceWidth(mNative); 75 int height = nativeGetSourceHeight(mNative); 76 77 return new Pixa(nativePixa, width, height); 78 } 79 80 public float getSkewAngle() { 81 return nativeGetSkewAngle(mNative); 82 } 83 84 public float[] getTextConfs() { 85 return nativeGetTextConfs(mNative); 86 } 87 88 public Pix getSourceImage() { 89 int nativePix = nativeGetSourceImage(mNative); 90 91 if (nativePix == 0) { 92 return null; 93 } 94 95 return new Pix(nativePix); 96 } 97 98 /** 99 * Sets the text detection source image to be a clone of the supplied source 100 * image. The supplied image may be recycled after calling this method. 101 * 102 * @param pixs The source image on which to perform text detection. 103 */ 104 public void setSourceImage(Pix pixs) { 105 nativeSetSourceImage(mNative, pixs.getNativePix()); 106 } 107 108 public void detectText() { 109 nativeDetectText(mNative); 110 } 111 112 public void clear() { 113 nativeClear(mNative); 114 } 115 116 // ****************** 117 // * PUBLIC CLASSES * 118 // ****************** 119 120 public class Parameters { 121 public boolean debug; 122 123 public String out_dir; 124 125 // Edge-based thresholding 126 public int edge_tile_x; 127 128 public int edge_tile_y; 129 130 public int edge_thresh; 131 132 public int edge_avg_thresh; 133 134 // Skew angle correction 135 public boolean skew_enabled; 136 137 public float skew_min_angle; 138 139 public float skew_sweep_range; 140 141 public float skew_sweep_delta; 142 143 public int skew_sweep_reduction; 144 145 public int skew_search_reduction; 146 147 public float skew_search_min_delta; 148 149 // Singleton filter 150 public float single_min_aspect; 151 152 public float single_max_aspect; 153 154 public int single_min_area; 155 156 public float single_min_density; 157 158 // Quick pair filter 159 public float pair_h_ratio; 160 161 public float pair_d_ratio; 162 163 public float pair_h_dist_ratio; 164 165 public float pair_v_dist_ratio; 166 167 public float pair_h_shared; 168 169 // Cluster pair filter 170 public int cluster_width_spacing; 171 172 public float cluster_shared_edge; 173 174 public float cluster_h_ratio; 175 176 // Finalized cluster filter 177 public int cluster_min_blobs; 178 179 public float cluster_min_aspect; 180 181 public float cluster_min_fdr; 182 183 public int cluster_min_edge; 184 185 public int cluster_min_edge_avg; 186 187 public Parameters() { 188 debug = false; 189 out_dir = Environment.getExternalStorageDirectory().toString(); 190 191 // Edge-based thresholding 192 edge_tile_x = 32; 193 edge_tile_y = 64; 194 edge_thresh = 64; 195 edge_avg_thresh = 4; 196 197 // Skew angle correction 198 skew_enabled = true; 199 skew_min_angle = 1.0f; 200 skew_sweep_range = 30.0f; 201 skew_sweep_delta = 5.0f; 202 skew_sweep_reduction = 8; 203 skew_search_reduction = 4; 204 skew_search_min_delta = 0.01f; 205 206 // Singleton filter 207 single_min_aspect = 0.1f; 208 single_max_aspect = 4.0f; 209 single_min_area = 4; 210 single_min_density = 0.2f; 211 212 // Quick pair filter 213 pair_h_ratio = 1.0f; 214 pair_d_ratio = 1.5f; 215 pair_h_dist_ratio = 2.0f; 216 pair_v_dist_ratio = 0.25f; 217 pair_h_shared = 0.25f; 218 219 // Cluster pair filter 220 cluster_width_spacing = 2; 221 cluster_shared_edge = 0.5f; 222 cluster_h_ratio = 1.0f; 223 224 // Finalized cluster filter 225 cluster_min_blobs = 5; 226 cluster_min_aspect = 2; 227 cluster_min_fdr = 2.5f; 228 cluster_min_edge = 32; 229 cluster_min_edge_avg = 1; 230 } 231 } 232 233 // ****************** 234 // * NATIVE METHODS * 235 // ****************** 236 237 private static native int nativeConstructor(); 238 239 private static native void nativeDestructor(int nativePtr); 240 241 private static native void nativeSetParameters(int nativePtr, Parameters params); 242 243 private static native int nativeGetTextAreas(int nativePtr); 244 245 private static native float nativeGetSkewAngle(int nativePtr); 246 247 private static native int nativeGetSourceWidth(int nativePtr); 248 249 private static native int nativeGetSourceHeight(int nativePtr); 250 251 private static native float[] nativeGetTextConfs(int nativePtr); 252 253 private static native int nativeGetSourceImage(int nativePtr); 254 255 private static native int nativeSetSourceImage(int nativePtr, int nativePix); 256 257 private static native void nativeDetectText(int nativePtr); 258 259 private static native void nativeClear(int nativePtr); 260}