在OpenCV中绘制矩形 [英] Draw rectangle in OpenCV

查看:129
本文介绍了在OpenCV中绘制矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用此功能在OpenCV中绘制一个矩形:

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 shift=0 )

但是当我使用它时,我遇到了一些错误.我的问题是:任何人都可以用示例解释该功能吗?我找到了一些示例,但具有另一个功能:

But when I use it I am facing some errors. My question is: can anyone explain the function with an example? I found some examples but with another function:

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

关于第二个功能的此示例:

This example on the second function:

rectangle(image, pt2, pt1, Scalar(0, 255, 0), 2, 8, 0);

我了解此功能,但是使用第一个功能时,我遇到了参数Rect中的问题.我不知道该如何致命?

This function I understand, but with the first function I'm facing a problem in parameter Rect. I don't know how I can deadlier it?

推荐答案

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

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))

这篇关于在OpenCV中绘制矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆