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

59
LINES

< > BotCompany Repo | #1006555 // autoCropOfBWImage_blackBG - returns Rect

JavaX fragment (include)

static float autoCropOfBWImage_blackBG_threshold = 0.2f;

static Rect autoCropOfBWImage_blackBG(BWImage img) {
  ret autoCropOfBWImage(img, new Rect(0, 0, img.getWidth(), img.getHeight()));
}

static Rect autoCropOfBWImage_blackBG(BWImage img, Rect r) {
  ret autoCropOfBWImage(img, r, autoCropOfBWImage_blackBG_threshold);
}

// r = initial rectangle
static Rect autoCropOfBWImage_blackBG(BWImage img, Rect r, float threshold) {
  int x1 = r.x, y1 = r.y, x2 = r.x2(), y2 = r.y2();

  // left side

  end1:
  while (x1 < x2) {
    for (int y = y1; y < y2; y++)
      if (img.getPixel(x1, y) >= threshold)
        break end1;
    x1++;
  }

  // top side

  end2:
  while (y1 < y2) {
    for (int x = x1; x < x2; x++)
      if (img.getPixel(x, y1) >= threshold)
        break end2;
    y1++;
  }

  // right side

  end3:
  while (x2 > x1) {
    for (int y = y1; y < y2; y++)
      if (img.getPixel(x2-1, y) >= threshold)
        break end3;
    x2--;
  }

  // bottom side

  end4:
  while (y2 > y1) {
    for (int x = x1; x < x2; x++)
      if (img.getPixel(x, y2-1) >= threshold)
        break end4;
    y2--;
  }

  if (x2 <= x1 || y2 <= y1)
    return r;
  else
    return new Rect(x1, y1, x2 - x1, y2 - y1);
}

Author comment

Began life as a copy of #1005705

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1006555
Snippet name: autoCropOfBWImage_blackBG - returns Rect
Eternal ID of this version: #1006555/1
Text MD5: 8f2b5d25c0db6394d6e5090ff2c6f274
Author: stefan
Category: javax / ocr
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-01-16 12:31:56
Source code size: 1274 bytes / 59 lines
Pitched / IR pitched: No / No
Views / Downloads: 499 / 519
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)