绘制形状和文字

绘制线

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

    img:要绘制直线的图像;

    pt1:直线的起点;

    pt2:直线的终点;

    color:直线的颜色;

    thickness:直线的厚度;

    lineType:直线的形态;

    shift:点坐标内的部分位数。

示例:

void Line(Mat img) {
	Point p1 = Point(100, 100);
	Point p2 = Point(200, 200);
	line(img, p1, p2, Scalar(0, 0, 255), 2, LINE_AA);
}

结果:


绘制椭圆

CV_EXPORTS_W void ellipse(InputOutputArray img, const RotatedRect& box, const Scalar& color,
                        int thickness = 1, int lineType = LINE_8);
CV_EXPORTS_W 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:要绘制椭圆的图像;

    box:椭圆外围矩形;

    center:椭圆圆心;

    axes:椭圆主轴的一半;

    angle:椭圆旋转角度度数;

    startAngle:椭圆弧度起始角度;

    endAngle:椭圆弧度结束角度。

示例:

void Ellipse(Mat img, int angle) {
	//RotatedRect rect = RotatedRect(Point2f(250, 250), Size2f(300, 100), angle);
	//ellipse(img, rect, Scalar(255, 0, 0));
	ellipse(img, Point(250, 250), Size(150, 50), angle, 0, 360, Scalar(12, 232, 121));
}

结果:


绘制矩形

       
CV_EXPORTS void rectangle(CV_IN_OUT Mat& img, Rect rec,
                          const Scalar& color, int thickness = 1,
                          int lineType = LINE_8, int shift = 0);
CV_EXPORTS_W void rectangle(InputOutputArray img, Point pt1, Point pt2,
                          const Scalar& color, int thickness = 1,
                          int lineType = LINE_8, int shift = 0);

示例:

void Rectangle(Mat img) {
	/*Rect rect = Rect(100, 100, 100, 150);
	rectangle(img, rect, Scalar(0, 255, 0), 2);*/
	rectangle(img, Point(100, 100), Point(300, 150), Scalar(23, 2, 213));
}

结果:


绘制圆

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

示例:

void Circle(Mat img) {
	circle(img, Point(350, 250), 40, Scalar(127, 20, 80), 2);
}

结果:


画填充多边形

CV_EXPORTS void fillPoly(Mat& img, const Point** pts,
                         const int* npts, int ncontours,
                         const Scalar& color, int lineType = LINE_8, int shift = 0,
                         Point offset = Point() );

    pts:多边形数组,数组内容是多边形点的数组;

    npts:多边形顶点数数组;

    ncontours:要填充满区域的轮廓数。

示例:

void FillPoly(Mat img) {
	Point pts[1][7];
	pts[0][0] = Point(200, 300);
	pts[0][1] = Point(300, 300);
	pts[0][2] = Point(350, 350);
	pts[0][3] = Point(300, 450);
	pts[0][4] = Point(200, 450);
	pts[0][5] = Point(150, 350);
	pts[0][6] = Point(200, 300);

	const Point* ppts[] = { pts[0] };
	int npt[] = { 7 };
	Scalar color = Scalar(255, 12, 255);
	fillPoly(img, ppts, npt, 1, color, 8);
}

结果:


随机数生成

示例:

void RandomLine(Mat img) {
	RNG rng(123456);
	Point p1, p2;
	Scalar color;
	for (int i = 0; i < 1000000; i++) {
		p1.x = rng.uniform(0, img.cols);
		p2.x = rng.uniform(0, img.cols);
		p1.y = rng.uniform(0, img.rows);
		p2.y = rng.uniform(0, img.rows);
		color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
		line(img, p1, p2, color);
		imshow("RandomLine", img);
		if (waitKey(50) > 0) {
			break;
		}
	}
}

结果:


绘制添加文字

CV_EXPORTS_W void putText( InputOutputArray img, const String& text, Point org,
                         int fontFace, double fontScale, Scalar color,
                         int thickness = 1, int lineType = LINE_8,
                         bool bottomLeftOrigin = false );

    text:要显示的文字;

    org:文字在图中的左下角;

    fontFace:字体;

    fontScale:字体的大小;

    bottomLeftOrigin:若为true,字符串图的起点在左下角,否则在右上角。

示例:

void PutText(Mat img) {
	putText(img, "Hello World", Point(350, 250), CV_FONT_HERSHEY_COMPLEX, 1.0, Scalar(3, 234, 34));
}

结果:


猜你喜欢

转载自blog.csdn.net/qq_33408113/article/details/81016422