c++ - 在 OpenCV 中绘制矩形

标签 c++ opencv

我想使用这个函数在 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::Pointcv::rectangle 函数接受矩形的左上角和右下角(分别为 pt1 和 pt2在 documentation 中)。如果该矩形与接受 cv::Rectcv::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/

相关文章:

python - OpenCV-查找Canny边缘的轮廓

c++ - Opencv << cout 和写入 FileStorage 的运算符错误

c++ - 在一个解决方案中共享类

c++ - vector n 维 vector 本身具有特定的数据类型

visual-c++ - 在Windows 7命令行中使用OpenCV 2.4编译应用

java - OpenCV 应用程序 - Android 相机在 10 秒后崩溃

c++ - 在 argv[] 开头添加参数

c++ - 编写完一个游戏后,如何绑定(bind)可执行文件中的 Assets 并打包,以便分发给其他人?

c++ - 在其定义中创建一个实例

python - 如何找到二值图像的最低点?