User Tools

Site Tools


processing:video_capture

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
processing:video_capture [2013/08/04 03:53] mithatprocessing:video_capture [2013/08/15 22:40] – [Brightest pixel] mithat
Line 266: Line 266:
  
 You can render grayscale instead of color above by replacing the line <code java>color c = img.pixels[col + row*img.width];</code> with <code java>float c = brightness(img.pixels[col + row*img.width]);</code> You can render grayscale instead of color above by replacing the line <code java>color c = img.pixels[col + row*img.width];</code> with <code java>float c = brightness(img.pixels[col + row*img.width]);</code>
 +
 +
 +===== Brightest pixel =====
 +The following example takes the reduced resolution camera feed above and draws a colored circle inside the brightest pixel found. If there is more than one pixel with the same brightness, then it shows the first one (scanning from top-left to bottom-right). It also adds some code to indicate frame timing.
 +
 +<file java capture_brightest_pixel.pde>
 +/** Reduce resolution of captured image and indicate brightest pixel. 
 + * @author Mithat Konar
 + */
 +
 +import processing.video.*;
 +
 +// === Program constants === //
 +// Rendering parameters:
 +// number of cells the rendered image should be in each direction:
 +final int REDUCED_WIDTH = 32;
 +final int REDUCED_HEIGHT = 24;
 +
 +// Canvas parameters:
 +// number of times you want REDUCED image blown up:
 +final int OUTPUT_SCALE = 20;
 +// frame rate of rendered output:
 +final int CANVAS_FPS = 6;  // should divide evenly into CAM_FPS
 +// to avoid jitter.
 +
 +// Video capture parameters
 +// (adjust as neeeded for your platform's available capture options):
 +final int CAM_WIDTH = 320;
 +final int CAM_HEIGHT = 240;
 +final int CAM_FPS = 30;
 +
 +// === Global variables ===//
 +Capture cam;  // The video capture device.
 +PImage img;   // Buffer image.
 +
 +int blink_state = 0;
 +
 +// === GO! === //
 +void setup() {
 +  frameRate(CANVAS_FPS);
 +  size(REDUCED_WIDTH*OUTPUT_SCALE, REDUCED_HEIGHT*OUTPUT_SCALE);
 +  ellipseMode(CORNER);
 +
 +  if (Capture.list().length == 0) {
 +    println("There are no cameras available for capture.");
 +    exit();
 +  }
 +
 +  // Instantiate a buffer image used for subsampling, etc.
 +  img = createImage(REDUCED_WIDTH, REDUCED_HEIGHT, RGB);
 +
 +  // Instantiate a new Capture object, requesting the specs:
 +  cam = new Capture(this, CAM_WIDTH, CAM_HEIGHT, CAM_FPS);
 +  cam.start();  // In Processing 2.0, you need to start the capture device
 +}
 +
 +void draw() {
 +  int brightestCol = 0;
 +  int brightestRow = 0;
 +  float bightestIntensity = 0.0;
 +  
 +  // Grab a frame
 +  if (cam.available() == true) {
 +    cam.read();
 +  }
 +
 +  // We are using a buffer img because
 +  // cam.resize(REDUCED_WIDTH, REDUCED_HEIGHT);
 +  // doesn't work :-(
 +  img.copy(cam, 0, 0, CAM_WIDTH, CAM_HEIGHT, 0, 0, REDUCED_WIDTH, REDUCED_HEIGHT);
 +  img.loadPixels();
 +
 +  // For each column in img:
 +  for (int col = 0; col < REDUCED_WIDTH; col++) {
 +    // For each row in img:
 +    for (int row = 0; row < REDUCED_HEIGHT; row++) {
 +      // Draw the pixel intensity.
 +      float pixelIntensity = brightness(img.pixels[col + row*img.width]);
 +      fill(pixelIntensity);
 +      stroke(pixelIntensity);
 +      rect(col*OUTPUT_SCALE, row*OUTPUT_SCALE, OUTPUT_SCALE, OUTPUT_SCALE);
 +
 +      // Determine whether this is the brightest pixel in this frame.
 +      if (pixelIntensity > bightestIntensity)
 +      {
 +        bightestIntensity = pixelIntensity;
 +        brightestCol = col;
 +        brightestRow = row;
 +      }
 +    }
 +  }
 +
 +  // Highlight brightest pixel.
 +  fill(#000000, 0);
 +  stroke(#ff0000);
 +  strokeWeight(2);
 +  ellipse(brightestCol*OUTPUT_SCALE, brightestRow*OUTPUT_SCALE,
 +       OUTPUT_SCALE, OUTPUT_SCALE);
 +  
 +  // Frame timer
 +  fill(#000099, 100);
 +  stroke(#0000ff, 100);
 +  strokeWeight(1);
 +  blink_state = ++blink_state % CANVAS_FPS;
 +  rect(0, 0, blink_state*OUTPUT_SCALE, OUTPUT_SCALE);
 +}</file>
processing/video_capture.txt · Last modified: 2013/08/23 04:10 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki