Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import processing.video.*;
- Capture capture;
- int avg;
- int tolerance = 20; //control how much a pixel must be diffrent from the corresponding pixel of the "samplingImage"
- int pixelsNecessary = 100; //this describes the quantity of reactive pixels required to set to activare a sensor
- int rows=20;
- int col=20;
- int rectnumber = rows * col;
- int whichMode = 0; //this variables is just to show the functionality and is not necessary
- boolean backgroundCaptured = false; //this variable are just to show the functionality and is not necessary
- rects[] rects; //this object array contains all the instances of the rectangles defined into the class "rects"
- PImage samplingImage; //this is the image that is used check the differences with the live image
- void setup() {
- int w=800;
- int h=600;
- size(w, h);
- samplingImage = createImage(w, h, RGB); //create a new blank image just to define its dimensions
- capture = new Capture(this, w, h, 30); //here begins a new sessions of capturing of the images from the webcam
- rectMode(CORNERS);
- rects = new rects[20000]; //define the rects array dimension
- //here you can insert more rectangles (sensors) by specifying x1,y1,x2,y2
- // rects[0] = new rects(0, 0, 100, 100); //a1
- // rects[1] = new rects(100, 0, 200, 100); //b1
- // rects[2] = new rects(200, 0, 300, 100); //c1
- // rects[3] = new rects(300, 0, 400, 100); //d1
- // rects[4] = new rects(0, 100, 100, 200); //a2
- // rects[5] = new rects(100, 100, 200, 200); //b2
- // rects[6] = new rects(200, 100, 300, 200); //c2
- // rects[7] = new rects(300, 100, 400, 200); //d2
- // rects[8] = new rects(0, 200, 100, 300); //a3
- // rects[9] = new rects(100, 200, 200, 300); //b3
- // rects[10] = new rects(200, 200, 300, 300); //c3
- // rects[11] = new rects(300, 200, 400, 300); //d3
- // rects[12] = new rects(00, 300, 100, 400); //d4
- // rects[13] = new rects(100, 300, 200, 400); //d4
- // rects[14] = new rects(200, 300, 300, 400); //d4
- // rects[15] = new rects(300, 300, 400, 400); //d4
- int x=0;
- int xgap = w/rows;
- int ygap = h/col;
- println("xgap="+xgap+"\nygap="+ygap);
- int x1=0, y1=0, x2=0, y2=0;
- while (x< (rows*col))
- {
- for (int i=0;i<=h-ygap;i+=ygap)
- {
- for (int j=0;j<=w-xgap;j+=xgap)
- {
- rects[x++]=new rects(j, i, j+xgap, i+ygap);
- println(x +"OK");
- }
- }
- }
- }
- void keyPressed() {
- if ( key == 'q' ) { //if the key is pressed reset a new sampleImage
- setSampleImage();
- }
- //Others keys commands to change the parameters
- if ( key == 's' && tolerance > 0 ) tolerance--;
- if ( key == 'a' && tolerance < 255 ) tolerance++;
- if ( key == 'f' && tolerance > 0 ) pixelsNecessary--;
- if ( key == 'g' ) pixelsNecessary++;
- if ( key == 'z' ) {
- if (backgroundCaptured==false)backgroundCaptured=true;
- else backgroundCaptured=false;
- whichMode++;
- }
- }
- void captureEvent(Capture myCapture) {
- capture.read(); //this refresh the image captured by webcam and stored in the capture variable
- }
- void draw() {
- int pixelX = -1, pixelY = -1;
- if ( whichMode % 2 == 1 ) setSampleImage(); //this is necessary for the motion mode because it needs a new sampleImage every cycle
- loadPixels();
- capture.loadPixels(); //fill an array of pixels from the current captured image
- for (int i=0; i<capture.height*capture.width; i++) { //read every element of the pixels array
- pixelX = i % width; //get the real pixel X position
- if ( pixelX != width ) pixelY = ( i / width ) +1;
- else pixelY = ( i / width ); //get the real pixel Y position
- if ( abs( hue( capture.pixels[i] ) - hue( samplingImage.pixels[i] ) ) > tolerance && abs( saturation( capture.pixels[i] ) - saturation( samplingImage.pixels[i] ) ) > tolerance && abs( brightness( capture.pixels[i] ) - brightness( samplingImage.pixels[i] ) ) >tolerance ) { //HSB values comparison between the current image and the sample image at the current pixel
- for ( int q = 0; q < rectnumber; q++) { //loop trough the rects array
- if (rects[q]!=null) {
- if ( pixelX > rects[q].x1 && pixelX < rects[q].x2 && pixelY > rects[q].y1 && pixelY < rects[q].y2 )
- //check if there is a sensors in this part of the image
- rects[q].amount++; //increase the number of pixels activated for this sensor}
- }
- }
- }
- }
- image( capture, 0, 0 ); //show the live webcam image
- if (!backgroundCaptured) { //this is just for this demostration. you nees just what is in the else parenthesis
- fill( 0, 0, 0, 100 );
- rect(0, 0, width, height-30);
- fill(255);
- textAlign(CENTER);
- text( "Go out from the screen and press 'q' key\n in order to capture the background.\n\n Drag the rectangles (sensors) to change the sensors positions.\n\nPress 'a'/'s' keys to decrease/increase the sensors tolerance.\n\nPress 'f'/'g' keys to decrease/increase the number of pixels\n necessary to activate a sensor.\n\n Press 'z' to switch to the motion sensor mode. ", width/2, 100 );
- textAlign(LEFT);
- }
- else
- {
- int sum=0; int count = 0;
- for ( int q = 0 ; q < rectnumber ; q++) { //loop trough the rects array
- int x1 =0, y1=0, x2=0, y2=0, amount = 0;
- if (rects[q]!=null)
- {
- x1 = rects[q].x1;
- y1 = rects[q].y1;
- x2 = rects[q].x2;
- y2 = rects[q].y2;
- amount = rects[q].amount;
- }
- if ( amount > pixelsNecessary || amount == ((x2-x1)*(y2-y1)) )
- {
- sum=sum + rects[q].x1;
- count= count + 1;
- fill( 0, 255, 0 );
- if ( rects[q] != null )
- rects[q].draw(); //draw this rectangle object (go to the class to learn more)
- }
- fill(0, 0, 0, 100);
- }
- if(count!=0)
- println(sum/count);
- }
- //these are for this demo as well
- fill( 0, 0, 0, 200 );
- stroke( 0 );
- rect( 0, height - 30, width, height );
- fill( 255 );
- text( "Tolerance (a/s): " + (255-tolerance), 10, height - 10 );
- text( "Pixels required (f/g): " + pixelsNecessary, 150, height - 10 );
- if (whichMode%2==1) text( "Mode (z): Motion Sensor", 320, height - 10 );
- else text( "Mode (z): Presence Sensor", 320, height - 10 );
- }
- class rects { //rectangle (sensors) class
- int x1, x2, y1, y2, amount ; //coordinateso of the corner-corner rectangle and the amount of activated pixels
- rects( int get_x1, int get_y1, int get_x2, int get_y2 ) { //lets create a new rectangles with the passed parameters
- x1 = get_x1;
- y1 = get_y1;
- x2 = get_x2;
- y2 = get_y2;
- }
- public void draw() { //rectangle draw function
- // noFill();
- //if ( amount > pixelsNecessary || amount == ((x2-x1)*(y2-y1)) ) fill( 255, 0, 0 ); //if the amount is higher than ne pixelsNecessary variable or if it is equal to the number of maximum pixels of a rectangle draw it of red
- // if ( mousePressed && mouseX > x1 && mouseX < x2 && mouseY > y1 && mouseY < y2 ) { //this is just for this demo in order to let you move the sensors
- //
- // x1 += mouseX - pmouseX ;
- // x2 += mouseX - pmouseX ;
- // y1 += mouseY - pmouseY ;
- // y2 += mouseY - pmouseY ;
- // }
- //draw the rectangle and the amount value text
- stroke( 0, 255, 0 );
- rect( x1, y1, x2, y2 );
- fill( 255 );
- //textAlign( CENTER );
- //text( amount, x1 + ( x2 - x1 ) / 2, y1 + ( y2 - y1 ) / 2 + 5 );
- amount = 0;
- //textAlign( LEFT );
- }
- }
- void setSampleImage() {
- backgroundCaptured=true; //this is for the demo
- loadPixels();
- samplingImage.loadPixels(); //fill an array of pixels from the sampling image
- for ( int i = 0 ; i < capture.height * capture.width ; i++ ) samplingImage.pixels[i] = capture.pixels[i]; //change every pixel with the current pixel of the live image
- samplingImage.updatePixels(); //update the image
- }
Add Comment
Please, Sign In to add comment