Draw rectangle in OpenCV

前端 未结 2 1553
花落未央
花落未央 2021-02-04 08:15

I want to draw a rectangle in OpenCV by using this function:

rectangle(Mat& img, Rect rec, const Scalar& color, int thickness=1, int lineType=8, int shif         


        
2条回答
  •  伪装坚强ぢ
    2021-02-04 08:34

    The cv::rectangle function that accepts two cv::Point's takes both the top left and the bottom right corner of a rectangle (pt1 and pt2 respectively in the documentation). If that rectangle is used with the cv::rectangle function that accepts a cv::Rect, then you will get the same result.

    // just some valid rectangle arguments
    int x = 0;
    int y = 0;
    int width = 10;
    int height = 20;
    // our rectangle...
    cv::Rect rect(x, y, width, height);
    // and its top left corner...
    cv::Point pt1(x, y);
    // and its bottom right corner.
    cv::Point pt2(x + width, y + height);
    // These two calls...
    cv::rectangle(img, pt1, pt2, cv::Scalar(0, 255, 0));
    // essentially do the same thing
    cv::rectangle(img, rect, cv::Scalar(0, 255, 0))
    

提交回复
热议问题