Guest User

Code

a guest
Apr 11th, 2016
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.90 KB | None | 0 0
  1. import processing.video.*;
  2. Capture capture;
  3. int avg;
  4. int tolerance = 20; //control how much a pixel must be diffrent from the corresponding pixel of the "samplingImage"
  5. int pixelsNecessary = 100; //this describes the quantity of reactive pixels required to set to activare a sensor
  6. int rows=20;
  7. int col=20;
  8. int rectnumber = rows * col;
  9. int whichMode = 0; //this variables is just to show the functionality and is not necessary
  10.  
  11. boolean backgroundCaptured = false; //this variable are just to show the functionality and is not necessary
  12.  
  13. rects[] rects; //this object array contains all the instances of the rectangles defined into the class "rects"
  14.  
  15. PImage samplingImage; //this is the image that is used check the differences with the live image
  16.  
  17. void setup() {
  18. int w=800;
  19. int h=600;
  20. size(w, h);
  21.  
  22.  
  23. samplingImage = createImage(w, h, RGB); //create a new blank image just to define its dimensions
  24.  
  25. capture = new Capture(this, w, h, 30); //here begins a new sessions of capturing of the images from the webcam
  26.  
  27. rectMode(CORNERS);
  28. rects = new rects[20000]; //define the rects array dimension
  29.  
  30. //here you can insert more rectangles (sensors) by specifying x1,y1,x2,y2
  31. // rects[0] = new rects(0, 0, 100, 100); //a1
  32. // rects[1] = new rects(100, 0, 200, 100); //b1
  33. // rects[2] = new rects(200, 0, 300, 100); //c1
  34. // rects[3] = new rects(300, 0, 400, 100); //d1
  35. // rects[4] = new rects(0, 100, 100, 200); //a2
  36. // rects[5] = new rects(100, 100, 200, 200); //b2
  37. // rects[6] = new rects(200, 100, 300, 200); //c2
  38. // rects[7] = new rects(300, 100, 400, 200); //d2
  39. // rects[8] = new rects(0, 200, 100, 300); //a3
  40. // rects[9] = new rects(100, 200, 200, 300); //b3
  41. // rects[10] = new rects(200, 200, 300, 300); //c3
  42. // rects[11] = new rects(300, 200, 400, 300); //d3
  43. // rects[12] = new rects(00, 300, 100, 400); //d4
  44. // rects[13] = new rects(100, 300, 200, 400); //d4
  45. // rects[14] = new rects(200, 300, 300, 400); //d4
  46. // rects[15] = new rects(300, 300, 400, 400); //d4
  47.  
  48. int x=0;
  49.  
  50.  
  51. int xgap = w/rows;
  52. int ygap = h/col;
  53. println("xgap="+xgap+"\nygap="+ygap);
  54. int x1=0, y1=0, x2=0, y2=0;
  55. while (x< (rows*col))
  56. {
  57. for (int i=0;i<=h-ygap;i+=ygap)
  58. {
  59. for (int j=0;j<=w-xgap;j+=xgap)
  60. {
  61. rects[x++]=new rects(j, i, j+xgap, i+ygap);
  62. println(x +"OK");
  63. }
  64. }
  65. }
  66. }
  67.  
  68.  
  69. void keyPressed() {
  70.  
  71. if ( key == 'q' ) { //if the key is pressed reset a new sampleImage
  72.  
  73. setSampleImage();
  74. }
  75.  
  76. //Others keys commands to change the parameters
  77. if ( key == 's' && tolerance > 0 ) tolerance--;
  78. if ( key == 'a' && tolerance < 255 ) tolerance++;
  79. if ( key == 'f' && tolerance > 0 ) pixelsNecessary--;
  80. if ( key == 'g' ) pixelsNecessary++;
  81. if ( key == 'z' ) {
  82. if (backgroundCaptured==false)backgroundCaptured=true;
  83. else backgroundCaptured=false;
  84. whichMode++;
  85. }
  86. }
  87.  
  88. void captureEvent(Capture myCapture) {
  89.  
  90. capture.read(); //this refresh the image captured by webcam and stored in the capture variable
  91. }
  92.  
  93.  
  94. void draw() {
  95.  
  96. int pixelX = -1, pixelY = -1;
  97.  
  98. if ( whichMode % 2 == 1 ) setSampleImage(); //this is necessary for the motion mode because it needs a new sampleImage every cycle
  99.  
  100.  
  101. loadPixels();
  102. capture.loadPixels(); //fill an array of pixels from the current captured image
  103.  
  104. for (int i=0; i<capture.height*capture.width; i++) { //read every element of the pixels array
  105.  
  106. pixelX = i % width; //get the real pixel X position
  107.  
  108. if ( pixelX != width ) pixelY = ( i / width ) +1;
  109. else pixelY = ( i / width ); //get the real pixel Y position
  110.  
  111.  
  112. 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
  113.  
  114. for ( int q = 0; q < rectnumber; q++) { //loop trough the rects array
  115.  
  116. if (rects[q]!=null) {
  117.  
  118. if ( pixelX > rects[q].x1 && pixelX < rects[q].x2 && pixelY > rects[q].y1 && pixelY < rects[q].y2 )
  119. //check if there is a sensors in this part of the image
  120. rects[q].amount++; //increase the number of pixels activated for this sensor}
  121. }
  122. }
  123. }
  124. }
  125.  
  126.  
  127. image( capture, 0, 0 ); //show the live webcam image
  128.  
  129.  
  130.  
  131.  
  132. if (!backgroundCaptured) { //this is just for this demostration. you nees just what is in the else parenthesis
  133. fill( 0, 0, 0, 100 );
  134. rect(0, 0, width, height-30);
  135. fill(255);
  136. textAlign(CENTER);
  137. 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 );
  138. textAlign(LEFT);
  139. }
  140. else
  141. {
  142. int sum=0; int count = 0;
  143. for ( int q = 0 ; q < rectnumber ; q++) { //loop trough the rects array
  144. int x1 =0, y1=0, x2=0, y2=0, amount = 0;
  145. if (rects[q]!=null)
  146. {
  147. x1 = rects[q].x1;
  148. y1 = rects[q].y1;
  149. x2 = rects[q].x2;
  150. y2 = rects[q].y2;
  151. amount = rects[q].amount;
  152. }
  153.  
  154. if ( amount > pixelsNecessary || amount == ((x2-x1)*(y2-y1)) )
  155. {
  156. sum=sum + rects[q].x1;
  157. count= count + 1;
  158. fill( 0, 255, 0 );
  159. if ( rects[q] != null )
  160. rects[q].draw(); //draw this rectangle object (go to the class to learn more)
  161.  
  162. }
  163. fill(0, 0, 0, 100);
  164. }
  165. if(count!=0)
  166. println(sum/count);
  167. }
  168.  
  169. //these are for this demo as well
  170. fill( 0, 0, 0, 200 );
  171. stroke( 0 );
  172. rect( 0, height - 30, width, height );
  173. fill( 255 );
  174. text( "Tolerance (a/s): " + (255-tolerance), 10, height - 10 );
  175. text( "Pixels required (f/g): " + pixelsNecessary, 150, height - 10 );
  176.  
  177. if (whichMode%2==1) text( "Mode (z): Motion Sensor", 320, height - 10 );
  178. else text( "Mode (z): Presence Sensor", 320, height - 10 );
  179. }
  180.  
  181.  
  182. class rects { //rectangle (sensors) class
  183.  
  184. int x1, x2, y1, y2, amount ; //coordinateso of the corner-corner rectangle and the amount of activated pixels
  185.  
  186. rects( int get_x1, int get_y1, int get_x2, int get_y2 ) { //lets create a new rectangles with the passed parameters
  187.  
  188. x1 = get_x1;
  189. y1 = get_y1;
  190. x2 = get_x2;
  191. y2 = get_y2;
  192. }
  193.  
  194. public void draw() { //rectangle draw function
  195.  
  196. // noFill();
  197.  
  198. //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
  199.  
  200. // if ( mousePressed && mouseX > x1 && mouseX < x2 && mouseY > y1 && mouseY < y2 ) { //this is just for this demo in order to let you move the sensors
  201. //
  202. // x1 += mouseX - pmouseX ;
  203. // x2 += mouseX - pmouseX ;
  204. // y1 += mouseY - pmouseY ;
  205. // y2 += mouseY - pmouseY ;
  206. // }
  207.  
  208. //draw the rectangle and the amount value text
  209. stroke( 0, 255, 0 );
  210. rect( x1, y1, x2, y2 );
  211. fill( 255 );
  212. //textAlign( CENTER );
  213. //text( amount, x1 + ( x2 - x1 ) / 2, y1 + ( y2 - y1 ) / 2 + 5 );
  214. amount = 0;
  215. //textAlign( LEFT );
  216. }
  217. }
  218.  
  219.  
  220.  
  221. void setSampleImage() {
  222.  
  223. backgroundCaptured=true; //this is for the demo
  224.  
  225. loadPixels();
  226. samplingImage.loadPixels(); //fill an array of pixels from the sampling image
  227.  
  228. 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
  229.  
  230. samplingImage.updatePixels(); //update the image
  231. }
Add Comment
Please, Sign In to add comment