25_OpenCV绘制圆、直线、矩形

导读:本篇文章讲解 25_OpenCV绘制圆、直线、矩形,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

本文的函数包括cv::circle、cv::line、cv::rectangle,分别用来绘制圆、直线、矩形。

目录

1. cv::circle

2. cv::line

3. cv::rectangle


1. cv::circle

OpenCV提供画圆的函数:cv::circle()。函数声明:

void circle(
	cv::Mat& img,  // image to be drawn on
	cv::Point center,  // location of circle center
	int radius,  // radius of circle
	const cv::Scalar& color,  // color RGB form
	int thickness = 1,  // thickness of line
	int lineType = 8,  // connectedness, 4 or 8
	int shift = 0  // bits of radius to treat as fraction
);

参数:
@img:绘图的图像;
@center:圆心,一个二维坐标;
@radius:半径;
@color/thickness/lineType/shift等,shift参数同时应用在半径和圆心上。

2. cv::line

cv::line在图像img上绘制一条从pt1到pt2的直线。直线自动被图像边缘切断。函数原型:

void line(
    cv::Mat& img,  // image to be drawn on
    cv::Point pt1,  // first endpoint of line
    cv::Point pt2,  // second endpoint of line
    const cv::Scalar& color,  // color BGR form
    int lineType = 8,  // connectedness, 4 or 8
    int shift=0  // bits of radius to treat fraction
);

3. cv::rectangle

cv::rectangle()函数在图像img上绘制一个从角点pt1到pt2的矩形。这个函数的另一种形式可以使用一个cv::Rect类型的参数r指定矩形的坐标和大小。函数原型:

void rectangle(
    cv::Mat& img,  // image to be drawn on
    cv::Point pt1,  // first corner of rectangle
    cv::Point pt2,  // opposite corner of rectangle
    const cv::Scalar color,  // color BGR form
    int lineType = 8,  // connectedness, 4 or 8
    int shift = 0  // bits of radius to treat as fraction
);
void rectangle(
    cv::Mat& img,  // image to be drawn on
    cv::Rect r,  // rectangle to draw
    const cv::Scalar& color,  // color BGR form
    int lineType = 8,  // connectedness, 4 or 8
    int shift=0  // bits of radius to treat as fraction
);

这三个函数的使用示例如下:

	cv::Mat img(400, 400, CV_8UC3, cv::Scalar(0, 0, 0));
	cv::circle(img, cv::Point(200, 200), 100, CV_RGB(255, 0, 0), 2, 4, 0);  // 画圆
	cv::circle(img, cv::Point(200, 200), 2, CV_RGB(0, 255, 0));  // 指明圆心
	cv::line(img, cv::Point(200, 200), cv::Point(300, 200), CV_RGB(0, 255, 0)); // 画直线
    cv::rectangle(img, cv::Point(100, 100), cv::Point(300, 300), CV_RGB(0, 0, 255));  // 画矩形

    cv::namedWindow("img");
	cv::imshow("img", img);
	cv::waitKey(0);

显示结果:

25_OpenCV绘制圆、直线、矩形

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/46165.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!