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

89
LINES

< > BotCompany Repo | #1031361 // IntegralImage45 [rotated 45°, RGB, dev.]

JavaX fragment (include)

1  
persistable sclass OctagonalIntegralImage {
2  
  final int w, h;
3  
  
4  
  // 3 ints per pixel. data[0] is normal, data[1] rotated 45°
5  
  final int[2][] data;
6  
  
7  
  // the column sums in three directions
8  
  final int[][] columnSums = new int[3][];
9  
  
10  
  *(RGBImage img) {
11  
    w = img.w();
12  
    h = img.h();
13  
    data1 = new int[w*h*3];
14  
    int i = 0;
15  
    for x to w: {
16  
      for y to h: {
17  
        // get pixel
18  
        int rgb = img.getInt(x, y);
19  
        int r = (rgb >> 16) & 0xFF;
20  
        int g = (rgb >> 8) & 0xFF;
21  
        int b = rgb & 0xFF;
22  
        
23  
        // add and subtract
24  
        data[i] = r + sumAt(x-1, y-1) + sumAt(x-1, y+1) - sumAt(x-1, y);
25  
        data[i+1] = r + sumAt(x-1, y-1) + sumAt(x-1, y+1) - sumAt(x-1, y);
26  
        data[i+2] = r + sumAt(x-1, y-1) + sumAt(x-1, y+1) - sumAt(x-1, y);
27  
      }
28  
    }
29  
  }
30  
  
31  
  *(BufferedImage img) {
32  
    w = img.getWidth();
33  
    h = img.getHeight();
34  
    if (longMul(w, h) > 8000000) fail("Image too big: " + w + "*" + h);
35  
    int[] pixels = pixelsOfBufferedImage(img);
36  
    for i over data:
37  
      data[i] = new int[w*h*3];
38  
      
39  
    for i over columnSums:
40  
      columnSums[i] = new int[max(w, h)*2]; // TODO: find actual upper bound
41  
      
42  
    int i = 0, j = 0, sumR = 0, sumG = 0, sumB = 0;
43  
    for x to w: {
44  
      int rgb = pixels[j++];
45  
      data[i++] = (sumR += (rgb >> 16) & 0xFF);
46  
      data[i++] = (sumG += (rgb >> 8) & 0xFF);
47  
      data[i++] = (sumB += rgb & 0xFF);
48  
    }
49  
    for (int y = 1; y < h; y++) {
50  
      sumR = sumG = sumB = 0;
51  
      for x to w: {
52  
        int rgb = pixels[j++];
53  
        sumR += (rgb >> 16) & 0xFF;
54  
        sumG += (rgb >> 8) & 0xFF;
55  
        sumB += rgb & 0xFF;
56  
        data[i] = sumR + data[i-w*3];
57  
        data[i+1] = sumG + data[i-w*3+1];
58  
        data[i+2] = sumB + data[i-w*3+2];
59  
        i += 3;
60  
      }
61  
    }
62  
  }
63  
  
64  
  // gets the integral value at x/y for given RGB channel
65  
  int get(int x, int y, int channel) {
66  
    ret x < 0 || y < 0 || x >= w || y >= h ? 0
67  
      : data[(y*w+x)*3+channel];
68  
  }
69  
  
70  
  // gets sum of the 3 channels
71  
  int get(int x, int y) {
72  
    if (x < 0 || y < 0 || x >= w || y >= h) ret 0;
73  
    int i = (y*w+x)*3;
74  
    ret data[i]+data[i+1]+data[i+2];
75  
  }
76  
  
77  
  double averageBrightness() {
78  
    ret doubleRatio(get(w-1, h-1), w*h*3*255.0);
79  
  }
80  
  
81  
  toString {
82  
    ret "HexagonalIntegralImage " + w + "*" + h + ", brightness: " + averageBrightness();
83  
  }
84  
  
85  
  public int getWidth() { ret w; }
86  
  public int getHeight() { ret h; }
87  
  int w() { ret w; }
88  
  int h() { ret h; }
89  
}

Author comment

Began life as a copy of #1031360

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1031361
Snippet name: IntegralImage45 [rotated 45°, RGB, dev.]
Eternal ID of this version: #1031361/1
Text MD5: 58fec6195dec4958abc75ff4db86c94b
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-06-07 07:31:22
Source code size: 2549 bytes / 89 lines
Pitched / IR pitched: No / No
Views / Downloads: 147 / 171
Referenced in: [show references]