detecting motion in a videostream and finding pixel locations...

edited January 2016 in Library Questions

I wrote a sketch to find the motion outlines using opencv. I then use the getPoints function to find the PVectors to use for placing particles. It works quite well but I'm worried about performance when I try to upscale this to hd video stream. I'm fairly new to Processing so can anyone tell me if there is a more efficient way of doing this? TIA My code (not including the Particle class but that just a simple class): ` import gab.opencv.*; import processing.video.*; import java.awt.*;

Capture video; OpenCV opencv; ArrayList contourPoints = new ArrayList (); ArrayList particles; PVector loc = new PVector (0,0);

void setup() { size(640, 480, P2D); video = new Capture(this, 640, 480); opencv = new OpenCV(this, 640, 480); particles = new ArrayList();

opencv.startBackgroundSubtraction(5, 3, 0.5);

video.start(); }

void draw() { background(255); opencv.loadImage(video);

opencv.updateBackground();

opencv.dilate(); opencv.erode();

for (Contour contour : opencv.findContours()) { contourPoints = contour.getPoints(); for (int i = 0; i < contourPoints.size()-10; i=i+10) { loc = contourPoints.get(i); particles.add(new Particle(new PVector(loc.x,loc.y))); } } // Looping through backwards to delete for (int i = particles.size()-1; i >= 0; i--) { Particle p = particles.get(i); p.run(); if (p.isDead()) { particles.remove(i); } } // println(frameRate); }

void captureEvent(Capture c) { c.read(); }`

Answers

  • Please highlight the code in your post then hit Ctrl+O to format it. Makes it a whole lot easier to read. Cheers.

  • Oops sorry. I did use the "code" icon at the top but doesn't work I guess. I tried it again, also CNRTL-O but that doesn't work either - just brings up IExplorer's "open" dialog. I re-formatted it by hand and hope it is saved when I post this.

    Capture video;
    OpenCV opencv; 
    ArrayList contourPoints = new ArrayList (); 
    ArrayList particles; 
    PVector loc = new PVector (0,0);
    
    void setup() { 
    size(640, 480, P2D); 
    video = new Capture(this, 640, 480); 
    opencv = new OpenCV(this, 640, 480); 
    particles = new ArrayList();
    opencv.startBackgroundSubtraction(5, 3, 0.5);
    video.start(); 
    }
    
    void draw() {
    
    background(255); 
    opencv.loadImage(video);
    opencv.updateBackground();
    opencv.dilate(); opencv.erode();
    
    for (Contour contour : opencv.findContours()) {
        contourPoints = contour.getPoints(); 
        for (int i = 0; i < contourPoints.size()-10; i=i+10) { 
            loc = contourPoints.get(i); 
            particles.add(new Particle(new PVector(loc.x,loc.y))); 
        } 
    } 
    
    // Looping through backwards to delete 
    for (int i = particles.size()-1; i >= 0; i--) { 
        Particle p = particles.get(i); 
        p.run(); 
        if (p.isDead()) { 
        particles.remove(i); 
     } 
    } 
    // println(frameRate); }
    
    void captureEvent(Capture c) { c.read(); }
    
  • Btw, why was my post moved into the kinect topic? I'm just using a webcam, not a kinect.

Sign In or Register to comment.