在OpenCV中绘制矩形

Ade*_*tem 7 c++ opencv

我想使用此函数在OpenCV中绘制一个矩形:

rectangle(Mat& img, Rect rec, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )
Run Code Online (Sandbox Code Playgroud)

但是当我使用它时,我遇到了一些错误.我的问题是:有人能用一个例子来解释这个功能吗?我发现了一些例子,但有另一个功能:

rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
Run Code Online (Sandbox Code Playgroud)

这个例子关于第二个函数:

rectangle(image, pt2, pt1, Scalar(0, 255, 0), 2, 8, 0);
Run Code Online (Sandbox Code Playgroud)

我理解这个功能,但是第一个功能我面临参数问题Rect.我不知道怎么会变得更加致命呢?

Rob*_*ost 10

cv::rectangle接受两个的函数cv::Point同时采用矩形的左上角和右下角(文档中分别为pt1和pt2 ).如果该矩形与cv::rectangle接受a 的函数一起使用cv::Rect,那么您将得到相同的结果.

// 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))
Run Code Online (Sandbox Code Playgroud)


Sar*_*wal 6

这是在图像上绘制预定义矩形的简单示例

using namespace cv;

int main(){
Mat img=imread("myImage.jpg");

Rect r=Rect(10,20,40,60);
//create a Rect with top-left vertex at (10,20), of width 40 and height 60 pixels.

rectangle(img,r,Scalar(255,0,0),1,8,0);
//draw the rect defined by r with line thickness 1 and Blue color

imwrite("myImageWithRect.jpg",img);


return 0;
}
Run Code Online (Sandbox Code Playgroud)