cloister
Full Member
Offline
Posts: 138
Re: Image warping/morphing
Reply #5 - Mar 3rd , 2009, 6:54pm
While mapping the image to a polygon surface and then deforming the surface will work, that is IMHO a very overkill method. Image warping is really just a process for taking each coordinate in the destination image, mapping it through some function to yield coordinates in the original image, then copying the pixel at the mapped coordinates in the original image to the proper coordinates in the destination image. If your goal is to play with image warping and get a sense for how that works, IMHO, don't distract yourself with all this business of building a polygon mesh and texture mapping the image onto it. That technique certainly has its place in the world, but a more straightforward method of mapping pixels from source image to destination image gives you a much more direct appreciation for what's going on, it's a lot shorter to code, and makes it a lot easier for you just to play around with different formulas to see what happens to the image. Here's a quick example to get you started: PImage source, destination; void setup() { source = loadImage("liberty_3.jpg"); // fill in your own image here size(source.width, source.height); noLoop(); destination = warp(source); } void draw() { image(destination, 0, 0); } // implement a simple vertical wave warp. PImage warp(PImage source) { float waveAmplitude = 20, // pixels numWaves = 5; // how many full wave cycles to run down the image int w = source.width, h = source.height; PImage destination = new PImage(w,h); source.loadPixels(); destination.loadPixels(); float yToPhase = 2*PI*numWaves / h; // conversion factor from y values to radians. for(int x = 0; x < w; x++) for(int y = 0; y < h; y++) { int newX, newY; newX = int(x + waveAmplitude*sin(y * yToPhase)); newY = y; color c; if(newX >= w || newX < 0 || newY >= h || newY < 0) c = color(0,0,0); else c = source.pixels[newY*w + newX]; destination.pixels[y*w+x] = c; } return destination; }