We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
flipping video (Read 1911 times)
flipping video
Jul 9th, 2008, 9:45pm
 
does anyone know a way to flip a cam feed in real time/ or close to it?
Re: flipping video
Reply #1 - Jul 10th, 2008, 1:57pm
 
I dunno if there's an easier way, but I do this (I have to do this loop anyway for lots of other processing - more info here http://www.memo.tv/webcam_piano_with_processing_v0_1):

Code:

video.read(); // Read the new frame from the camera
video.loadPixels();

for (int i=0; i<numPixels; i++) {
int posX = i % video.width; // x coordinate of pixel
int posY = floor(i / video.width); // y coordinate of pixel

if(flipHorizontal) posX = video.width - posX - 1; // flip x coordinate if we want to flip
imgCam.pixels[posY * video.width + posX] = video.pixels[i];
}
imgCam.updatePixels();


Re: flipping video
Reply #2 - Jul 10th, 2008, 2:06pm
 
There is a much easier way to do it...

Code:
beginShape();
texture(video);
vertex(0,0,0,video.height);
vertex(width,0,video.width,video.height);
vertex(width,height,video.width,0);
vertex(0,height,0,0);
endShape();
Re: flipping video
Reply #3 - Jul 10th, 2008, 3:43pm
 
thank you both very much. i'll be using memo's solution as it fits with some of the other things i'm implimenting.

thanks

henry
Re: flipping video
Reply #4 - Jul 18th, 2008, 12:30am
 
The first solution didn't work for me so I wrote myself this function.
It returns a copy of the provided image rotated by -90 (or 270), 90 or 180 degrees.
It works also with videos of course (I'm using it for face detection on a portrait webcam input - yes, I'm rotating the webcam too ;)
Use it as you wish and let me know if it does help.

Code:

PImage rotateImage(PImage img, int angle)
{
 PImage rot;
 int numPixels = img.height*img.width;
 if (abs(angle) == 180)
 {
   rot = new PImage(img.width,img.height);
 }
 else
 {
   rot = new PImage(img.height,img.width);
 }
 img.loadPixels();
 
 int rotpx = 0;
 for (int i = 0; i < numPixels; i++)
 {
     int x0 = i % img.width;
     int y0 = floor(i / img.width);
     
     // for each pixel calculates new x & y coordinates and index
     if (angle == 90)
     {
       int x1 = abs(y0 + 1 - img.height);
       int y1 = x0;
       rotpx = x1 + (y1 * rot.width);
     }
     else if (angle == -90 || angle == 270)
     {
       int x1 = y0;
       int y1 = abs(x0 + 1 - img.width);
       rotpx = x1 + (y1 * rot.width);
     }
     else if (angle == 180 || angle == -180)
     {
       // 180 is the easiest, just flip the array index...
       rotpx = numPixels - 1 - i;
     }
     else
     {
       // angles other than 90/180 are far more complicated...
       println("Not implemented, sorry :)");
     }
     
     rot.pixels[rotpx] = img.pixels[i];
 }
 rot.updatePixels();
 return rot;
}

Page Index Toggle Pages: 1