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

72
LINES

< > TinyBrain | #1005931 - Find selection boxes in ImageSurface by OCR [v2, dev.]

JavaX source code - run with: x30.jar

!752

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

p {
  ShootLoop loop = new ShootLoop {
    void recognize() {
      selections.clear();
      float xInner = new RGB(outer).getBrightness();
      float xOuter = new RGB(outer).getBrightness();
      print("xInner=" + xInner + ", xOuter=" + xOuter);
      BWImage screen = screen();
      for (int y1 = 0; y < screenHeight-3; y1++)
        for (int x1 = 0; x < screenWidth-1; x1++)
          if (screen.getPixel(x1, y1) == xOuter
            && screen.getPixel(x1+1, y1+1) == xInner
            && screen.getPixel(x1, y1+1) == xOuter
            && screen.getPixel(x1+1, y1) == xOuter) {
            
          int x = x1, y = y1;
          while (x+1 < screenWidth
            && screen.getPixel(x+1, y) == xOuter
            && screen.getPixel(x+1, y+1) == xInner) ++x;
          ++x;
          if (!(screen.getPixel(x, y) == xOuter
            && screen.getPixel(x, y+1) == xOuter)) {
            x1 = x;
            continue;
          }
          
        && screen.getPixel(x, y+1) != 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));
      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);
            Rect r = followRect(x, y, xOuter);
            if (r != null && r.w > 1 && r.h > 1) {
              print("Found outer: " + r);
              Rect r2 = followRect(x+1, y+1, xInner);
              if (r2.w+2 == r.w && r2.h+2 == r.h)
                selections.add(r);
            }
            while (x < screenWidth && screen.getPixel(x, y) == xOuter) ++x;
          }

      printStruct(+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, y+1) != 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));
      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);
    }
  };
  
  loop.doSingle(loadBufferedImage("#1005930"));
  //loop.run();
}

Author comment

Began life as a copy of #1005929

download  show line numbers  debug dex   

Travelled to 6 computer(s): cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, mqqgnosmbjvj, tslmcundralx, tvejysmllsmz

No comments. add comment

Snippet ID: #1005931
Snippet name: Find selection boxes in ImageSurface by OCR [v2, dev.]
Eternal ID of this version: #1005931/1
Text MD5: 9c1cbcf6b9464fb34b6003e5a0340350
Author: stefan
Category: javax / ocr
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-12-12 22:30:17
Source code size: 2594 bytes / 72 lines
Pitched / IR pitched: No / No
Views / Downloads: 131 / 166
Referenced in: [show]