Source Code. Projects. Nerd Stuff. Art Stuff.

Pixel Tracking, Background Subtraction, Frame Differencing

 

[gn_spoiler title="greenPixelTrack" open="0" style="1"]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import processing.video.*;

Capture cam;
PImage displayImg;

void setup() {
  size(640, 480);

  cam = new Capture(this, 640, 480);
  cam.start();

  displayImg = new PImage(640, 480);
}

void draw() {
  if (cam.available()) {
    cam.read();

    int greenX = 0;
    int greenY = 0;
    float mostGreenPixel = 10000;  // start green value very high

    for (int x = 0; x < cam.width; x++) {
      for (int y = 0; y < cam.height; y++) {
        // (width - x - 1) refers to a reversed image
        int dIndex = x + y * cam.width;  // display index
        int cIndex = (width - x - 1) + y * cam.width;  // camera index

        color currentPixel = cam.pixels[cIndex];
       
        float distFromGreen = dist(0, 255, 0,
              red(currentPixel), green(currentPixel), blue(currentPixel));
             
        if(distFromGreen < mostGreenPixel) {
          mostGreenPixel = distFromGreen;
          greenX = x;
          greenY = y;
        }

        displayImg.pixels[dIndex] = cam.pixels[cIndex];
      }
    }
   
    displayImg.updatePixels();

    image(displayImg, 0, 0);
   
    ellipse(greenX, greenY, 100, 100);
  }
}
[/gn_spoiler]

 

[gn_spoiler title="brightestPixel" open="0" style="1"]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import processing.video.*;

Capture cam;
PImage displayImg;

void setup() {
  size(640, 480);
  colorMode(HSB, 255);

  cam = new Capture(this, 640, 480);
  cam.start();

  displayImg = new PImage(640, 480);
}

void draw() {
  if (cam.available()) {
    cam.read();

    int brightX = 0;
    int brightY = 0;
    float brightestPixel = 0;  // set brightness very low

    for (int x = 0; x < cam.width; x++) {
      for (int y = 0; y < cam.height; y++) {
        // (width - x - 1) refers to a reversed image
        int dIndex = x + y * cam.width;  // display index
        int cIndex = (width - x - 1) + y * cam.width;  // camera index

        color currentPixel = cam.pixels[cIndex];

        float currentBright = brightness(currentPixel);

        if (currentBright > brightestPixel) {
          brightestPixel = currentBright;
          brightX = x;
          brightY = y;
        }

        displayImg.pixels[dIndex] = cam.pixels[cIndex];
      }
    }

    displayImg.updatePixels();

    image(displayImg, 0, 0);

    ellipse(brightX, brightY, 100, 100);
  }
}
[/gn_spoiler]

 

[gn_spoiler title="frameDifferencing" open="0" style="1"]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import processing.video.*;

Capture cam;
PImage img;
PImage bgImg;

void setup() {
  size(640, 480);

  cam = new Capture(this, width, height);
  cam.start();

  img = new PImage(width, height);
  bgImg = new PImage(width, height);
}

void draw() {
  if (cam.available()) {
    cam.read();

    for (int x = 0; x < cam.width; x++) {
      for (int y = 0; y < cam.height; y++) {
        int camIndex = x + y * cam.width;
        int displayIndex = (cam.width - x - 1) + y * cam.width;

        img.pixels[displayIndex] = cam.pixels[camIndex];

        // grab the pixel out of the array, the pixels
        // have the same x and y coordinates because both
        // images are the same size
        color currentPixelColor = img.pixels[displayIndex]; // store currentPixel color
        color bgImgPixelColor = bgImg.pixels[displayIndex];   // store bgImgPixel color
       
        float colorDist = dist(red(bgImgPixelColor), // compare pixel colors: color distance
        green(bgImgPixelColor), // between current pixel value and the
        blue(bgImgPixelColor),      // background pixel value
        red(currentPixelColor),
        green(currentPixelColor),
        blue(currentPixelColor));
       
        if (colorDist < 100) {
          img.pixels[displayIndex] = color(255, 0, 0);
        }
      }
    }

    img.updatePixels();

    image(img, 0, 0);
  }
}

void keyPressed() {
  for (int x = 0; x < cam.width; x++) {
    for (int y = 0; y < cam.height; y++) {
      int index = x + y * cam.width;

      bgImg.pixels[index] = img.pixels[index];
    }
  }  
  bgImg.updatePixels();
}
[/gn_spoiler]