绘制形状和文字

  • 线(线)
  • 矩形(长方形)
  • 圆(圆)
  • 椭圆(椭圆)
  • 填充(fillPoly)

API展示

#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
void mylines();
void myrectangle();
void myellipse();
void mypolygon();//画多边形;
void randomline();
Mat src1, src2;
int main()
{
	src1 = imread("C:\\Users\\马迎伟\\Desktop\\heibao.jpg");
	//src2 = imread("C:\\Users\\马迎伟\\Desktop\\a1a.jpg");
	if (src1.empty())
	{
		cout << "could not find src1" << endl;
		return -1;
	}
	putText(src1, "hello opencv3.4.1", Point(300, 300), CV_FONT_HERSHEY_COMPLEX, 1.0, Scalar(2, 2, 2), 1, 8);
	mylines();
	myrectangle();
	myellipse();
	namedWindow("output", CV_WINDOW_AUTOSIZE);
	imshow("output", src1);
	randomline();
	waitKey(0);
	return 0;
}
void mylines()
{
	Point p1 = Point(20, 30);
	Point p2;
	p2.x = 300;
	p2.y = 300;
	Scalar color = Scalar(0, 0, 255);
	// outputdst , point1,point2, color , 粗细 , 线的形状
	line(src1,p1,p2,color,1,LINE_8);
}
void myrectangle()
{
	Rect rec(235, 35, 300, 300);
	Scalar color = Scalar(0, 12, 12);
	rectangle(src1, rec, color,1,LINE_8);
}
void myellipse()
{
	Scalar color = Scalar(255, 12, 12);
	ellipse(src1, Point(src1.rows / 2, src1.cols / 2), Size(src1.rows / 4, src1.cols / 8), 90, 0, 360, color, 1, LINE_8, 0);
}
void randomline()
{
	Mat bg = Mat::zeros(src1.size(), src1.type());
	RNG rng;
	Point x1;
	Point x2;
	namedWindow("out", CV_WINDOW_AUTOSIZE);
	for (int i = 0; i < 500; i++)
	{
		x1.x = rng.uniform(0, src1.rows);
		x2.x = rng.uniform(0, src1.rows);
		x1.y = rng.uniform(0, src1.cols);
		x2.y = rng.uniform(0, src1.cols);
		Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
		line(bg, x1, x2, color, 1, 8);
		imshow("out",bg);
	}
}