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

59
LINES

< > BotCompany Repo | #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).

1  
!752
2  
3  
static Color inner = Color.green, outer = Color.white;
4  
5  
p {
6  
  ShootLoop loop = new ShootLoop {
7  
    void recognize() {
8  
      selections.clear();
9  
      float xInner = new RGB(inner).getBrightness();
10  
      float xOuter = new RGB(outer).getBrightness();
11  
      print("xInner=" + xInner + ", xOuter=" + xOuter);
12  
      BWImage screen = screen();
13  
      for (int y = 1; y < screenHeight-1; y++)
14  
        for (int x = 1; x < screenWidth-1; x++)
15  
          if (screen.getPixel(x, y) == xInner) {
16  
            Rect r = followRect(x, y, xInner);
17  
            if (r != null) {
18  
              //print("Found inner: " + r);
19  
              if (checkRect(x-1, y-1, r.w+2, r.h+2, xOuter))
20  
                selections.add(new Rect(x-1, y-1, r.w+2, r.h+2));
21  
            }
22  
            while (x < screenWidth && screen.getPixel(x, y) == xInner) ++x;
23  
          }
24  
25  
      printStruct(n(l(selections), "boxes") + " found", selections);
26  
      showThumbnail();
27  
    }
28  
  
29  
    Rect followRect(int x1, int y1, float c) {
30  
      BWImage screen = screen();
31  
      int x = x1, y = y1;
32  
      while (x+1 < screenWidth && screen.getPixel(x+1, y) == c
33  
        && screen.getPixel(x+1, y+1) != c) ++x;
34  
      if (x+1 < screenWidth && screen.getPixel(x+1, y) == c) ++x;
35  
      while (y+1 < screenHeight && screen.getPixel(x, y+1) == c) ++y;
36  
      int x2 = x, y2 = y;
37  
      //print("Got " + c + " " + (x2-x1) + "/" + (y2-y1) + " from " + x1 + "/" + y1);
38  
      while (x > x1 && screen.getPixel(x-1, y) == c) --x;
39  
      if (x != x1) null;
40  
      while (y > y1 && screen.getPixel(x, y-1) == c) --y;
41  
      if (y != y1) null;
42  
      ret pointsRect(x1, y1, x2+1, y2+1);
43  
    }
44  
    
45  
    bool checkRect(int x1, int y1, int w, int h, float c) {
46  
      BWImage screen = screen();
47  
      for y to h: for x to w:
48  
        if (screen.getPixel(x1+x, y1) != c
49  
          || screen.getPixel(x1, y1+y) != c
50  
          || screen.getPixel(x1+x, y1+h-1) != c
51  
          || screen.getPixel(x1+w-1, y1+y) != c)
52  
          false;
53  
      true;
54  
    }
55  
  };
56  
  
57  
  //loop.doSingle(loadBufferedImage("#1005930"));
58  
  loop.run();
59  
}

Author comment

Began life as a copy of #1005929

download  show line numbers  debug dex  old transpilations   

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

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: 493 / 574
Referenced in: [show references]