# Find point coordinate in a projected rectangle

Hello OpenCV users.

I'm in front of a thought-it-was-simple problem, and as I am quite rusty concerning geometry and mathematics, I would like to request your help.

On the image below you can see a picture representing the problem This entire image represent the picture I get from my camera.

In this image I suceed to detect 4 points (A,B,C,D), forming a rectangle.

-I know the coordinates(in pixels) of these 4 points in my camera view.

-I also know the real dimensions of the rectangle I'm trying to detect.

Considering a point F in my camera view, I would like to calculate its coordinate in the frame formed by A,B,C,D. How to do this with openCV ?

edit retag close merge delete

Sort by » oldest newest most voted

First you need to find transformation from 4 points of source quadrilateral (A, B, C and D) to 4 points of some destination quadrilateral. In your case I guess it will be something like (0,0), (width,0), (width,height), (0,height). You can find them by use of getPerspectiveTransform. Output of getPerspectiveTransform is 3x3 matrix P of perspective transformation of 2D points (in homogeneous coordinates of course). You may apply it to F in order to get the coordinates you need. The calculation is:

Given F=(x,y). Multiply P by (x,y,1). Resulting vector will be (xr,yr,zr). Since this is homogeneous coordinates this is equivalent to (xr/zr, yr/zr, 1). So the coordinate of transformed F is (xr/zr, yr/zr).

more

1

you also can apply matrix P to point F using perspectiveTransform() function I think you do this easly using the following step.

1. Find slop of new x and y axis.
2. Draw a parallel line with the x axis with its slop from F.
3. Find the distance between these two parallel line and that will be your y.
4. Draw a line parallel to y axis from F and find the distance between these two line and that will be your x. more

@Haris, projected rectangle becames general quadrilateral, i.e. it is not a rectangle anymore and don't have parallel edges.

@Michael Burdinov In your question you mentioned it is rectangle that's why I gave such a solution. But in your image look like a quadrilateral.

The question is not mine but @ArnoG's. Yes he called it rectangle in the question, but the title of question is projected rectangle and the image is quadrilateral, so I assumed he is asking about general quadrilateral.

Official site

GitHub

Wiki

Documentation