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

53
LINES

< > BotCompany Repo | #1033759 // FindRegionOutline (dev.)

JavaX source code (desktop) - run with: x30.jar

Download Jar.

1  
srecord noeq FindRegionOutline(BWImage_FastRegions regions, int iRegion) {
2  
  gettable new PtBuffer outline;
3  
  int x, y, dx, dy;
4  
  int polarity;
5  
  
6  
  run {
7  
    Pt p = regions.samplePixel(iRegion);
8  
    x = p.x;
9  
    y = p.y;
10  
    
11  
    // move up until we're at the edge of the region
12  
    // probably we don't actually need to do this because of the way
13  
    // the image was scanned, but we'll do it anyway
14  
    while (y > 0 && regions.inRegion(iRegion, x, y-1))
15  
      y--;
16  
    
17  
    // found our first outline pixel with background above
18  
    outline.add(x, y);
19  
    
20  
    // remember first point
21  
    firstX = x; firstY = y;
22  
    
23  
    // start searching to the right
24  
    dx = 1; dy = 0;
25  
    int safety = (regions.getWidth()+2)*(regions.getHeight()+2);
26  
    do {
27  
      if (!findNextDirection())
28  
        break;
29  
    } while (x != firstX && y != firstY && l(outline) < safety);
30  
  }
31  
  
32  
  void findNextDirection {
33  
    // try directions (left, forward, right, turn around)
34  
    
35  
    Pt dir = turnLeft(dx, dy);
36  
    repeat 4 {
37  
      if (inRegion(x+dir.x, y+dir.y)) {
38  
        dx = dir.x; dy = dir.y;
39  
        true;
40  
      }
41  
    }
42  
    
43  
    false;
44  
  }
45  
    
46  
    // forward?
47  
    Pt left = turnLeft(dx, dy);
48  
    if (inRegion(x+left.x, y+left.y)) {
49  
      dx = left.x;
50  
      dy = left.y;
51  
    }
52  
  }
53  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): bhatertpkbcr, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1033759
Snippet name: FindRegionOutline (dev.)
Eternal ID of this version: #1033759/1
Text MD5: 06d60f28897cb895a3806e80f2c71997
Author: stefan
Category: javax / maths
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-01 23:08:54
Source code size: 1320 bytes / 53 lines
Pitched / IR pitched: No / No
Views / Downloads: 250 / 463
Referenced in: [show references]