Convert OpenCV's Rect to dlib's rectangle?
Asked Answered
C

4

10

I use OpenCV's face detector with C++ for dlib's face alignment instead of dlib's detector because of slow speed.
To use dlib's face alignment, I have to pass the detection rectangle to the face alignment function.
However, I cannot do that even though dlib's detector is ok.
Because std::vector<rectangle> detsis used in dlib's sample code, I tried to assign as shown below, but I couldn't.
Note that detect_rect is face detection rectangle by OpenCV's detector.

dets[0].l = detect_rect.left;
dets[0].t = detect_rect.top;
dets[0].r = detect_rect.right;
dets[0].b = detect_rect.bottom;

Could you tell me any advice?

Thank you.

Careerist answered 19/1, 2016 at 8:24 Comment(3)
dlib uses .l .t .r .b? Can you explain how they must be interpreted? Is it maybe the distance from those image borders (so some kind of cropping)? If yes you'll have to use: .l = rect.x; .t = rect.y; .r = imageWidth - (rect.x+rect.width); .b = imageHeight - (rect.y+rect.height);Absorbefacient
Dlibs face detector is not slow. Are you sure you ran in release mode?Alum
Sorry, I could solve by myself! The next codes work! rectangle rect(left, top, right, bottom); dets.push_back(rect); Thank you!Careerist
B
23

It has to be noted that OpenCV uses the following definition:

OpenCV typically assumes that the top and left boundary of the rectangle are inclusive, while the right and bottom boundaries are not.

dlib's definition includes all boundaries, so the conversion function has to take care of shifting bottom right corner by 1.

Here's a function that I have in my Utils.h

static cv::Rect dlibRectangleToOpenCV(dlib::rectangle r)
{
  return cv::Rect(cv::Point2i(r.left(), r.top()), cv::Point2i(r.right() + 1, r.bottom() + 1));
}

And the other way around:

static dlib::rectangle openCVRectToDlib(cv::Rect r)
{
  return dlib::rectangle((long)r.tl().x, (long)r.tl().y, (long)r.br().x - 1, (long)r.br().y - 1);
}
Bolection answered 19/1, 2016 at 9:35 Comment(1)
Thank you for your advice. I could solve by myself! The next codes work! rectangle rect(left, top, right, bottom); dets.push_back(rect); I think my codes are the same to your code. Thank you for your answer!Careerist
A
7

The idea is right, but you're doing wrong in accessing cv::Rect's elements.

It should be:

dets[0].l = detect_rect.x;
dets[0].t = detect_rect.y;
dets[0].r = detect_rect.x + detect_rect.width;
dets[0].b = detect_rect.y + detect_rect.height;
Angie answered 19/1, 2016 at 8:29 Comment(1)
Thank you for your advice. I could solve by myself! The next codes work! rectangle rect(left, top, right, bottom); dets.push_back(rect); Thank you!Careerist
S
2

This answer is for Python.
You can use the construct of dlib rectangle wiz. dlib.rectangle(). You can use the OpenCV's facial bounding boxes
x = face[0] y = face[1] w = face[2] h = face[3]
and map them to dlib.rectangle(x, y, w, h).
Then you can call predictor code shape = predictor(img, rect)

Somali answered 5/2, 2020 at 11:39 Comment(0)
S
0

Convert OpenCV rectangle coordinates to DLIB rectangle coordinates in python:

If detections is list of rectangle coordinates obtained from Opencv

left = detections[0]
top = detections[1]
right = detections[2]
bottom = detections[3] 
dlibRect = dlib.rectangle(left, top, right, bottom) 

dlibRect will be dlib type rectangle.

Scanlan answered 29/6, 2021 at 19:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.