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

98
LINES

< > BotCompany Repo | #1035024 // G22RegionThinner_LookupTable

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (12716L/74K).

1  
// Lookup table to speed up the last phase of G22RegionThinner_v2.
2  
// There are 256 possible neighborhood images for a 3x3 neighborhood
3  
// excluding the central pixel.
4  
// For each of these, we store whether the central pixel should be
5  
// deleted (bit is 1 = deletable)
6  
sclass G22RegionThinner_LookupTable {
7  
  byte[] table;
8  
  
9  
  settable int lastPhaseThreshold1 = 2;
10  
  settable int lastPhaseThreshold = 5;
11  
  
12  
  static byte[] defaultTable = null/*bytesFromHex(
13  
    "80c8c84c00ce0004004e5c17000715010000dc04000000000007150101031100"
14  
  )*/;
15  
  
16  
  // for debugging
17  
  L<IImageRegion<BWImage>> regions;
18  
  int sides;
19  
  
20  
  byte[] get aka table aka getTable() {
21  
    if (table == null) {
22  
      if (defaultTable != null && lastPhaseThreshold1 == 2 && lastPhaseThreshold == 5)
23  
        table = defaultTable;
24  
      else {
25  
        table = new byte[256/8];
26  
        for imgPattern to 256:
27  
          setBit(table, imgPattern, calculatePattern(imgPattern));
28  
      }
29  
    }
30  
    ret table;
31  
  }
32  
  
33  
  S toHex() { ret bytesToHex(table()); }
34  
35  
  // imgPattern bit set = black  
36  
  bool calculatePattern(int imgPattern) {
37  
    // special case "cross" (don't delete center point)
38  
    if (imgPattern == 0b01011010) false;
39  
  
40  
    Image2B img = new(3, 3);
41  
    imgPattern = ~imgPattern;
42  
    img.setPixel(0, 0, testBit(imgPattern, 0));
43  
    img.setPixel(1, 0, testBit(imgPattern, 1));
44  
    img.setPixel(2, 0, testBit(imgPattern, 2));
45  
    img.setPixel(0, 1, testBit(imgPattern, 3));
46  
    img.setPixel(1, 1, true);
47  
    img.setPixel(2, 1, testBit(imgPattern, 4));
48  
    img.setPixel(0, 2, testBit(imgPattern, 5));
49  
    img.setPixel(1, 2, testBit(imgPattern, 6));
50  
    img.setPixel(2, 2, testBit(imgPattern, 7));
51  
    var bwImage = img.toBW();
52  
    
53  
    FastRegions_BWImage regionMaker = new(bwImage);
54  
    regionMaker.withDiagonals(true);
55  
    regionMaker.run();
56  
    
57  
    // get all the black regions out of the 3x3 image
58  
    regions = regionMaker.regions();
59  
    regions = filter(regions, r -> r.brightness() == 0);
60  
    
61  
    bool delete = false;
62  
    int pixels = 0;
63  
    
64  
    // no regions? it's a lonely pixel - keep it
65  
    if (empty(regions)) {}
66  
    else if (l(regions) == 1) {
67  
      // one region
68  
      
69  
      pixels = first(regions).numberOfPixels();
70  
      // if it's only one or two pixels, we are at the end of a line - keep
71  
      if (pixels <= lastPhaseThreshold1)
72  
        delete = false;
73  
      else {
74  
        // delete pixel if the region is small, but not too small
75  
        // (lastPhaseThreshold is 5 by default)
76  
        if (pixels < lastPhaseThreshold)
77  
          delete = true;
78  
        else if (pixels == lastPhaseThreshold) {
79  
          // if it's exactly 5 pixels, check out how far
80  
          // the region "surrounds" the pixel (does it touch 2 or 3 sides?)
81  
          // delete pixel if it is not surrounded
82  
          
83  
          sides = 4-(int)
84  
            (bwImage.getFloatPixel(1, 0)
85  
            + bwImage.getFloatPixel(0, 1)
86  
            + bwImage.getFloatPixel(2, 1)
87  
            + bwImage.getFloatPixel(1, 2));
88  
          delete = sides <= 3; // XXX - formerly: < 3
89  
        }
90  
      }
91  
    } else
92  
      // we have more than one region - pixel was
93  
      // structurally important, keep it
94  
      {}
95  
96  
    ret delete;
97  
  }
98  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035024
Snippet name: G22RegionThinner_LookupTable
Eternal ID of this version: #1035024/10
Text MD5: 45dad372e875042423b137e64c61616b
Transpilation MD5: 2e760e7e3704f1b53fe806baf1d6c4ca
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-05-03 19:25:09
Source code size: 3276 bytes / 98 lines
Pitched / IR pitched: No / No
Views / Downloads: 151 / 255
Version history: 9 change(s)
Referenced in: [show references]