006-OpenCV4(C++)绘制直线、矩形、圆、椭圆、绘制字体、随机函数RNG绘制随机图形

一、直线绘制

line(绘制在什么上,起点坐标,终点坐标,颜色,线宽,渲染方式)

//直线
	line(canvas, Point(10, 10), Point(400, 400), Scalar(0,0,255),1, LINE_AA);

二、矩形绘制

定义矩形Rect 矩形名(左上角坐标x,左上角坐标y,宽,高)

//绘制矩形
	Rect rect(100, 100, 200, 200);
	rectangle(canvas, rect, Scalar(255, 0, 0), 1, 8);

三、圆绘制

circle(绘制在什么上,圆心,半径,颜色,线宽,渲染方式)

//绘制圆
circle(canvas, Point(256, 256), 100, Scalar(0, 255, 0), 1, 8);

四、椭圆

ellipse(绘制在什么上, 要绘制的椭圆, 颜色,线宽,渲染方式)

RotatedRect rrt;//定义椭圆
rrt.center= Point2f(256, 256);//椭圆中心点
rrt.angle = 90;//椭圆绘制角度(顺时针)
rrt.size = Size(100, 200);//长轴与短轴
ellipse(canvas, rrt, Scalar(0, 255, 255),2, 8);

五、绘制字体

putText(在什么上绘制,要绘制的字,最左边字的左下角坐标,字体类型,字体大小,字体颜色,字体线宽,字体渲染方式)

putText(canvas, "Hello OpenCV", Point(100, 50), FONT_HERSHEY_PLAIN, 1, Scalar(0, 255, 0), 2, 8);

六、随机函数RNG

Mat image = Mat::zeros(Size(512, 512), CV_8UC3);
	int x1 = 0, y1 = 0;
	int x2 = 0, y2 = 0;
	RNG rng(12345);//创建随机数

	while (true) {
		x1 = (int)rng.uniform(0, 512);
		y1 = (int)rng.uniform(0, 512);
		x2 = (int)rng.uniform(0, 512);
		y2 = (int)rng.uniform(0, 512);//定义随机坐标
		int w = abs(x2 - x1);
		int h = abs(y2 - y1);//定义随机线宽
		Rect rect(x1, y1, w, h);
		image = Scalar(0, 0, 0);
		rectangle(image, rect, Scalar(rng.uniform(0, 256), rng.uniform(0, 256), rng.uniform(0, 256)), 1, 8);//随机绘制随机颜色随机大小的矩形
	    line(image, Point(x1, y1), Point(x2, y2), Scalar(rng.uniform(0, 256), rng.uniform(0, 256), rng.uniform(0, 256)), 1, LINE_AA);//随机绘制随机长度随机颜色的直线

     imshow(" result", image);
        char c = waitKey(10);
         if (c == 27) {
	           break;
		}
	}

你可能感兴趣的:(OpenCV初级,opencv,c++)