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

50
LINES

< > BotCompany Repo | #1035045 // BWImageHistogram - histogram of a BWImage

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

Libraryless. Click here for Pure Java version (8105L/47K).

1  
// Speed making histogram from BWImage: ~1 GPixels/s
2  
// Time to get median brightness: ~1us
3  
4  
sclass BWImageHistogram {
5  
  IBWImage image; // optional
6  
  int[] data; // pixel count for each brightness from 0 to 255
7  
  int nPixels; // sum of pixel count
8  
  int medianBrightness = -1;
9  
  int medianSum;
10  
  
11  
  *(BufferedImage image) {
12  
    this(BWImage(image));
13  
  }
14  
  
15  
  *(BWImage *image) {
16  
    nPixels = area(image);
17  
    data = bwImageHistogramArray(image);
18  
  }
19  
  
20  
  toString { ret "BWImageHistogram of " + nPixels(nPixels)
21  
    + (image == null ? "" : appendBracketed(str_px(image))); }
22  
  
23  
  int[] getArray() { ret data; }
24  
  
25  
  int medianSumTarget() { ret (nPixels+1)/2; }
26  
  
27  
  int medianBrightness() {
28  
    if (medianBrightness < 0) {
29  
      int threshold = medianSumTarget(), sum = 0;
30  
      for i to 256: {
31  
        sum += data[i];
32  
        if (sum > threshold) {
33  
          medianBrightness = i;
34  
          medianSum = sum;
35  
          break;
36  
        }
37  
      }
38  
    }
39  
    ret medianBrightness;
40  
  }
41  
  
42  
  // get pixel count for brightness
43  
  int get(int brightness) { ret data[brightness]; }
44  
  
45  
  void printMedianStats() {
46  
    int threshold = medianBrightness();
47  
    int prevSum = threshold == 0 ? 0 : medianSum-get(threshold-1);
48  
    printVars(+threshold, +prevSum, target := medianSumTarget(), sum := medianSum);
49  
  }
50  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1035045
Snippet name: BWImageHistogram - histogram of a BWImage
Eternal ID of this version: #1035045/13
Text MD5: 5f420c49edd319dae93a96a263ddcb77
Transpilation MD5: 32ef3f8bef9a359048bfe7aa30408e1a
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-25 22:12:20
Source code size: 1349 bytes / 50 lines
Pitched / IR pitched: No / No
Views / Downloads: 181 / 293
Version history: 12 change(s)
Referenced in: [show references]