Issues with web-camera and size

edited June 2016 in Library Questions

Hi, mates. I am kinda really new in Processing, but some things I already know and try to use it. Recently found an example of changing brightness of camera's pixels (it's like a flashlight). The code works, all fine and nice. But when I try to change the size of window(not camera), it ruins evething (photo). And don't know what to do. And one more thing what i saw. First we load all pixels, then cam.pixels. And in (loop in loop) we work with camera's pixels, but when we update Pixels, we update all pixels, not only from camera. And when i try to write cam.pixels[loc] = c; or cam.updatePixels(); it doesn't work, just gray screen. Could you help me or if know( i didn't found) links with similar problems. Thanks in advance.

    import processing.video.*;

    // Step 2. Declare a Capture object
    Capture video;

    void setup() {
      size(500, 500);

    video = new Capture(this, 320, 240,30); 
      video.start();
    }

    // An event for when a new frame is available
    void captureEvent(Capture video) {
      // Step 4. Read the image from the camera.
      video.read();
    }
    void draw() {

      loadPixels();
      video.loadPixels();

      for (int x = 0; x < video.width; x++) {
        for (int y = 0; y < video.height; y++) {

          // Calculate the 1D location from a 2D grid
          int loc = x + y*video.width;

          // Get the R,G,B values from image
          float r, g, b;
          r = red  (video.pixels[loc]);
          g = green(video.pixels[loc]);
          b = blue (video.pixels[loc]);

          // Calculate an amount to change brightness based on proximity to the mouse
          float d = dist(x, y, mouseX, mouseY);
          float adjustbrightness = map(d, 0, 100, 4, 0);
          r *= adjustbrightness;
          g *= adjustbrightness;
          b *= adjustbrightness;

          // Constrain RGB to make sure they are within 0-255 color range
          r = constrain(r, 0, 255);
          g = constrain(g, 0, 255);
          b = constrain(b, 0, 255);

          // Make a new color and set pixel in the window
          color c = color(r, g, b);
        pixels[loc] = c;
        }
      } updatePixels();
       }
Tagged:

Answers

  • int loc = x + y*video.width;
    
    ...
    
    pixels[loc] = c;
    

    you are reading from the camera which is 320 pixels wide but writing to the screen which is 500 pixels wide

    input

    AAAAAAAAAA
    BBBBBBBBBB
    CCCCCCCCCC
    

    output

    AAAAAAAAAABBBB
    BBBBBBCCCCCCCC
    CC
    

    etc

  • edited June 2016

    But how (I beg your pardon) use only camera's pixels on the screen? I tried to write video.pixels[loc] = c but nothing. So....I guess I need to create a new canvas with createGraphicson the screen and write it there? Or not?

  • edited June 2016 Answer ✓

    what you have is fine. you just need to ensure your output wraps where your input does. try replacing line 49 with:

    int loc2 = x + y * width;
    pixels[loc2] = c;
    

    (untested)

  • Dear savior, Thank you a lot ;;)

Sign In or Register to comment.