Advertisement
sprocket2cog

Ambient Light (processing)

Oct 17th, 2015
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.89 KB | None | 0 0
  1. /*
  2. Processing color picker
  3. Shows a canvas filled with the average color seen by the video source (usually a webcam).
  4. Version 1.0
  5. --------------------------------------------
  6. Copyright 2013 DraftFCB Chicago
  7. Licensed under the Eclipse Public License, Version 1.0 (the "License");
  8. you may not use this file except in compliance with the License.
  9. You may obtain a copy of the License at
  10.   http://www.eclipse.org/legal/epl-v10.html
  11. Unless required by applicable law or agreed to in writing, software
  12. distributed under the License is distributed on an "AS IS" BASIS,
  13. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. See the License for the specific language governing permissions and
  15. limitations under the License.
  16. */  
  17. //code mpdified from this code
  18. //https://github.com/dfcb/processing-color-averager/blob/master/processing_color_averager.pde
  19.    import processing.serial.*;
  20.  
  21. import processing.video.*;
  22. // Variable for capture device
  23. Capture video;
  24. Serial myPort;
  25. String test1 ="";
  26. int Colorsend = 0;
  27. int canvasWidth = 640;
  28. int canvasHeight = 480;
  29. int  bt = 1;
  30. void setup() {  String portName = Serial.list()[32]; //change the 0 to a 1 or 2 etc. to match your port
  31.   myPort = new Serial(this, portName, 9600);
  32.   size(canvasWidth,canvasHeight);
  33.   video = new Capture(this, width, height, 30);
  34.   video.start();
  35.   // Remove border from shapes drawn
  36.   strokeWeight(0);
  37. }
  38.  
  39. void draw() {
  40.  
  41.   // Capture video
  42.   if (video.available()) {
  43.     video.read();
  44.   }
  45.  
  46.   loadPixels();
  47.   video.loadPixels();
  48.  
  49.   //red, green and blue accumulators
  50.   int rAcc = 0;
  51.   int gAcc = 0;
  52.   int bAcc = 0;
  53.  
  54.   //red, green and blue value averages
  55.   int rAvg = 0;
  56.   int gAvg = 0;
  57.   int bAvg = 0;
  58.  
  59.   //total number of pixels for calculating average color
  60.   int numPix =(video.width * video.height);
  61.      
  62.   // Loop through the pixels
  63.   for (int x = 0; x < video.width; x ++ ) {
  64.     for (int y = 0; y < video.height; y ++ ) {
  65.      
  66.       int loc = x + y*video.width;            
  67.       color current = video.pixels[loc];      
  68.      
  69.       // Add the current red, green and blue values to the accumulators
  70.       rAcc += red(current);
  71.       gAcc += green(current);
  72.       bAcc += blue(current);
  73.      
  74.     }
  75.   }
  76.   // Average red, green and blue values by dividing the accumulator by the number of pixels
  77.   rAvg = rAcc / numPix;
  78.   gAvg = gAcc / numPix;
  79.   bAvg = bAcc / numPix;
  80.   rAvg  = constrain(rAvg,5, 255);
  81.   bAvg  = constrain(bAvg,5, 255);
  82.   gAvg  = constrain(gAvg,5, 255);
  83.   test1= +rAvg+","+gAvg+","+gAvg+'\r'; //create a comma seperated string in R,G,B format with a carriage return
  84.   // Set fill color to average value
  85.   fill(color(rAvg, gAvg, bAvg));
  86.   // Fill canvas with a rectangle
  87.   rect(0, 0, canvasWidth, canvasHeight);
  88.   set(0, 0, video);
  89.     rect(0, 0, 150, 150);
  90.   myPort.write(test1);          //send CSV string to the arduino
  91.  
  92.  // println(test1);
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement