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

59
LINES

< > TinyBrain | #1005932 - Find selection boxes in ImageSurface by OCR [v3, WORKS for "clean" boxes]

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

Libraryless. Click here for Pure Java version (4600L/31K/105K).

!752

static Color inner = Color.green, outer = Color.white;

p {
  ShootLoop loop = new ShootLoop {
    void recognize() {
      selections.clear();
      float xInner = new RGB(inner).getBrightness();
      float xOuter = new RGB(outer).getBrightness();
      print("xInner=" + xInner + ", xOuter=" + xOuter);
      BWImage screen = screen();
      for (int y = 1; y < screenHeight-1; y++)
        for (int x = 1; x < screenWidth-1; x++)
          if (screen.getPixel(x, y) == xInner) {
            Rect r = followRect(x, y, xInner);
            if (r != null) {
              //print("Found inner: " + r);
              if (checkRect(x-1, y-1, r.w+2, r.h+2, xOuter))
                selections.add(new Rect(x-1, y-1, r.w+2, r.h+2));
            }
            while (x < screenWidth && screen.getPixel(x, y) == xInner) ++x;
          }

      printStruct(n(l(selections), "boxes") + " found", selections);
      showThumbnail();
    }
  
    Rect followRect(int x1, int y1, float c) {
      BWImage screen = screen();
      int x = x1, y = y1;
      while (x+1 < screenWidth && screen.getPixel(x+1, y) == c
        && screen.getPixel(x+1, y+1) != c) ++x;
      if (x+1 < screenWidth && screen.getPixel(x+1, y) == c) ++x;
      while (y+1 < screenHeight && screen.getPixel(x, y+1) == c) ++y;
      int x2 = x, y2 = y;
      //print("Got " + c + " " + (x2-x1) + "/" + (y2-y1) + " from " + x1 + "/" + y1);
      while (x > x1 && screen.getPixel(x-1, y) == c) --x;
      if (x != x1) null;
      while (y > y1 && screen.getPixel(x, y-1) == c) --y;
      if (y != y1) null;
      ret pointsRect(x1, y1, x2+1, y2+1);
    }
    
    bool checkRect(int x1, int y1, int w, int h, float c) {
      BWImage screen = screen();
      for y to h: for x to w:
        if (screen.getPixel(x1+x, y1) != c
          || screen.getPixel(x1, y1+y) != c
          || screen.getPixel(x1+x, y1+h-1) != c
          || screen.getPixel(x1+w-1, y1+y) != c)
          false;
      true;
    }
  };
  
  //loop.doSingle(loadBufferedImage("#1005930"));
  loop.run();
}

Author comment

Began life as a copy of #1005929

download  show line numbers  debug dex   

Travelled to 10 computer(s): aoiabmzegqzx, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, sawdedvomwva, tslmcundralx, tvejysmllsmz

No comments. add comment

Snippet ID: #1005932
Snippet name: Find selection boxes in ImageSurface by OCR [v3, WORKS for "clean" boxes]
Eternal ID of this version: #1005932/1
Text MD5: 1f4e0285615dc37b37a362337a693b2c
Transpilation MD5: b4159f97c828b15c30102a796034c369
Author: stefan
Category: javax / ocr
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-12-13 00:15:31
Source code size: 2097 bytes / 59 lines
Pitched / IR pitched: No / No
Views / Downloads: 205 / 229
Referenced in: [show]