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)

persistable sclass OctagonalIntegralImage {
  final int w, h;
  
  // 3 ints per pixel. data[0] is normal, data[1] rotated 45°
  final int[2][] data;
  
  // the column sums in three directions
  final int[][] columnSums = new int[3][];
  
  *(RGBImage img) {
    w = img.w();
    h = img.h();
    data1 = new int[w*h*3];
    int i = 0;
    for x to w: {
      for y to h: {
        // get pixel
        int rgb = img.getInt(x, y);
        int r = (rgb >> 16) & 0xFF;
        int g = (rgb >> 8) & 0xFF;
        int b = rgb & 0xFF;
        
        // add and subtract
        data[i] = r + sumAt(x-1, y-1) + sumAt(x-1, y+1) - sumAt(x-1, y);
        data[i+1] = r + sumAt(x-1, y-1) + sumAt(x-1, y+1) - sumAt(x-1, y);
        data[i+2] = r + sumAt(x-1, y-1) + sumAt(x-1, y+1) - sumAt(x-1, y);
      }
    }
  }
  
  *(BufferedImage img) {
    w = img.getWidth();
    h = img.getHeight();
    if (longMul(w, h) > 8000000) fail("Image too big: " + w + "*" + h);
    int[] pixels = pixelsOfBufferedImage(img);
    for i over data:
      data[i] = new int[w*h*3];
      
    for i over columnSums:
      columnSums[i] = new int[max(w, h)*2]; // TODO: find actual upper bound
      
    int i = 0, j = 0, sumR = 0, sumG = 0, sumB = 0;
    for x to w: {
      int rgb = pixels[j++];
      data[i++] = (sumR += (rgb >> 16) & 0xFF);
      data[i++] = (sumG += (rgb >> 8) & 0xFF);
      data[i++] = (sumB += rgb & 0xFF);
    }
    for (int y = 1; y < h; y++) {
      sumR = sumG = sumB = 0;
      for x to w: {
        int rgb = pixels[j++];
        sumR += (rgb >> 16) & 0xFF;
        sumG += (rgb >> 8) & 0xFF;
        sumB += rgb & 0xFF;
        data[i] = sumR + data[i-w*3];
        data[i+1] = sumG + data[i-w*3+1];
        data[i+2] = sumB + data[i-w*3+2];
        i += 3;
      }
    }
  }
  
  // gets the integral value at x/y for given RGB channel
  int get(int x, int y, int channel) {
    ret x < 0 || y < 0 || x >= w || y >= h ? 0
      : data[(y*w+x)*3+channel];
  }
  
  // gets sum of the 3 channels
  int get(int x, int y) {
    if (x < 0 || y < 0 || x >= w || y >= h) ret 0;
    int i = (y*w+x)*3;
    ret data[i]+data[i+1]+data[i+2];
  }
  
  double averageBrightness() {
    ret doubleRatio(get(w-1, h-1), w*h*3*255.0);
  }
  
  toString {
    ret "HexagonalIntegralImage " + w + "*" + h + ", brightness: " + averageBrightness();
  }
  
  public int getWidth() { ret w; }
  public int getHeight() { ret h; }
  int w() { ret w; }
  int h() { ret h; }
}

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