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.

1  
sinterface IIntegralImage extends MakesBufferedImage {
2  
  public int getWidth();
3  
  public int getHeight();
4  
  default public Pt getSize() { ret pt(getWidth(), getHeight()); }
5  
  
6  
  default public int defaultChannel() { ret 0; }
7  
  default public int nChannels() { ret 3; }
8  
9  
  // get value for 1 channel
10  
  // normal range [0; pixelCount*256)
11  
  public double getIntegralValue(int x, int y, int channel);
12  
  
13  
  default double getIntegralValue(double x, double y, int channel) {
14  
    ret getIntegralValue(ifloor(x), ifloor(y), channel);
15  
  }
16  
    
17  
  // gets summed value of the 3 channels
18  
  // normal range [0; pixelCount*256*3)
19  
  default double getIntegralValue(int x, int y) {
20  
    ret getIntegralValue(x, y, 0)
21  
      + getIntegralValue(x, y, 1)
22  
      + getIntegralValue(x, y, 2);
23  
  }
24  
  
25  
  default double rectSum(int x1, int y1, int x2, int y2, int channel) {
26  
    double bottomLeft  = getIntegralValue(x1-1, y2-1, channel);
27  
    double bottomRight = getIntegralValue(x2-1, y2-1, channel);
28  
    double topLeft     = getIntegralValue(x1-1, y1-1, channel);
29  
    double topRight    = getIntegralValue(x2-1, y1-1, channel);
30  
    ret bottomRight+topLeft-topRight-bottomLeft;
31  
  }
32  
  
33  
  default double rectSum(double x1, double y1, double x2, double y2, int channel) {
34  
    double bottomLeft  = getIntegralValue(x1-1, y2-1, channel);
35  
    double bottomRight = getIntegralValue(x2-1, y2-1, channel);
36  
    double topLeft     = getIntegralValue(x1-1, y1-1, channel);
37  
    double topRight    = getIntegralValue(x2-1, y1-1, channel);
38  
    ret bottomRight+topLeft-topRight-bottomLeft;
39  
  }
40  
  
41  
  default double rectSum(int x1, int y1, int x2, int y2) {
42  
    double bottomLeft  = getIntegralValue(x1-1, y2-1);
43  
    double bottomRight = getIntegralValue(x2-1, y2-1);
44  
    double topLeft     = getIntegralValue(x1-1, y1-1);
45  
    double topRight    = getIntegralValue(x2-1, y1-1);
46  
    ret bottomRight+topLeft-topRight-bottomLeft;
47  
  }
48  
  
49  
  default double rectAverage(int x1, int y1, int x2, int y2, int channel) {
50  
    ret doubleRatio(rectSum(x1, y1, x2, y2, channel), areaFromPoints(x1, y1, x2, y2));
51  
  }
52  
  
53  
  default double rectAverage(Rect r, int channel) {
54  
    ret doubleRatio(rectSum(r, channel), rectArea(r));
55  
  }
56  
  
57  
  default double rectSum(Rect r) {
58  
    ret rectSum(r.x, r.y, r.x2(), r.y2());
59  
  }
60  
  
61  
  default double rectSum(Rect r, int channel) {
62  
    ret rectSum(r.x, r.y, r.x2(), r.y2(), channel);
63  
  }
64  
  
65  
  default double pixelSum(DoubleRect r) {
66  
    ret rectSum(toRect_floor(r), defaultChannel());
67  
  }
68  
  
69  
  default IIntegralImage clip(int x, int y, int w, int h) {
70  
    ret IIVirtualClip(this, x, y, w, h);
71  
  }
72  
  
73  
  default double averageBrightness() {
74  
    int w = getWidth(), h = getHeight();
75  
    ret doubleRatio(getIntegralValue(w-1, h-1), w*h*3*255.0);
76  
  }
77  
  
78  
  default RGB averageRGB() {
79  
    int w = getWidth(), h = getHeight();
80  
    double factor = 1/(255.0*(w*h));
81  
    ret RGB(
82  
      rectSum(0, 0, w, h, 0)*factor,
83  
      rectSum(0, 0, w, h, 1)*factor,
84  
      rectSum(0, 0, w, h, 2)*factor);
85  
  }
86  
  
87  
  // normal range: (0, 255)
88  
  default double getPixel(int x, int y, int channel) {
89  
    ret rectSum(x, y, x+1, y+1, channel);
90  
  }
91  
  
92  
  // returns RGB pixel without alpha
93  
  default int getPixel(int x, int y) {
94  
    int r = iround(rectSum(x, y, x+1, y+1, 0));
95  
    int g = iround(rectSum(x, y, x+1, y+1, 1));
96  
    int b = iround(rectSum(x, y, x+1, y+1, 2));
97  
    ret rgbInt(r, g, b);
98  
  }
99  
  
100  
  default int nPixels() { ret getWidth()*getHeight(); }
101  
  
102  
  default BufferedImage getBufferedImage() {
103  
    int w = getWidth(), h = getHeight();
104  
    int[] pixels = new[w*h];
105  
    int i = 0;
106  
    for y to h:
107  
      for x to w:
108  
        pixels[i++] = getPixel(x, y) | fullAlphaMask();
109  
    ret intArrayToBufferedImage(pixels, w, h);
110  
  }
111  
  
112  
  // minimum and maximum brightness possible in image 
113  
  // Better not to use because without this information
114  
  // you have a more general recognition algorithm.
115  
  //default DoubleRange colorRange(int channel) { ret doubleRange(0, 256); }
116  
}

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: 226 / 481
Version history: 37 change(s)
Referenced in: [show references]