Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Given an image of a rectangle (and assuming I could find the corners of that rectangle), what is the math I would need to determine where I am relative to the center of the rectangle, assuming that I am on a flat plane with the rectangle affixed to the wall. I have worked out the math to solve this problem if the camera is at the height of the rectangle, but I need to solve for a camera that is a couple feet below the rectangle. Here is what I have so far:


Beforehand, you could measure the height of the rectangle at say, 10 feet, to find the height (in px) in the image, once that is done, you can dynamically figure out the distance by averaging h1 and h2, and multiplying by the predetermined ratio. As the camera approaches 90° on either side of the rectangle, w approaches 0, so since the ratio of w/h is known before hand, you can use that to figure out what the width would be if you were viewing the rectangle straight on at a given distance. Then, you take the ratio of w to w', giving you a ratio ranging from 1 (when viewing straight on), to 0 (at 90°). Take the arccos of that, and you should get a degree measure, giving you all you need to have polar coordinates of the location of the camera based on the image.

However, when the camera is not at the height of the rectangle, there is vertical warping that will happen as well. How can I account for this in the calculations?

share|cite|improve this question

This is a well-studied problem in computer vision, with many resources available. One is this 1995 paper by Tan, Sullivan, Baker: "Recovery of Intrinsic and Extrinsic Camera Parameters Using Perspective Views of Rectangles," Computer and Information Science PDF link. Another is "Estimating Intrinsic Parameters of Cameras using Two Arbitrary Rectangles," Pattern Recognition, 2006. Essentially you want to invert the camera projection transformation. Often material can be found under the phrase camera calibration, e.g., this OpenCV link. Another key phrase is homography (e.g., Wikipedia article).

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.