Trigonometry/Geometry problem using a photograph

Train

Lifer
Jun 22, 2000
13,581
80
91
www.bing.com
So I have a digital photo. In this photo is a rectangle, could be a picture on a wall, a window, a door, anything that is rectangular in the "real world", however in the photograph(2D) it may be at an angle, so as far as the 2D image is concerned, it's likely NOT a rectangle.

Knowing the 2D coordinates of the 4 points of the rectangle, how can I determine the angle(X and Y rotation) of the rectangle from the camera? Assuming looking directly at the rect (all right angles in 2D) is zero degrees X rotation and zero degrees Y rotation.

Would appreciate any and all ideas on how to solve this. It doesnt matter to me if its easier for you to speak in angles or radians, I can use either.
 

CycloWizard

Lifer
Sep 10, 2001
12,348
1
81
Do you know the true dimensions of the object? If so, it's pretty easy. If not, it's a bit more complicated. Let me know and I'll write it up accordingly a little later.
 

silverpig

Lifer
Jul 29, 2001
27,703
12
81
You will also need the focal length of the lens used to take the picture. Taking a picture of a rectangle with a fish eye lens will distort it.
 

Train

Lifer
Jun 22, 2000
13,581
80
91
www.bing.com
I certainly won't be using any fish eye lenses, and the slight distortion caused by regular cameras is more than acceptable for what I am trying to do.
 

CycloWizard

Lifer
Sep 10, 2001
12,348
1
81
Well, sorry it took so long... I've been in the lab way too much this week.

Anyway, I just got to looking at this and I came up with something very brief that should at least get you started. If you can neglect errors due to distortion, then the following should work. I also assumed that two of the four edges are vertical, but obviously this isn't always the case. If you let me know a little bit more about what you're trying to do, then I can go in deeper and remove some of these assumptions, but I'm too tired to worry much about it tonight. It just so happens that this problem is identical to determining the rotation of a rigid body, but I'm trying to work it out from scratch since I don't have my books handy (and I want to see if I can actually do it :p). A simpler method that I am more familiar with would be to use a deformation gradient tensor, but that has a lot more math involved and it's too late on a Friday night to even mention it.

Link

edit: Bah! Apparently that site can no longer be seen off campus.
 

Train

Lifer
Jun 22, 2000
13,581
80
91
www.bing.com
Cyclo,
I got the PDF you sent me, thanks, I understand it, but havent had much time to look at it closer and try it out in my app. I will hopefully have a test app written for it this week.

I also assumed that two of the four edges are vertical
What do you mean by "vertical" ? as in parallel to the Y axis? (I'm using Left Handed Cartesian for the 3D portion) No, they could be at any rotation at all. I guess the only truth of this rectangle is that it can NOT be rotated beyond to 90 degrees or more on any axis, since it is based on something visible (in the photo) it HAS to be visible in the 3D representation. And rotating a 3D plane that represents the rect more than 89.999 degs on any axis would make it invisible.
 

CycloWizard

Lifer
Sep 10, 2001
12,348
1
81
Originally posted by: Train
What do you mean by "vertical" ? as in parallel to the Y axis? (I'm using Left Handed Cartesian for the 3D portion) No, they could be at any rotation at all. I guess the only truth of this rectangle is that it can NOT be rotated beyond to 90 degrees or more on any axis, since it is based on something visible (in the photo) it HAS to be visible in the 3D representation. And rotating a 3D plane that represents the rect more than 89.999 degs on any axis would make it invisible.
You're right... I typed that reply then edited the file I sent you. The stuff I sent you is valid for any arbitrary orientation of the lines I believe. I did write it late at night though, so let me know if you get it working or find something incorrect and I can take a closer look to see where I went wrong.
 

hypn0tik

Diamond Member
Jul 5, 2005
5,866
2
0
Originally posted by: CycloWizard
Thanks hyp. Let me know if anything in there needs to be clarified or corrected.

No worries at all.

The pdf looks good. The problem itself is an exercise in projecting lines onto axes and planes (assuming of course no distortion caused by the lens).

However, there is an issue that needs to be addressed.

What about the situation where there is rotation in 2 axes? 3 axes?

We would need additional information (the 3rd dimension of the object). If that information is unknown, there is not enough information to solve the problem. I could be mistaken but I don't think this problem is solvable if there is rotation in all 3 axes.
 

CycloWizard

Lifer
Sep 10, 2001
12,348
1
81
Originally posted by: hypn0tik
Originally posted by: CycloWizard
Thanks hyp. Let me know if anything in there needs to be clarified or corrected.

No worries at all.

The pdf looks good. The problem itself is an exercise in projecting lines onto axes and planes (assuming of course no distortion caused by the lens).

However, there is an issue that needs to be addressed.

What about the situation where there is rotation in 2 axes? 3 axes?

We would need additional information (the 3rd dimension of the object). If that information is unknown, there is not enough information to solve the problem. I could be mistaken but I don't think this problem is solvable if there is rotation in all 3 axes.
I think you could still solve it as long as you assumed the z value for one of the corners in the rotated image. You might have to do it iteratively such that you calculate all of the values for the other corners, then check and make sure that your initial guess is correct. If it's not, then pick a new value. Sounds bad, but it would be easily programmable. Not 100% sure this is correct, but I don't have time to think about it now. The woman's parents are in town. :p

edit: I don't think you would even need to solve this kind of problem iteratively. You would simply choose a z value for one point, then find all of the others relative to it. A simple scaling factor could be applied to the resulting coordinates to recover the correct lengths.
 

Train

Lifer
Jun 22, 2000
13,581
80
91
www.bing.com
don't the dimensions of the rect let you solve for Z as well? Knowing what the projected points of all four corners is, and knowing thier size, cant the z values of all points be calculated?
 

CycloWizard

Lifer
Sep 10, 2001
12,348
1
81
Originally posted by: Train
don't the dimensions of the rect let you solve for Z as well? Knowing what the projected points of all four corners is, and knowing thier size, cant the z values of all points be calculated?
I think you can solve for all the z values but one. My analysis that I sent you assumed that all of the coordinates for one end of the line segment were known (and I set them as the origin such that all of the calculated coordinates were relative to these). Thus, in the case where you don't know any of the z coordinates, you'd have to assume one of them, then find all of the others relative to this one, then scale it according to the known dimensions. I don't know if there is any other way to do it, but I don't believe there is. It's not hard though.
 

homercles337

Diamond Member
Dec 29, 2004
6,340
3
71
It may have already been answered, but i didnt see anything mentioning affine transformations (shear, scale, rotation, translation) in that PDF (though it seems to suggest it). This will take care of what you need. It will also give you a "map" (your trans matrix) to transform the entire image. I have written code to do this, but i was only dealing with small scale transformations.
 

CycloWizard

Lifer
Sep 10, 2001
12,348
1
81
Originally posted by: homercles337
It may have already been answered, but i didnt see anything mentioning affine transformations (shear, scale, rotation, translation) in that PDF (though it seems to suggest it). This will take care of what you need. It will also give you a "map" (your trans matrix) to transform the entire image. I have written code to do this, but i was only dealing with small scale transformations.
This is basically what I did in the PDF, just omitted the language to bring it back to a high school level. In the end, it's simple geometry/trigonometry. What would make the problem really interesting would be to have finite, non-affine deformations involved. :D
 

firewolfsm

Golden Member
Oct 16, 2005
1,848
29
91
So, if you're doing this for the math, then forget it.

But I've used to the photoshop correction filter to find that out. Just adjust the angles until they line up.