## Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

• Help others & share knowledge
• Earn cash & points
Solved

# HomographyWarp -> Processing

Posted on 2011-03-12
368 Views
Right, I am trying to create a method for a homography warp. Problem is, I suck at linear algebra (actually, didn't do so bad, but it's been several years since I've taken it). And there's no book for this course.

Could anyone out there give me the idiot's version of how to implement this?

Thanks,
Ryan

``````/// Creates a new image that is width x height pixels.
/// The pixels of this image are set by applying the inverse homography (provided can be either
///
/// @param source       Image to inverse warp
/// @param H            Homography matrix
/// @param width        Width of image to create
/// @param height       Height of image to create
/// @returns            A width x height sub-region of warped source image
///
PImage homographyWarp(PImage source, Matrix H, int width, int height) {
//
// @todo
//    Inverse warp source to get the pixel values for the new image
//    Don't forget to divide the image coordinates with w (the 3rd coordinate) when you convert
//    the homogeneous coordinates back to image coordinates
//    Also, be sure to use bilinear (or some other) interpolation to get a smooth result.
//
// @note
//    Remember that accessing a negative index'd pixel, or a pixel outside of the image will
//    return a black pixel. This means you do not have to worry about the size of the image.
//

PImage result = createImage(width, height, RGB);
return result;
}
``````
0
Question by:rossryan
• 7
• 5

Author Comment

ID: 35119399
Actually, I'd settle for a proper explanation of image warping and homography. Wikipedia isn't looking so hot.
0

LVL 37

Expert Comment

ID: 35123955
Let's say you have an image taken by a camera at a certain point. Now you want to move everything so that it looks like it was taken from a different location. You can't just shift everything evenly - some things will be closer together in the second image and some will be farther apart. The process of 'changing camera angles' after the fact is what is being called homography. If you look really closely past all the technical mumbo-jumbo you can see that wikipedia is saying the same thing, just in a longer winded way.
In the general case, you would need the 3D coordinates of the objects in the image so that you know where to put them all. The homography matrix is the transformation matrix that specifies how everything must rotate.
0

LVL 37

Expert Comment

ID: 35123990
http://opencv.willowgarage.com/documentation/camera_calibration_and_3d_reconstruction.html#findhomography
OpenCV (which I use) has a routine to find the homography matrix between two images, but it appears you are trying to find the second image given the matrix.
Note: there are several known methods for doing this. I assume (from the way the comments make it look like an academic assignment) that you were given methods in your book or in class that you should use.
If you remember anything from linear algebra, try to remember transformation matrices. That's all this is really.
The simplest (but somewhat technical) definition is that homographic warping is just changing the coordinates of a 3D space and projecting it onto a 2D space.
0

Author Comment

ID: 35123999
I feel ya, but my brain isn't cooperating.

I'm trying to find an equation with focal length and vanishing points to get some planes, and help convert it into a 3D box, but I am coming up short.
0

LVL 37

Expert Comment

ID: 35124063
I think your problem is that you are trying to make the problem harder than it really is. You already have the homography matrix, so you shouldn't need to explicitly mess with a 3D space at all. I don't know what you are expected to know at this point, but if you step back and review your lecture notes and book if you have them, you should see the answer fall out. It's just going to be a good deal of matrix algebra.
Work on it for a bit, but if you get stuck, give some more detail and context and I'll see if I can shed some light on the darker areas.
0

Author Comment

ID: 35124106
Well, yeah. It's for another part, which I am working on, and just not getting.

I am just not understanding how we can calculate 3D points from a 2D image + focal length and a vanishing point.

I've browsed around looking for information at other universities, and they are all using the same slides as my instructor. I can't find a single rephrasing of the equations I am supposed to use, or the knowledge I am supposed to be sporting, because they are quite literally reusing the same exact slides.

It's incredibly frustrating, and my usual solution is to read someone else's take on an algorithm if the primary instructor is unclear, but of the few dozen university that I've found on Google, the slides are the same, and there is zero elaboration of the algorithms they use.
``````///
/// Sets the 4 points of each of the 5 'Rectangle3D' objects.
///
/// @todo Set nw, ne, se, sw for each retangle
///
/// @param focalLength      Focal length for image
/// @param vanishingPoint   2D image location of vanishing point
/// @param Quadrilaterals   5 quadrilaterals, one for each visible wall in the image
/// @param Rectangle3Ds     5 3D rectangle objects
///
/// @post  The 5 Rectangle3D objects have had their points updated
///
void get3DPlanes(float focalLength, Point vanishingPoint,
Rectangle3D back3D, Rectangle3D left3D, Rectangle3D top3D, Rectangle3D right3D, Rectangle3D bottom3D) {

//
// @todo
//    1. The width and height ratio of the back plane directly tells you the aspect
//       ratio of the box, so specifying the 3D height gives you the 3D width.
//    2. The back plane's height is related to the depth of the box
//       (see lecture slide: Depth of Box), so specifying the 3D height gives you the 3D depth
//    3. Once you compute the 3D width, height, and depth of the box, you can compute the 3D
//       coordinates of all corners points of the 5 surfaces (you may set the origin to be the
//       top left corner of the wall surface, 3D X axis aligned to the horizontal axis of the
//       image, the 3D Y axis aligned to the vertical axis, and the 3D Z axis aligned to the
//       depth axis of the box).
//    4. Store the 3D coordinates for each surface in the corresponding Rectangle object
//
// @note When you compute these 3D coordinates, don't forget to account for the fact that the
//       surfaces are not exactly aligned with the box (see the image displayed before calculation
//       and notice that the front end of the surfaces are not aligned).
//       You need to adjust the depth coordinates of front points of each surface accordingly
//       using the 2D image coordinates to get proper results.
//
//       Also, first compute the 3D coordinates for the back plane since those can be reused in
//       other surfaces.
//

Point3D Backplane_NW = new Point3D();
Point3D Backplane_NE = new Point3D();
Point3D Backplane_SE = new Point3D();
Point3D Backplane_SW = new Point3D();

}
``````
0

LVL 37

Expert Comment

ID: 35124234
The focal length and vanishing point should translate to a third dimension easily with some basic trig (assuming you have a simple enough image, hot air balloons of varying sizes in the middle of a blue sky would be basically impossible to get a depth on).

Can you link to the slides you mention? I am usually able to find better ways of explaining things.
0

Author Comment

ID: 35124257
0

Author Comment

ID: 35124305
I need only understand these two pieces.

And, if at all possible, I'd like to understand this stuff tonight (project comes due at midnight, so the sooner, the better). No pressure ^_^.

Normally, this stuff isn't an issue, but like I said, it's been several years since I've done any linear algebra, and I am unlikely to relearn everything in the next three hours.

-Ryan
0

Author Comment

ID: 35124444
0

Author Comment

ID: 35124503
Can't figure it out either?
0

LVL 37

Accepted Solution

TommySzalapski earned 500 total points
ID: 35125693
The slides from 59-73 really seem to give all the needed information. Which parts are you stuck on specifically? Slide 73 gives all the equations you need to get the 3D coordinates (with trig too, not linear algebra).
My guess is that you hit a brain block because you were trying to figure it out too fast since it was due last night (assuming you are in eastern time as it says in the original post).
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question