Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

Re : SOLVED - Blend video cam with background image

$
0
0

Here it is how to apply filters to a video cam:


  1. import processing.opengl.*;
  2. import codeanticode.glgraphics.*;
  3. import codeanticode.gsvideo.*;
  4. GSCapture cam;
  5. GLTexture texSrc, texFiltered;
  6. GLTextureFilter blur, emboss, edges, poster, pixelate, invert;
  7. GLTextureFilter currentFilter;
  8. String filterStr;
  9. PFont font;

  10. void setup() {
  11.   size(640, 480, GLConstants.GLGRAPHICS);
  12.   texSrc = new GLTexture(this);  
  13.   texFiltered = new GLTexture(this);   
  14.   cam = new GSCapture(this, 640, 480);
  15.   cam.setPixelDest(texSrc);
  16.   cam.start();
  17.   blur = new GLTextureFilter(this, "Blur.xml");
  18.   emboss = new GLTextureFilter(this, "Emboss.xml");
  19.   edges = new GLTextureFilter(this, "Edges.xml");
  20.   poster = new GLTextureFilter(this, "Posterize.xml");
  21.   pixelate = new GLTextureFilter(this, "Pixelate.xml");
  22.   invert = new GLTextureFilter(this, "Invert.xml");
  23.   font = loadFont("EstrangeloEdessa-24.vlw");
  24.   textFont(font, 24);     
  25.   currentFilter = edges;
  26.   filterStr = "edges";
  27. }

  28. void captureEvent(GSCapture cam) {
  29.   cam.read();
  30. }

  31. void draw() {
  32.   if (cam.ready()) {
  33.     if (texSrc.putPixelsIntoTexture()) {
  34.       // Calculating height to keep aspect ratio.     
  35.       float h = width * texSrc.height / texSrc.width;
  36.       float b = 0.5 * (height - h);
  37.       if (currentFilter == null) image(texSrc, 0, b, width, h);
  38.       else {
  39.         if (currentFilter == pixelate) {
  40.           currentFilter.setParameterValue("pixel_size", map(mouseX, 0, width, 1, 30));
  41.         }
  42.         if (currentFilter == invert) {
  43.           float x = map(mouseX, 0, width, 0, texSrc.width);
  44.           float y = map(mouseY, 0, height, 0, texSrc.height);
  45.           currentFilter.setParameterValue("mpos", new float[]{x, y});
  46.           currentFilter.setParameterValue("mdist", 50.0);             
  47.         }
  48.         texSrc.filter(currentFilter, texFiltered);
  49.         image(texFiltered, 0, b, width, h);           
  50.       }
  51.       fill(50, 200, 50);
  52.       if (currentFilter == null) text("Selected filter: none (1-6 to change, 0 to disable)", 10, 30);
  53.       else text("Selected filter: " + currentFilter.getName() + " (1-6 to change, 0 to disable)", 10, 30);
  54.     }    
  55.   }
  56. }

  57. void keyPressed() {
  58.   if ((key == '1')) { currentFilter = blur; }
  59.   else if ((key == '2')) { currentFilter = emboss; }
  60.   else if ((key == '3')) { currentFilter = edges; } 
  61.   else if ((key == '4')) { currentFilter = poster; }
  62.   else if ((key == '5')) { currentFilter = pixelate; } 
  63.   else if ((key == '6')) { currentFilter = invert; }
  64.   else if ((key == '0')) { currentFilter = null; }
  65. }

Viewing all articles
Browse latest Browse all 1768

Trending Articles