k92
k92

Reputation: 385

How to calculate top co-ordinates of a rectangle given the bottom ones using opncv?

I want to draw a rectangle on a certain area of the face of which I have lower 2 co-ordinates (left and right).

Now I am trying to calculate the top 2 co-ordinates that would complete a rectangle. I have height as int value that I want to add.

How do I add this height to 2 bottom points? Say:

bottom_left = (x1, y1)
botoom_right = (x2, y2)

And I want to add 60 as heigh to both point above. So how do I calculate:

top_left = ?
top_right = ?

so that I can use:

cv2.rectangle(face, (bottom_left, top_left), (top_right, bottom_right), (255, 0, 0), 3)

enter image description here

Upvotes: 0

Views: 101

Answers (1)

Drew Hall
Drew Hall

Reputation: 29045

How about the following:

h = 60
deltaX = x2 - x1
deltaY = y2 - y1
w = math.sqrt(deltaX**2 + deltaY**2)

dxHat = -deltaY / w
dyHat = deltaX / w

dx = h * dxHat
dy = h * dyHat

top_left = (x1 + dx, y1 + dy)
top_right = (x2 + dx, y2 + dy)

This version corrects for the skewness of my previous (incorrect) trig-based solution by using the 2D perp operation explicitly (in the computation of dxHat and dyHat). It then scales by the desired h to compute the offsets from the original points.

Upvotes: 3

Related Questions