Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

110
LINES

< > BotCompany Repo | #1005706 // Test splitting text image into characters with auto-cropping characters

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (4188L/28K/93K).

!752

static BWImage img;
static ImageSurface is2;
static float threshold = 0.9f;

p {
  img = loadBWImage("#1005702");
  update();
}

svoid update() {
  float[] f = columnBrightnesses(img);
  //quickShowZoomedImage(img.toRGB());
  //showZoomedImage(brightnessesAsImage(f).toRGB());
  L<Streak> streaks = makeStreaks(f, threshold);
  //printStruct(f);
  //printStruct(streaks);
  print("Threshold: " + threshold + ", streaks: " + l(streaks));
  RGBImage vis = visualizeStreaks(img, streaks).toRGB();
  if (is2 == null) {
    is2 = showZoomedImage(vis);
    addToWindowAndPack(is2, liveSlider(threshold/10000f*9999f, voidfunc(float x) {
      threshold = x*10000f/9999f;
      update();
    }));
  } else
    is2.setImage(vis);
}

// f: voidfunc(float)
static JSlider liveSlider(float def, final O f) {
  final int max = 9999;
  final JSlider slider = new JSlider(0, max, iround(def*max));
  slider.addChangeListener(new ChangeListener {
    int lastValue = slider.getValue();
    public void stateChanged(ChangeEvent e) {
      int value = slider.getValue();
      if (value != lastValue) {
        lastValue = value;
        pcallF(f, value/(float) max);
      }
    }
  });
  ret slider;
}

sclass Streak {
  int from, to; // to = exclusive
  
  *() {}
  *(int *from, int *to) {}
}

static L<Streak> makeStreaks(float[] f, float threshold) {
  new L<Streak> l;
  int j = -1;
  for (int i = 0; i < l(f); i++)
    if (f[i] < threshold) { // start / continue streak
      if (j < 0) j = i; 
    } else {
      if (j >= 0) { // end streak
        l.add(new Streak(j, i));
        j = -1;
      }
    }
  if (j >= 0)
    l.add(new Streak(j, l(f)));
  ret l;
}

static float[] columnBrightnesses(BWImage img) {
  int w = img.getWidth(), h = img.getHeight();
  float[] f = new float[w];
  for x to w:
    f[x] = (float) img.clip(x, 0, 1, h).averageBrightness();
  ret f;
}

static BWImage brightnessesAsImage(float[] f) {
  int w = f.length, h = 20;
  BWImage img = new BWImage(w, h);
  for y to h: for x to w: img.setPixel(x, y, f[x]);
  ret img;
}

static float[] streaksToFloats(int w, L<Streak> l) {
  float[] f = new float[w];
  for (Streak s : l)
    for (int i = s.from; i < s.to; i++)
      f[i] = 1f;
  ret f;
}

static BWImage visualizeStreaks(BWImage img, L<Streak> streaks) {
  float gapColor = 0.5f;
  int w = img.getWidth(), h = img.getHeight();
  BWImage im = new BWImage(w, h*2, gapColor);
  float[] f = streaksToFloats(w, streaks);
  
  for x to w:
    copyBWImage(img, x, 0, im, x, (int) (f[x]*h), 1, h);
  
  // auto-crop characters  
  for (Streak s : streaks) {
    Rect r = autoCropOfBWImage(im, new Rect(s.from, h, s.to-s.from, h));
    darkenBWImagePart(im, r);
  }
  ret im;
}

Author comment

Began life as a copy of #1005703

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1005706
Snippet name: Test splitting text image into characters with auto-cropping characters
Eternal ID of this version: #1005706/1
Text MD5: 67439e2a1ae91eacb017ac8cd6f55bf5
Transpilation MD5: 5123e980abab9d9b8d3211c77c8a2e92
Author: stefan
Category: javax / ocr
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-12-04 13:21:44
Source code size: 2805 bytes / 110 lines
Pitched / IR pitched: No / No
Views / Downloads: 354 / 438
Referenced in: [show references]