# Can't get pixels out of my image

**URL:** https://discourse.processing.org/t/cant-get-pixels-out-of-my-image/21733
**Category:** Libraries
**Created:** [June 10, 2020, 10:47am UTC](https://discourse.processing.org/t/cant-get-pixels-out-of-my-image/21733 "2020-06-10T10:47:07Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![marianalimoes](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@marianalimoes](https://discourse.processing.org/u/marianalimoes)
#### Post date: [June 10, 2020, 10:47am UTC](https://discourse.processing.org/t/cant-get-pixels-out-of-my-image/21733/1 "2020-06-10T10:47:07Z")

</div>

Hello kind people,  
I’m following Mr. Daniel Shiffman’s tutorial on blob detection ([the first one](https://www.youtube.com/watch?v=ce-2l2wRqO8&list=PLRqwX-V7Uu6bw0bVn4M63p8TMJf3OhGy8&index=8&t=57s))

Here is the code I copied from his GitHub. I also have the Blob tab (exactly as his) and a DCapture class. Because my camera isn’t read properly (probably a problem with the Surface’s driver), I have no choice but to use Directshow. However, from my novice understanding, I realize the object cap isn’t read as the image it actually is. Therefore, I can’t find it’s pixels or work with its pixels.

I get the following message: “The global variable ‘pixels’ does not exist”

I hope anyone can help me for I have this assignment and can’t get this to work ☹

```auto
import de.humatic.dsj.*;
import java.awt.image.BufferedImage;
import processing.video.*;
 
DCapture cap;

color trackColor; 
float threshold = 20;
float distThreshold = 75;

ArrayList<Blob> blobs = new ArrayList<Blob>();

void setup() {
  size(640, 360);
  cap = new DCapture();
  
  trackColor = color(255, 0, 0);
}

void keyPressed() {
  if (key == 'a') {
    distThreshold++;
  } else if (key == 'z') {
    distThreshold--;
  }
  println(distThreshold);
}

void draw() {
  image(cap.updateImage(), 0, 0, cap.width, cap.height);

  blobs.clear();

  //threshold = map(mouseX, 0, width, 0, 100);
  threshold = 80;

  // Begin loop to walk through every pixel
  for (int x = 0; x < cap.width; x++ ) {
    for (int y = 0; y < cap.height; y++ ) {
      int loc = x + y * cap.width;
      // What is current color
      color currentColor = cap.pixels[loc];
      float r1 = red(currentColor);
      float g1 = green(currentColor);
      float b1 = blue(currentColor);
      float r2 = red(trackColor);
      float g2 = green(trackColor);
      float b2 = blue(trackColor);

      float d = distSq(r1, g1, b1, r2, g2, b2); 

      if (d < threshold*threshold) {

        boolean found = false;
        for (Blob b : blobs) {
          if (b.isNear(x, y)) {
            b.add(x, y);
            found = true;
            break;
          }
        }

        if (!found) {
          Blob b = new Blob(x, y);
          blobs.add(b);
        }
      }
    }
  }

  for (Blob b : blobs) {
    if (b.size() > 500) {
      b.show();
    }
  }
}

float distSq(float x1, float y1, float x2, float y2) {
  float d = (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1);
  return d;
}

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 color where the mouse is clicked in trackColor variable
  int loc = mouseX + mouseY*cap.width;
  trackColor = cap.pixels[loc];
}

```

Here is the code for the DCapture class. it is, supposedly, drawing an image but I can’t get it’s pixels.

```auto
class DCapture implements java.beans.PropertyChangeListener {
 
  private DSCapture capture;
  public int width, height;
 
  DCapture() {
    DSFilterInfo[][] dsi = DSCapture.queryDevices();
    capture = new DSCapture(DSFiltergraph.DD7, dsi[0][1], false, 
    DSFilterInfo.doNotRender(), this);
    width = capture.getDisplaySize().width;
    height = capture.getDisplaySize().height;
  }
 
  public PImage updateImage() {
    PImage img = createImage(width, height, RGB);
    BufferedImage bimg = capture.getImage();
    bimg.getRGB(0, 0, img.width, img.height, img.pixels, 0, img.width);
    img.updatePixels();
    return img;
  }
 
  public void propertyChange(java.beans.PropertyChangeEvent e) {
    switch (DSJUtils.getEventType(e)) {
    }
  }
}

```

Thank you very much in advance!

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [June 10, 2020, 1:55pm UTC](https://discourse.processing.org/t/cant-get-pixels-out-of-my-image/21733/2 "2020-06-10T13:55:10Z")

</div>

Hi again 🙂

I think that you need to use the `updateImage()` method of the `DSCapture` class. Since it’s fetching the current webcam image and returning a [`PImage`](https://processing.org/reference/PImage.html) (you can read the documentation), you can use it like this :

```auto
// Get the latest image from your webcam
PImage webcam = cap.updateImage();

// Load the image pixels
webcam.loadPixels();

// Do your stuff with pixels
// by using webcam.pixels[...]

```

---

<div class="post-metadata">

### Author: ![marianalimoes](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@marianalimoes](https://discourse.processing.org/u/marianalimoes)
#### Post date: [June 10, 2020, 7:30pm UTC](https://discourse.processing.org/t/cant-get-pixels-out-of-my-image/21733/4 "2020-06-10T19:30:23Z")

</div>

Thank you so much, it worked!
