Can I apply ripple effect to Kinect

edited December 2017 in Kinect

Hi everyone, I'm trying to put some ripple effects to Kinect video. I found a ripple code online and tried it on a still image, it works. However,`` when I put it together with Kinect, there's no ripple at all-suppose to have the ripple effect when I drag the mouse. There's no error shown when I run the code. Can anyone help me on this? Thank you!

// A simple ripple effect. Click on the image to produce a ripple
// Author: radio79
// Code adapted from http://www.neilwallis.com/java/water.html

import org.openkinect.processing.*;

Kinect2 kinect2;


PImage img;
Ripple ripple;

void setup() {
  size(1920, 1080);
  kinect2 = new Kinect2(this);
  kinect2.initVideo();
  kinect2.initDevice();

  img = new PImage(kinect2.colorWidth, kinect2.colorHeight);

  ripple = new Ripple();
  //frameRate(60);
}

void draw() {

  image(kinect2.getVideoImage(), 0, 0);

  img.loadPixels();

  for (int loc = 0; loc < kinect2.colorWidth * kinect2.colorHeight; loc++) {
    img.pixels[loc] = ripple.col[loc];
  }


  img.updatePixels();
  ripple.newframe();
}

class Ripple {
  int i, a, b;
  int oldind, newind, mapind;
  short ripplemap[]; // the height map
  int col[]; // the actual pixels
  int riprad;
  int rwidth, rheight;
  int ttexture[];
  int ssize;

  Ripple() {
    // constructor
    riprad = 3;
    rwidth = width >> 1;
    rheight = height >> 1;
    ssize = width * (height + 2) * 2;
    ripplemap = new short[ssize];
    col = new int[width * height];
    ttexture = new int[width * height];
    oldind = width;
    newind = width * (height + 3);
  }



  void newframe() {
    // update the height map and the image
    i = oldind;
    oldind = newind;
    newind = i;

    i = 0;
    mapind = oldind;
    for (int y = 0; y < height; y++) {
      for (int x = 0; x < width; x++) {
        short data = (short)((ripplemap[mapind - width] + ripplemap[mapind + width] + 
          ripplemap[mapind - 1] + ripplemap[mapind + 1]) >> 1);
        data -= ripplemap[newind + i];
        data -= data >> 5;
        if (x == 0 || y == 0) // avoid the wraparound effect
          ripplemap[newind + i] = 0;
        else
          ripplemap[newind + i] = data;

        // where data = 0 then still, where data > 0 then wave
        data = (short)(1024 - data);

        // offsets
        a = ((x - rwidth) * data / 1024) + rwidth;
        b = ((y - rheight) * data / 1024) + rheight;

        //bounds check
        if (a >= width) 
          a = width - 1;
        if (a < 0) 
          a = 0;
        if (b >= height) 
          b = height-1;
        if (b < 0) 
          b=0;

        col[i] = img.pixels[a + (b * width)];
        mapind++;
        i++;
      }
    }
  }
}

void mouseDragged() {
  for (int j = mouseY - ripple.riprad; j < mouseY + ripple.riprad; j++) {
    for (int k = mouseX - ripple.riprad; k < mouseX + ripple.riprad; k++) {
      if (j >= 0 && j < height && k>= 0 && k < width) {
        ripple.ripplemap[ripple.oldind + (j * width) + k] += 512;
      }
    }
  }
}
Sign In or Register to comment.