mirroring web cam image?

What's the best/quickest way to get the web cam image mirrored so that it mirrors your movements?

Creating a new PImage and looping through the original pixels, 'moving them from left to right'? scale(-1,1) is not an option in this case since I want to use, and refer to, the pixels from the web cam image.

Tagged:

Answers

  • edited March 2017

    Normally scale(-1,1); image(cam); is the easiest.

    If you can't use scale(), just loop through pixels using a nested loop with your x loop run backwards. Increment the pixel offset counter by width each y loop line, decrement back down 1 px at a time until width times in the inner loop.

    Alternately, call scale(-1,1) and then loop through pixels[] in normal order with a non-nested loop, as per the standard example. Modify values as needed. This code approach is also much easier to read!

    Re:quickest, try both approaches and benchmark. They should both be fast, but results can sometimes be surprising.

    You mention "creating a new PImage" -- that shouldn't be necessary in either case.

    P.S. Why is this under "Library Questions"?

  • It was under 'library questions' because something went wrong. I've corrected it.

    Regarding scale(), wouldn't it have to be image(cam,-width, 0); then?

    How would I modify it without a non-nested loop?

    Right now I'm doing it like this:

    void flipImage() {
      img.loadPixels();
      movie.loadPixels();
      for (int y = 0; y<height; y++) {
        for (int x = 0; x<width; x++) {
          img.pixels[y*width+x] = movie.pixels[y*width+(width-1-x)];
        }
      }
      img.updatePixels();
    }
    

    And I'm creating a PImg called 'img' because I want to sample info from it later on (and not from 'movie', because 'movie' is not mirrored correctly).

Sign In or Register to comment.