Can't get pixels out of my image

Hello kind people,
I’m following Mr. Daniel Shiffman’s tutorial on blob detection (the first one)

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 :frowning:

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.

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!

1 Like

Hi again :slight_smile:

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 (you can read the documentation), you can use it like this :

// 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[...]
1 Like

Thank you so much, it worked!

1 Like