# Setting Pixels of a Webcam Capture

**URL:** <https://discourse.processing.org/t/setting-pixels-of-a-webcam-capture/43976>\
**Category:** Libraries\
**Created:** [February 22, 2024, 1:51pm UTC](https://discourse.processing.org/t/setting-pixels-of-a-webcam-capture/43976 "2024-02-22T13:51:59Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [February 22, 2024, 5:51pm UTC](https://discourse.processing.org/t/setting-pixels-of-a-webcam-capture/43976/2 "2024-02-22T17:51:32Z")

</div>

Hello @dswayne92,

Modified your code:

```auto
// Modified code from here:
// https://discourse.processing.org/t/setting-pixels-of-a-webcam-capture/43976

import processing.video.*;

Capture video;
Movie clip;

color trackColor;
float threshold = 50;

void setup() 
  {
  size(640, 480);
  //frameRate(25);
  String[] cameras = Capture.list();
  printArray(cameras);
  video = new Capture(this, 640, 480);
  println(RGB); // GLV added! 
  println(video.format); // GLV added! 
  println(ARGB); // GLV added! 
  video.format = ARGB; // GLV added! 
  println(video.format); // GLV added! 
  video.start();

  trackColor = color(255, 0, 0);
  noLoop(); // GLV added!
  }

void draw() 
  {
  background(255, 255, 0);
  video.format = 0; // GLV added!          
  image(video, 0, 0);
  }

void captureEvent(Capture video) 
  {
  video.read();
  video.loadPixels();

  //iterate through pixel array
  for (int x = 0; x < video.width; x++) {
    for (int y = 0; y < video.height; y++)
    {
      //location of individual pixels
      int loc = x + y * video.width;

      //what is current color
      color currentColor = video.pixels[loc];

      //get color values
      int r1 = (currentColor >> 16) & 0xFF; // Faster way of getting red(argb)
      int g1 = (currentColor >> 8) & 0xFF; // Faster way of getting green(argb)
      int b1 = currentColor & 0xFF;//fast blue

      int r2 = (trackColor >> 16) & 0xFF; // Faster way of getting red(argb)
      int g2 = (trackColor >> 8) & 0xFF; // Faster way of getting green(argb)
      int b2 = trackColor & 0xFF; // Faster way of getting blue(argb)

      //compare similarity of current color to track color
      //using 3d distance function
      float d = distSq(r1, g1, b1, r2, g2, b2);

      //if distance is below threshold squared
      if ( d < threshold * threshold) 
        {
        video.pixels[loc] = color(0, 0, 0, 0);
        }
      }
    }
  redraw(); // GLV added!
  }

//3d distance for color compariso
float distSq(float x1, float y1, float z1, float x2, float y2, float z2) {
  float d = (x2-x1) * (x2-x1) + (y2 - y1 ) * (y2- y1) + (z2-z1) * (z2-z1);
  return d;
}

void mousePressed() {
  //save a color where the mouse is clicked in trackColor
  //number of pixels is pixelWidth * pixelHeight;
  // 1200 * 800 = 960,000
  int loc = mouseX + mouseY * video.width;
  trackColor = video.pixels[loc];
}

```

Related discussions:

- [Setting transparency with pixels[] - #17 by GoToLoop](https://discourse.processing.org/t/setting-transparency-with-pixels/501/17)
- [Alpha of Zero in first pixel makes whole image opaque - #2 by stevensrmiller](https://discourse.processing.org/t/alpha-of-zero-in-first-pixel-makes-whole-image-opaque/32400/2)

Scrutinize the changes and look up the references where needed.

`:)`

---

_[View the full topic](https://discourse.processing.org/t/setting-pixels-of-a-webcam-capture/43976)._
