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).

// Speed making histogram from BWImage: ~1 GPixels/s
// Time to get median brightness: ~1us

sclass BWImageHistogram {
  IBWImage image; // optional
  int[] data; // pixel count for each brightness from 0 to 255
  int nPixels; // sum of pixel count
  int medianBrightness = -1;
  int medianSum;
  
  *(BufferedImage image) {
    this(BWImage(image));
  }
  
  *(BWImage *image) {
    nPixels = area(image);
    data = bwImageHistogramArray(image);
  }
  
  toString { ret "BWImageHistogram of " + nPixels(nPixels)
    + (image == null ? "" : appendBracketed(str_px(image))); }
  
  int[] getArray() { ret data; }
  
  int medianSumTarget() { ret (nPixels+1)/2; }
  
  int medianBrightness() {
    if (medianBrightness < 0) {
      int threshold = medianSumTarget(), sum = 0;
      for i to 256: {
        sum += data[i];
        if (sum > threshold) {
          medianBrightness = i;
          medianSum = sum;
          break;
        }
      }
    }
    ret medianBrightness;
  }
  
  // get pixel count for brightness
  int get(int brightness) { ret data[brightness]; }
  
  void printMedianStats() {
    int threshold = medianBrightness();
    int prevSum = threshold == 0 ? 0 : medianSum-get(threshold-1);
    printVars(+threshold, +prevSum, target := medianSumTarget(), sum := medianSum);
  }
}

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: 179 / 290
Version history: 12 change(s)
Referenced in: [show references]