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
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!