Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Processing color picker
- Shows a canvas filled with the average color seen by the video source (usually a webcam).
- Version 1.0
- --------------------------------------------
- Copyright 2013 DraftFCB Chicago
- Licensed under the Eclipse Public License, Version 1.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.eclipse.org/legal/epl-v10.html
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- //code mpdified from this code
- //https://github.com/dfcb/processing-color-averager/blob/master/processing_color_averager.pde
- import processing.serial.*;
- import processing.video.*;
- // Variable for capture device
- Capture video;
- Serial myPort;
- String test1 ="";
- int Colorsend = 0;
- int canvasWidth = 640;
- int canvasHeight = 480;
- int bt = 1;
- void setup() { String portName = Serial.list()[32]; //change the 0 to a 1 or 2 etc. to match your port
- myPort = new Serial(this, portName, 9600);
- size(canvasWidth,canvasHeight);
- video = new Capture(this, width, height, 30);
- video.start();
- // Remove border from shapes drawn
- strokeWeight(0);
- }
- void draw() {
- // Capture video
- if (video.available()) {
- video.read();
- }
- loadPixels();
- video.loadPixels();
- //red, green and blue accumulators
- int rAcc = 0;
- int gAcc = 0;
- int bAcc = 0;
- //red, green and blue value averages
- int rAvg = 0;
- int gAvg = 0;
- int bAvg = 0;
- //total number of pixels for calculating average color
- int numPix =(video.width * video.height);
- // Loop through the pixels
- for (int x = 0; x < video.width; x ++ ) {
- for (int y = 0; y < video.height; y ++ ) {
- int loc = x + y*video.width;
- color current = video.pixels[loc];
- // Add the current red, green and blue values to the accumulators
- rAcc += red(current);
- gAcc += green(current);
- bAcc += blue(current);
- }
- }
- // Average red, green and blue values by dividing the accumulator by the number of pixels
- rAvg = rAcc / numPix;
- gAvg = gAcc / numPix;
- bAvg = bAcc / numPix;
- rAvg = constrain(rAvg,5, 255);
- bAvg = constrain(bAvg,5, 255);
- gAvg = constrain(gAvg,5, 255);
- test1= +rAvg+","+gAvg+","+gAvg+'\r'; //create a comma seperated string in R,G,B format with a carriage return
- // Set fill color to average value
- fill(color(rAvg, gAvg, bAvg));
- // Fill canvas with a rectangle
- rect(0, 0, canvasWidth, canvasHeight);
- set(0, 0, video);
- rect(0, 0, 150, 150);
- myPort.write(test1); //send CSV string to the arduino
- // println(test1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement