Trouble with Absolute Difference in OpenCV
in
Contributed Library Questions
•
5 months ago
I have been using a Kinect's IR feed to track blobs using OpenCV then doing fun things with the points and send that out through Syphon.
I was able to use the opencv.absDiff(); method to remove the background using the space bar using my webcam earlier. However, when I switched to using the Kinect's IR feed I was unable to perform the background subtraction. This is a mission critical feature, so I am trying to see how it can be done.
I got the IR feed into OpenCV through the opencv.copy(); method and I believe that is somehow the issue. I know in the CV reference sheet, you should be able to specify if the absDiff(); should look at the capture or a buffer but I am not sure how.
Any suggestions?
-----
import codeanticode.syphon.*;
import hypermedia.video.*;
import java.awt.Rectangle;
import java.awt.Point;
import SimpleOpenNI.*;
OpenCV opencv;
SimpleOpenNI context;
SyphonServer syphonout;
int w = 640;
int h = 480;
int cvthreshold = 80;
int blur = 5;
PImage img; //This becomes the infrared feed from the Kinect
boolean find=true; //Not really sure what this does
PFont font;
void setup() {
size( w, h, P3D );
context = new SimpleOpenNI(this);
context.enableIR();
opencv = new OpenCV( this );
opencv.capture(640, 480);
syphonout = new SyphonServer(this, "SyphonOut");
println( "Drag mouse inside sketch window to change threshold" );
println( "Press space bar to record background image" );
}
void draw() {
background(0);
context.update(); //Updates kinect feed
PImage img = context.irImage(); // Asigns IR feed to "img"
opencv.copy(img); // Gives kinect IR to OpenCV's buffer
//opencv.read();
opencv.absDiff();
opencv.blur( OpenCV.BLUR, blur );
opencv.threshold(cvthreshold);
//image( opencv.image(), 0, 0 );
image(context.irImage(),0 ,0);
// This generates the blobs
Blob[] blobs = opencv.blobs( 100, w*h/3, 20, true );
noFill();
pushMatrix();
translate(0, 0);
for ( int i=0; i<blobs.length; i++ ) {
Rectangle bounding_rect
= blobs[i].rectangle;
float area = blobs[i].area;
float circumference = blobs[i].length;
Point centroid = blobs[i].centroid;
Point[] points = blobs[i].points;
// rectangle
noFill();
stroke( blobs[i].isHole ? 128 : 64 );
rect( bounding_rect.x, bounding_rect.y, bounding_rect.width, bounding_rect.height );
// centroid
stroke(0, 0, 255);
line( centroid.x-5, centroid.y, centroid.x+5, centroid.y );
line( centroid.x, centroid.y-5, centroid.x, centroid.y+5 );
noStroke();
fill(0, 0, 255);
text( area, centroid.x+5, centroid.y+5 );
fill(255, 0, 255, 64);
stroke(255, 0, 255);
if ( points.length>0 ) {
beginShape();
for ( int j=0; j<points.length; j++ ) {
vertex( points[j].x, points[j].y );
}
endShape(CLOSE);
}
noStroke();
fill(255, 0, 255);
text( circumference, centroid.x+5, centroid.y+15 );
}
popMatrix();
syphonout.sendImage(img);
}
//This allows the background the be removed
void keyPressed() {
if ( key==' ' ) opencv.remember();
}
//This allows you to use the mouse the adjust blur and threshold
void mouseDragged() {
cvthreshold = int( map(mouseX, 0, width, 0, 255) );
blur = int( map(mouseY, 0, height, 50, 0) );
}
public void stop() {
opencv.stop();
super.stop();
}
------
1