【问题标题】:Convert OpenCV's Rect to dlib's rectangle?将 OpenCV Rect 转换为 dlib 矩形?
【发布时间】:2016-04-24 15:04:42
【问题描述】:

由于速度慢,我使用 OpenCV 的人脸检测器和 C++ 进行 dlib 的人脸对齐,而不是 dlib 的检测器。
要使用 dlib 的人脸对齐,我必须将检测矩形传递给人脸对齐函数。
但是,即使 dlib 的检测器没问题,我也不能这样做。
因为std::vector<rectangle> detsdlib's sample code中使用,所以我尝试如下分配,但我不能。
请注意,detect_rect 是 OpenCV 检测器的人脸检测矩形。

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

你能告诉我任何建议吗?

谢谢。

【问题讨论】:

  • dlib 使用 .l .t .r .b?你能解释一下它们必须如何解释吗?可能是与这些图像边界的距离(所以某种裁剪)?如果是,您将不得不使用:.l = rect.x;.t = rect.y;.r = imageWidth - (rect.x+rect.width);.b = imageHeight - (rect.y+rect.height);
  • Dlibs 人脸检测器并不慢。你确定你是在发布模式下运行的吗?
  • 对不起,我可以自己解决!下一个代码有效! rectangle rect(left, top, right, bottom);dets.push_back(rect);谢谢!

标签: c++ opencv rect dlib


【解决方案1】:

这个想法是对的,但是你在访问cv::Rect的元素时做错了。

应该是:

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;

【讨论】:

  • 感谢您的建议。我可以自己解决!下一个代码有效! rectangle rect(left, top, right, bottom);dets.push_back(rect);谢谢!
【解决方案2】:

需要注意的是,OpenCV 使用如下定义:

OpenCV 通常假定矩形的上下边界包含,而左右边界不包含。 p>

dlib 的定义包括所有边界,因此转换函数必须注意将右下角移动 1。

这是我的 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));
}

反之亦然:

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);
}

【讨论】:

  • 感谢您的建议。我可以自己解决!下一个代码有效! rectangle rect(left, top, right, bottom); dets.push_back(rect); 我认为我的代码与您的代码相同。谢谢你的回答!
【解决方案3】:

此答案适用于 Python。
您可以使用 dlib 矩形向导的构造。 dlib.rectangle()。您可以使用 OpenCV 的面部边界框
x = face[0] y = face[1] w = face[2] h = face[3]
并将它们映射到dlib.rectangle(x, y, w, h)
然后就可以调用预测代码shape = predictor(img, rect)

【讨论】:

    【解决方案4】:

    在python中将OpenCV矩形坐标转换为DLIB矩形坐标:

    如果检测是从 Opencv 获得的矩形坐标列表

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

    dlibRect 将是 dlib 类型的矩形。

    【讨论】:

      猜你喜欢
      • 2014-05-07
      • 2019-10-12
      • 1970-01-01
      • 2021-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-09
      • 1970-01-01
      相关资源
      最近更新 更多