//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //BlobDetection by v3ga //March 2007 //Processing(Beta) v1.24 // // In still situation, behave as a mirror // Activity determines movement of a yellow circle (headmouse) // // Author: Davide Rocchesso, modifying example available from f // http://www.v3ga.net/processing/BlobDetection/index-page-examples.html // Author: Giovanna Nicosia, modifying storing input // // ~~~~~~~~~~ // software : // ~~~~~~~~~~ // - Super Fast Blur v1.1 by Mario Klingemann // - BlobDetection library // // ~~~~~~~~~~ // hardware : // ~~~~~~~~~~ // - iSight // //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- import processing.video.*; import blobDetection.*; // storing input int num = 60; float mx[] = new float[num]; float my[] = new float[num]; Capture cam; PImage camDummy; BlobDetection theBlobDetection; PImage img; boolean newFrame=false; int MAXBLOB = 20; PImage monster, pointer; PImage mostri[] = new PImage[16]; float[] prevX = new float[MAXBLOB]; float[] prevY = new float[MAXBLOB]; boolean[] noFirstFrame = new boolean[MAXBLOB]; int hMouseX = 0; int hMouseY = 0; int prevHMouseX = 0; int prevHMouseY = 0; int stopTime = 0; int reflectTime = 0; int elapsedTime; int alphaDissolve; boolean SPECCHIA = true; boolean MOVING = true; boolean MONSTER = false; // PARAMETERS --- change these values to change behavior int blurRadius = 2; // if small, many blobs; if large, heavy load float stillThresh = 0.05; // threshold for motion float maxBlobWidth = 0.9; // boundaries for blob width (smaller than 1) float minBlobWidth = 0.1; float smoothCoef = 0.7; // smoothing coefficient (between 0 and 1) int maxStill = 900; // in milliseconds int maxMirror = 5000; float brightThre = 0.35; // will detect bright areas whose luminosity > brightThre float darkThre = 6.0; // below this value, it is considered dark // ================================================== // setup() // ================================================== void setup() { // Size of applet //size(640, 480); size(1024,768); // Capture cam = new Capture(this, 40*4, 30*4, 15); camDummy = new PImage(40*4, 30*4); //roc // BlobDetection // img which will be sent to detection (a smaller copy of the cam frame); img = new PImage(80,60); theBlobDetection = new BlobDetection(img.width, img.height); theBlobDetection.setPosDiscrimination(true); theBlobDetection.setThreshold(brightThre); monster = loadImage("mostro.png"); pointer = loadImage("gira.png"); mostri[0] = loadImage("mostro1.png"); mostri[1] = loadImage("mostro2.png"); mostri[2] = loadImage("mostro3.png"); mostri[3] = loadImage("mostro4.png"); mostri[4] = loadImage("mostro5.png"); mostri[5] = loadImage("mostro6.png"); mostri[6] = loadImage("mostro7.png"); mostri[7] = loadImage("mostro8.png"); // storing input //smooth(); noStroke(); fill(255, 153); } // ================================================== // captureEvent() // ================================================== void captureEvent(Capture cam) { cam.read(); for (int i=0; i maxMirror) { SPECCHIA = false; MONSTER = false; } fastblur(img, blurRadius); howdark = darkness(img); if (howdark < darkThre) { //println(howdark); MONSTER = true; SPECCHIA = true; reflectTime = millis(); } else { theBlobDetection.computeBlobs(img.pixels); drawBlobsAndEdges(true,false); } } // storing input for (int i = 1; i stillThresh)||(abs(prevY[n] - b.yMin) > stillThresh))// roc { // if the blob moved since previous frame if (!MOVING) { MOVING = true; if ((millis() - stopTime) > maxStill) { SPECCHIA = true; reflectTime = millis(); } } /* if ((b.w > 0.8)&&(b.h > 0.8)) { MONSTER = true; { SPECCHIA = true; reflectTime = millis(); } } */ if ((b.w < maxBlobWidth)&&(b.w > minBlobWidth)) // roc { // if the blob width is neither too small nor too large // draw embracing rectangle /* strokeWeight(1); stroke(255,0,0); rect( b.xMin*width,b.yMin*height, b.w*width,b.h*height ); */ hMouseX += b.xMin*width + b.w*width/2; NX += 1; hMouseY += b.yMin*width +b.h*height/2; } } else { // still image if (MOVING) { stopTime = millis(); // set timer MOVING = false; } } } prevX[n] = b.xMin; prevY[n] = b.yMin; } } } if (NX>0) { hMouseX = hMouseX/NX; hMouseY = hMouseY/NX; /* smoothing headmouse trajectory */ hMouseX = (int)((1-smoothCoef)*hMouseX+smoothCoef*prevHMouseX); hMouseY = (int)((1-smoothCoef)*hMouseY+smoothCoef*prevHMouseY); } else { hMouseX = prevHMouseX; hMouseY = prevHMouseY; } prevHMouseX = hMouseX; prevHMouseY = hMouseY; strokeWeight(3); stroke(200,200,0); //image(pointer,hMouseX, hMouseY); //ellipse(hMouseX, hMouseY, 30, 30); } // ================================================== // Super Fast Blur v1.1 // by Mario Klingemann // // ================================================== void fastblur(PImage img,int radius) { if (radius<1){ return; } int w=img.width; int h=img.height; int wm=w-1; int hm=h-1; int wh=w*h; int div=radius+radius+1; int r[]=new int[wh]; int g[]=new int[wh]; int b[]=new int[wh]; int rsum,gsum,bsum,x,y,i,p,p1,p2,yp,yi,yw; int vmin[] = new int[max(w,h)]; int vmax[] = new int[max(w,h)]; int[] pix=img.pixels; int dv[]=new int[256*div]; for (i=0;i<256*div;i++){ dv[i]=(i/div); } yw=yi=0; for (y=0;y>16; gsum+=(p & 0x00ff00)>>8; bsum+= p & 0x0000ff; } for (x=0;x>16; gsum+=((p1 & 0x00ff00)-(p2 & 0x00ff00))>>8; bsum+= (p1 & 0x0000ff)-(p2 & 0x0000ff); yi++; } yw+=w; } for (x=0;x