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

92
LINES

< > BotCompany Repo | #1035288 // G22BurnIn (motion/still image detector)

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

Transpiled version (10913L) is out of date.

1  
sclass G22BurnIn {
2  
  settable double alphaStep = 0.1;
3  
  settable double tolerance = 0.1;
4  
  settable double colorMorph = 0.1; // speed to update color within tolerance
5  
  settable Color backgroundColor = Color.black;
6  
  
7  
  int toleranceSquaredInt;
8  
  int w, h;
9  
  int[] mask;
10  
  
11  
  // how much motion is there in the image overall? (0 to 1)
12  
  double motionFactor = Double.NaN;
13  
  
14  
  void processFrame(BufferedImage frame) {
15  
    toleranceSquaredInt = iround(sqr(tolerance*255)*3);
16  
    
17  
    int w = frame.getWidth(), h = frame.getHeight();
18  
    if (mask == null || this.w != w || this.h != h) {
19  
      this.w = w;
20  
      this.h = h;
21  
      this.mask = new int[w*h];
22  
    }
23  
    
24  
    // We assume the frame has no transparency
25  
    var gp = grabbableIntPixels_fastOrSlow(frame);
26  
    int n = w*h, iMask = 0, iFrame = gp.offset;
27  
    int[] pixels = gp.data;
28  
    int[] mask = this.mask;
29  
    
30  
    for y to h: {
31  
      for x to w: {
32  
        int mpix = mask[iMask];
33  
        int fcol = pixels[iFrame++] & 0xFFFFFF;
34  
        if (mpix == 0)
35  
          mpix = differentColor(mpix, fcol);
36  
        else {
37  
          int mcol = mpix & 0xFFFFFF;
38  
          int diff = rgbDistanceSquaredInt(mcol, fcol);
39  
          if (diff <= toleranceSquaredInt)
40  
            mpix = sameColor(mpix, fcol);
41  
          else
42  
            mpix = differentColor(mpix, fcol);
43  
        }
44  
        mask[iMask++] = mpix;
45  
      }
46  
      
47  
      iFrame += gp.scanlineStride-w;
48  
    }
49  
    
50  
    motionFactor = Double.NaN;
51  
  }
52  
  
53  
  bool isSameColor(int col1, int col2) {
54  
    
55  
    ret toleranceSquaredInt == 0
56  
      ? col1 == col2
57  
      : rgbDistanceSquaredInt(col1, col2) <= tolerance;
58  
  }
59  
  
60  
  int sameColor(int mpix, int fcol) {
61  
    double newAlpha = rgbAlphaZeroToOne(mpix)+alphaStep;
62  
    int newColor = blendRGBInts(mpix, fcol, colorMorph);
63  
    ret withAlpha(newAlpha, newColor);
64  
  }
65  
  
66  
  int differentColor(int mpix, int fcol) {
67  
    ret withAlpha(alphaStep, fcol);
68  
  }
69  
  
70  
  BufferedImage stillImage aka image() {
71  
    if (mask == null) null;
72  
    var img = bufferedImage(w, h, mask);
73  
    ret renderImageOnBackground(backgroundColor, img);
74  
  }
75  
  
76  
  BufferedImage motionImage() {
77  
    if (mask == null) null;
78  
    int[] mask2 = pixelsWithInvertedAlpha(mask);
79  
    var img = bufferedImage(w, h, mask2);
80  
    ret renderImageOnBackground(backgroundColor, img);
81  
  }
82  
  
83  
  BWImage motionDetectionImage() {
84  
    ret BWImage(w, h, alphaChannelFromPixels(mask));
85  
  }
86  
  
87  
  double motionFactor() {
88  
    if (isNaN(motionFactor) && mask != null)
89  
      motionFactor = 1-alphaChannelAverage(mask)/255.0;
90  
    ret motionFactor;
91  
  }
92  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1035288
Snippet name: G22BurnIn (motion/still image detector)
Eternal ID of this version: #1035288/16
Text MD5: bfd7adfb591096935a70f33c23a872f2
Author: stefan
Category: javax / gazelle 22
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-05-10 20:12:57
Source code size: 2626 bytes / 92 lines
Pitched / IR pitched: No / No
Views / Downloads: 94 / 200
Version history: 15 change(s)
Referenced in: [show references]