前言html
最近使用dlib庫的同時也會用到opencv,特別是因爲對dlib庫的畫圖函數不熟悉,都想着轉換到opencv進行show。本文介紹一下兩種開源庫中rectangle類型之間的轉換。函數
類型說明url
opencv中cv::Rect 以及opencv中的rectangle函數:spa
void cv::rectangle( InputOutputArray img, Point pt1, Point pt2, const Scalar & color, int thickness = 1, int lineType = LINE_8, int shift = 0)
或者.net
void cv::rectangle(Mat & img, Rect rec, const Scalar & color, int thickness = 1, int lineType = LINE_8, int shift = 0)
dlib中的rectangle類型:3d
rectangle ( long left_, long top_, long right_, long bottom_ );
或者code
template <typename T> rectangle ( const vector<T,2>& p1, const vector<T,2>& p2);
如何轉換htm
static cv::Rect dlibRectangleToOpenCV(dlib::rectangle r) { return cv::Rect(cv::Point2i(r.left(), r.top()), cv::Point2i(r.right() + 1, r.bottom() + 1)); }
或者blog
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); }
或者get
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類型;
參考
3.stackoverflow-convert-opencvs-rect-to-dlibs-rectangle;
完