Skip navigation

I started looking at similarities from a security camera, but I couldn’t get the sample code to reverse sort (most different to least different)  in a useful way. I’m not sure if it was a bug in my code,  if I wasn’t using enough bins, or if my images weren’t well suited.   So I switched to the OpenCV module for Processing which provides some of the basic absdiff and threshold modules needed for simple motion detection.

// -*-Java-*-
import java.io.File;
import java.io.FilenameFilter;

import hypermedia.video.*;

OpenCV opencv;
PImage[] images;
int[] moved = new int[65536];
int movedCount = 0;
File datafolder;
String[] files;

int ci = 0;
int imageW = 320;
int imageH = 240;

void setup() {

    size(700,600);

    // open video stream
    opencv = new OpenCV( this );
    opencv.allocate(imageW, imageH);

    // read list of images from directory   
    datafolder = new File(sketchPath, “data”);
    files = datafolder.list(filter);
    images = new PImage[files.length];

    for (int i = 0; i < files.length; i++) {     println(files[i]);     images[i] = loadImage(files[i]);     } } // turn draw into a method that generates a list of interesting images // based on threshold values. void draw() {     int t;     // only do the search once     if (movedCount == 0) {     FindUnusual();     background(0);     }     if (ci > movedCount) {
    ci = 0;
    background(0);
    delay(1000);
    }

    t = moved[ci];
    image(images[t], 0, 0);
    print(ci); print(“: “);
    println(files[t]);
}

int CountWhite(PImage im)
{
    int thresh = 250;
    int white = 0;
    int p;

    // if r, g, and b are all greater than thresh, then we’re
    // calling this “white”

    int l = im.width * im.height;
    for (int i = 0; i < l; i++) {     //    println (hex(im.pixels[i]));     p = im.pixels[i];     if ( (((p >> 24) & 0xFF) > thresh)
         && ((( p >> 16) & 0xFF) > thresh)
         && ((( p >> 8)  & 0xFF) > thresh) ) {
        white++;
    }
    }
    //    if (white > 0) {
    //    println(white);
    //    }
    return white;
}

void FindUnusual()
{
    int mi = images.length -1;
    for (int i = 0; i < mi; i++) {     println(i);     opencv.copy(images[i]);     opencv.remember(OpenCV.BUFFER);     opencv.copy(images[i+1]);     //    image(opencv.image(), images[i].width, 0);     opencv.absDiff();     opencv.threshold(70);     //    image(images[i], 0, 0);     //    image( opencv.image(), 0, images[i].height );               PImage ti = opencv.image();     if (CountWhite(ti) > 100) {
        moved[movedCount++] = (i+1);
        //        moved[movedCount++] = i+1;
    }
    }
}

FilenameFilter filter = new FilenameFilter() {
    public boolean accept(File dir, String name) {
        if (name.toLowerCase().endsWith(“.jpg”)) return true;
        return false;
    }
    };

void keyPressed()
{
    if (key == ‘n’) {
    ci++;
    }
    else if (key == ‘p’) {
    ci–;
    }

    if (ci < 0) {     ci = movedCount;     }     else if (ci == movedCount )  {     ci = 0;     }     println(ci); } [/sourcecode]

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: