【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類型;

參考

1.opencv中的Rect類型

2.dlib中的rectangle類型

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

4.dlib和opencv之間類型格式的轉換


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM