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.

srecord noeq FindRegionOutline(BWImage_FastRegions regions, int iRegion) {
  gettable new PtBuffer outline;
  int x, y, dx, dy;
  int polarity;
  
  run {
    Pt p = regions.samplePixel(iRegion);
    x = p.x;
    y = p.y;
    
    // move up until we're at the edge of the region
    // probably we don't actually need to do this because of the way
    // the image was scanned, but we'll do it anyway
    while (y > 0 && regions.inRegion(iRegion, x, y-1))
      y--;
    
    // found our first outline pixel with background above
    outline.add(x, y);
    
    // remember first point
    firstX = x; firstY = y;
    
    // start searching to the right
    dx = 1; dy = 0;
    int safety = (regions.getWidth()+2)*(regions.getHeight()+2);
    do {
      if (!findNextDirection())
        break;
    } while (x != firstX && y != firstY && l(outline) < safety);
  }
  
  void findNextDirection {
    // try directions (left, forward, right, turn around)
    
    Pt dir = turnLeft(dx, dy);
    repeat 4 {
      if (inRegion(x+dir.x, y+dir.y)) {
        dx = dir.x; dy = dir.y;
        true;
      }
    }
    
    false;
  }
    
    // forward?
    Pt left = turnLeft(dx, dy);
    if (inRegion(x+left.x, y+left.y)) {
      dx = left.x;
      dy = left.y;
    }
  }
}

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: 249 / 461
Referenced in: