Reputation: 305
Hi everyone I have a bounding box of faces from the face detection system in the format [x1, y1, x2, y2]
.
I want to crop out and align the face my current approach is as follows: calculate the angle from the eye position then rotate and crop using cv2 warpAffine function.
The problem is the new rotated bounding box is not completely covering the old bounding box, how I can calculate the size of the new bounding box so it completely contains the old one
center = (x1 + x2) // 2, (y1 + y2) // 2
d_y = eye_center[1] - mouth_center[1]
d_x = eye_center[0] - mouth_center[0]
angle = np.degrees(np.arctan2(d_y, d_x)) + 90
M = cv2.getRotationMatrix2D(center, angle, 1)
M[0, 2] += (width * 0.5) - center[0] # width is x2-x1 from face detector
M[1, 2] += (height * 0.5) - center[1] # height is y2-y1 from face detector
res_img = cv2.warpAffine(img, M, (width, height))
Upvotes: 0
Views: 931
Reputation: 80187
Dimensions of larger box are (for w,h = width, height
of smaller box, rotation angle Fi
):
H = w * Abs(Sin(Fi)) + h * Abs(Cos(Fi))
W = w * Abs(Cos(Fi)) + h * Abs(Sin(Fi))
Center remains the same, so base corner coordinates are
xx = centerx - (W-w)/2
yy = centery - (H-h)/2
Upvotes: 3