25_OpenCV绘制圆、直线、矩形

本文的函数包括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);

显示结果:

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值