我想使用这个函数在 OpenCV 中绘制一个矩形:
rectangle(Mat& img, Rect rec, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )
但是当我使用它时,我遇到了一些错误。我的问题是:任何人都可以用一个例子来解释这个功能吗?我找到了一些例子,但有另一个功能:
rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
第二个函数的例子:
rectangle(image, pt2, pt1, Scalar(0, 255, 0), 2, 8, 0);
这个函数我理解,但是第一个函数我在参数 Rect
中遇到了问题。我不知道我怎样才能更致命?
最佳答案
接受两个 cv::Point
的 cv::rectangle
函数接受矩形的左上角和右下角(分别为 pt1 和 pt2在 documentation 中)。如果该矩形与接受 cv::Rect
的 cv::rectangle
函数一起使用,那么您将得到相同的结果。
// 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))
关于c++ - 在 OpenCV 中绘制矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40120433/