图形的绘制与填充

opencv中提供了很多绘制图形的函数,可以方便的进行图形绘制

直线绘制:line()

函数原型: 

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

参数声明:

  • InputOutputArray img:输出图像
  • Point pt1:线段的第一个点
  • Point pt2:线段的第二个点
  • const Scalar& color:直线颜色
  • int thickness = 1:直线粗细程度
  • int lineType = LINE_8:直线类型
  • int shift = 0:点坐标的小数点位数

矩形绘制:rectangle()

函数原型:

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

函数声明:

  • CV_IN_OUT Mat& img:输出图像
  •  Rect rec: 矩形的位置和长宽
  •  const Scalar& color:矩形颜色
  • int thickness = 1:线宽
  • int lineType = LINE_8:直线类型
  •  shit:点坐标的小数点位数

圆形绘制:circle()

函数原型:

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

函数声明:

  • img  图像
  • center 圆心
  • radius 半径
  • color 颜色
  • thickness 线宽
  • linetype 线型
  • shift 坐标点的小数点位数

椭圆绘制:ellipse()

函数原型:

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

函数声明:

  • img 图像
  • center 椭圆原心
  • axes  椭圆x轴长度的一半,y轴长度的一半
  • angle 椭圆旋转角度
  • startAngle 起始角度
  • endAngle 终止角度
  • color 椭圆颜色
  • thickness 线宽
  • linetype 线型
  • shift 坐标小数点位数

示例

 1 #include <iostream>
 2 #include <opencv2/opencv.hpp>
 3 using namespace std;
 4 using namespace cv;
 5 
 6 int main(int argc, char** argv) {
 7     Mat canvas = Mat::zeros(Size(512, 512), CV_8UC3);
 8     namedWindow("canvas", WINDOW_AUTOSIZE);
 9 
10     //绘制API演示
11     line(canvas, Point(10, 10), Point(400, 400), Scalar(255, 0, 0), 1, 8);
12     Rect rect(100, 100, 200, 200);
13     rectangle(canvas, rect, Scalar(255, 0, 0), 1, 8);
14     circle(canvas, Point(255, 255), 100, Scalar(0, 255, 0), 1, 8);
15     putText(canvas, "hello opencv", Point(100, 50), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0,255,0),2,8);
16     RotatedRect rt;
17     rt.center = Point2f(255, 255);
18     rt.angle = CV_PI / 4;
19     rt.size = Size(100,200);
20     ellipse(canvas, rt, Scalar(0, 255, 255), -1, 8);
21     imshow("canvas", canvas);
22 
23     Mat image = Mat::zeros(Size(512, 512), CV_8UC3);
24     int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
25     RNG rng(12345);
26     int rrg;
27     while (true) {
28         x1 = (int)rng.uniform(0, 512);
29         x2 = (int)rng.uniform(0, 512);
30         y1 = (int)rng.uniform(0, 512);
31         y2 = (int)rng.uniform(0, 512);
32         line(image, Point(x1, y1), Point(x2, y2), 
33             Scalar(rng.uniform(0, 256), rng.uniform(0, 256), rng.uniform(0, 256)), 1, LINE_8);
34         imshow("image", image);
35         char c = waitKey(20);
36         if (c == 27) {
37             break;
38         }
39     }
40     waitKey(0);
41     return 0;
42 }

其中putText用于打印文字

显示的结果为:

 

 

 

原文地址:https://www.cnblogs.com/djcsch2001/p/15423837.html