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

116
LINES

< > BotCompany Repo | #1031920 // IIntegralImage - interface for RGB integral image

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

Transpiled version (5936L) is out of date.

sinterface IIntegralImage extends MakesBufferedImage {
  public int getWidth();
  public int getHeight();
  default public Pt getSize() { ret pt(getWidth(), getHeight()); }
  
  default public int defaultChannel() { ret 0; }
  default public int nChannels() { ret 3; }

  // get value for 1 channel
  // normal range [0; pixelCount*256)
  public double getIntegralValue(int x, int y, int channel);
  
  default double getIntegralValue(double x, double y, int channel) {
    ret getIntegralValue(ifloor(x), ifloor(y), channel);
  }
    
  // gets summed value of the 3 channels
  // normal range [0; pixelCount*256*3)
  default double getIntegralValue(int x, int y) {
    ret getIntegralValue(x, y, 0)
      + getIntegralValue(x, y, 1)
      + getIntegralValue(x, y, 2);
  }
  
  default double rectSum(int x1, int y1, int x2, int y2, int channel) {
    double bottomLeft  = getIntegralValue(x1-1, y2-1, channel);
    double bottomRight = getIntegralValue(x2-1, y2-1, channel);
    double topLeft     = getIntegralValue(x1-1, y1-1, channel);
    double topRight    = getIntegralValue(x2-1, y1-1, channel);
    ret bottomRight+topLeft-topRight-bottomLeft;
  }
  
  default double rectSum(double x1, double y1, double x2, double y2, int channel) {
    double bottomLeft  = getIntegralValue(x1-1, y2-1, channel);
    double bottomRight = getIntegralValue(x2-1, y2-1, channel);
    double topLeft     = getIntegralValue(x1-1, y1-1, channel);
    double topRight    = getIntegralValue(x2-1, y1-1, channel);
    ret bottomRight+topLeft-topRight-bottomLeft;
  }
  
  default double rectSum(int x1, int y1, int x2, int y2) {
    double bottomLeft  = getIntegralValue(x1-1, y2-1);
    double bottomRight = getIntegralValue(x2-1, y2-1);
    double topLeft     = getIntegralValue(x1-1, y1-1);
    double topRight    = getIntegralValue(x2-1, y1-1);
    ret bottomRight+topLeft-topRight-bottomLeft;
  }
  
  default double rectAverage(int x1, int y1, int x2, int y2, int channel) {
    ret doubleRatio(rectSum(x1, y1, x2, y2, channel), areaFromPoints(x1, y1, x2, y2));
  }
  
  default double rectAverage(Rect r, int channel) {
    ret doubleRatio(rectSum(r, channel), rectArea(r));
  }
  
  default double rectSum(Rect r) {
    ret rectSum(r.x, r.y, r.x2(), r.y2());
  }
  
  default double rectSum(Rect r, int channel) {
    ret rectSum(r.x, r.y, r.x2(), r.y2(), channel);
  }
  
  default double pixelSum(DoubleRect r) {
    ret rectSum(toRect_floor(r), defaultChannel());
  }
  
  default IIntegralImage clip(int x, int y, int w, int h) {
    ret IIVirtualClip(this, x, y, w, h);
  }
  
  default double averageBrightness() {
    int w = getWidth(), h = getHeight();
    ret doubleRatio(getIntegralValue(w-1, h-1), w*h*3*255.0);
  }
  
  default RGB averageRGB() {
    int w = getWidth(), h = getHeight();
    double factor = 1/(255.0*(w*h));
    ret RGB(
      rectSum(0, 0, w, h, 0)*factor,
      rectSum(0, 0, w, h, 1)*factor,
      rectSum(0, 0, w, h, 2)*factor);
  }
  
  // normal range: (0, 255)
  default double getPixel(int x, int y, int channel) {
    ret rectSum(x, y, x+1, y+1, channel);
  }
  
  // returns RGB pixel without alpha
  default int getPixel(int x, int y) {
    int r = iround(rectSum(x, y, x+1, y+1, 0));
    int g = iround(rectSum(x, y, x+1, y+1, 1));
    int b = iround(rectSum(x, y, x+1, y+1, 2));
    ret rgbInt(r, g, b);
  }
  
  default int nPixels() { ret getWidth()*getHeight(); }
  
  default BufferedImage getBufferedImage() {
    int w = getWidth(), h = getHeight();
    int[] pixels = new[w*h];
    int i = 0;
    for y to h:
      for x to w:
        pixels[i++] = getPixel(x, y) | fullAlphaMask();
    ret intArrayToBufferedImage(pixels, w, h);
  }
  
  // minimum and maximum brightness possible in image 
  // Better not to use because without this information
  // you have a more general recognition algorithm.
  //default DoubleRange colorRange(int channel) { ret doubleRange(0, 256); }
}

Author comment

Began life as a copy of #1027205

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx

No comments. add comment

Snippet ID: #1031920
Snippet name: IIntegralImage - interface for RGB integral image
Eternal ID of this version: #1031920/38
Text MD5: 327b3cc64aeaae3ff94df0252d32a682
Author: stefan
Category: javax / image recognition
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-13 01:48:03
Source code size: 4038 bytes / 116 lines
Pitched / IR pitched: No / No
Views / Downloads: 294 / 583
Version history: 37 change(s)
Referenced in: #1031958 - UpdatableImage - IIntegralImage where you can swap parts out while retaining the integral image property
#1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)