OpenCV入门(十三):常用图形绘制

绘制直线

@param img 输入图像
@param pt1 第一个点
@param pt2 第二个点
@param color 绘制颜色
@param thickness 线条粗细,负数则填充区域
@param lineType 画线算法,有四连通、8连通、反走样法
@param shift 坐标小数点精度

void line(InputOutputArray img, Point pt1, Point pt2, const Scalar& color,
                     int thickness = 1, int lineType = LINE_8, int shift = 0);
                     

绘制带箭头直线

@param tipLength 箭头线长度占线段长度的百分比

void arrowedLine(InputOutputArray img, Point pt1, Point pt2, const Scalar& color,
                     int thickness=1, int line_type=8, int shift=0, double tipLength=0.1);
                     

绘制矩形

@param rec 定义矩形左上角点,及其宽高

void rectangle(InputOutputArray img, Rect rec,
                          const Scalar& color, int thickness = 1,
                          int lineType = LINE_8, int shift = 0);

绘制圆形

void circle(InputOutputArray img, Point center, int radius,
                       const Scalar& color, int thickness = 1,
                       int lineType = LINE_8, int shift = 0);

绘制椭圆或弧

@param center 圆心
@param axes 长轴短轴的一半
@param angle 椭圆角度,单位 °
@param startAngle 弧的起始角度
@param endAngle 弧的结束角度

void ellipse(InputOutputArray img, Point center, Size axes,
                        double angle, double startAngle, double endAngle,
                        const Scalar& color, int thickness = 1,
                        int lineType = LINE_8, int shift = 0);

举个例子吧,老丝儿

void OpencvExample::Example_Drawing(Mat &image)
{
	//直线
	Point pt1;
	Point pt2;
	pt1.x = 0;
	pt1.y = 0;
	pt2.x = image.cols - 1;
	pt2.y = image.rows - 1;
	line(image, pt1, pt2, Scalar(255,0,0), 5, 8, 0);
	
	//带箭头的直线
	pt1.x = 0;
	pt1.y = image.rows - 1;
	pt2.x = image.cols - 50;
	pt2.y = 50;
	arrowedLine(image, pt1, pt2, Scalar(255, 0, 0), 5, 8, 0, 0.1);

	//矩形
	Rect rect;
	rect.x = 330;
	rect.y = 90;
	rect.width = 300;
	rect.height = 300;
	rectangle(image, rect, Scalar(0,0,255), 5, 8, 0);

	//圆
	Point center;
	int radius = 30;
	center.x = 431;
	center.y = 291;
	circle(image, center, radius, Scalar(0,255,0), 5, 8, 0);

	//椭圆
	Size axes = Size(60,30);
	double angle = 0;
	double startAngle = 0;
	double endAngle = 360;
	ellipse(image, center, axes, 0, 0, 360, Scalar(200,48,125), 5, 8, 0);

	//扇形
	axes = Size(30, 30);
	center.x = 529;
	center.y = 291;
	ellipse(image, center, axes, 0, 270, 360, Scalar(200, 200, 125), -1, 8, 0);

	imshow("dst",image);
}

结果图与原图结果图

原图

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值