【opencv基础】opencv和dlib库中rectangle类型之间的转换

前言

最近使用dlib库的同时也会用到opencv,特别是由于对dlib库的画图函数不熟悉,都想着转换到opencv进行show。本文介绍一下两种开源库中rectangle类型之间的转换。

类型说明

opencv中cv::Rect     以及opencv中的rectangle函数:

void cv::rectangle( InputOutputArray img, Point pt1, Point pt2, const Scalar & color, int thickness = 1, int lineType = LINE_8, int shift = 0)     

或者

void cv::rectangle(Mat & img, Rect rec, const Scalar & color, int thickness = 1, int lineType = LINE_8, int shift = 0)         

dlib中的rectangle类型:

rectangle ( long left_, long top_, long right_, long bottom_ );

或者

template <typename T> rectangle ( const vector<T,2>& p1, const vector<T,2>& p2);

如何转换

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

或者

dets.l = detect_rect.x;
dets.t = detect_rect.y;
dets.r = detect_rect.x + detect_rect.width;
dets.b = detect_rect.y + detect_rect.height;

其中detect_rect是opencv类型,dets是dlib类型;

扫描二维码关注公众号,回复: 3676084 查看本文章

参考

1.opencv中的Rect类型

2.dlib中的rectangle类型

3.stackoverflow-convert-opencvs-rect-to-dlibs-rectangle

4.dlib和opencv之间类型格式的转换

猜你喜欢

转载自www.cnblogs.com/happyamyhope/p/9831055.html
今日推荐