opencv 绘制图形和线

cv::Point
point表示一个点。
cv::Scalar

绘制线、矩形等
划线cv::line(line_4/line_8/line_AA)
line_4
line_8
line_AA 反锯齿
画矩形Rect(x,y,width,height)
画椭圆
cv::ellipse(Mat img,Point(x,y),Size(a,b),angle,0,360,Scalar(,,),thickness,lineType);

ellipse(bgimg, Point(bgimg.cols / 2, bgimg.rows / 2), Point(bgimg.cols / 4, bgimg.rows / 8), 90, 0, 360, color, 2, LINE_8);
bgimg背景图
Point(bgimg.cols / 2, bgimg.rows / 2),椭圆中心点
Point(bgimg.cols / 4, bgimg.rows / 8)长轴和短轴
90是角度
0, 360这个是0-360度,代表画整个椭圆。0-270代表画一段弧
color 是线的颜色
2是线的宽度
LINE_8是线的类型

#include
#include

using namespace cv;
using namespace std;

void MyLine();  //声明直线函数
void MyRectangle();//声明矩形函数
void MyOval();//声明椭圆函数
void MyCircle();
void MyPolygon();

Mat bgimg;

int main(int argc, char** argv) {

	bgimg = imread("d:/gg.jpg");
	if (!bgimg.data) {
		cout << "not load..." << endl;
	}
	MyLine();    //函数调用
	MyRectangle();
	MyOval();
	MyCircle();
	MyPolygon();
	putText(bgimg, "hello world", Point(200, 100),CV_FONT_HERSHEY_COMPLEX, 1.0, Scalar(128, 120, 50), 3, 8);

	namedWindow("draw", WINDOW_AUTOSIZE);
	imshow("draw", bgimg);

	waitKey(0);
	return 0;
}

void MyLine() {
	Point p1 = Point(20, 30);
	Point p2;
	p2.x = 300;
	p2.y = 300;
	Scalar color = Scalar(0, 0, 255);
	line(bgimg, p1, p2, color, 1, LINE_AA);
}

void MyRectangle() {
	Rect rect = Rect(200, 50, 200, 200);
	Scalar color = Scalar(0, 0, 225);
	rectangle(bgimg, rect, color, 2, LINE_8);
}

void MyOval() {
	Scalar color = Scalar(0, 250, 0);
	ellipse(bgimg, Point(bgimg.cols / 2, bgimg.rows / 2), Point(bgimg.cols / 4, bgimg.rows / 8), 180, 0, 270, color, 2, LINE_8);
}

void MyCircle() {
	Scalar color = Scalar(130, 0, 0);
	circle(bgimg, Point(bgimg.cols / 2, bgimg.rows / 2), 100, color, 2, LINE_8);
}

void MyPolygon() {
	Scalar color = Scalar(250, 0, 0);
	Point pts[1][5];
	pts[0][0] = Point(100, 100);
	pts[0][1] = Point(100, 200);
	pts[0][2] = Point(200, 200);
	pts[0][3] = Point(200, 100);
	pts[0][4] = Point(100, 100);

	const Point* ppts[] = { pts[0] };
	int npt[] = { 5 };
	fillPoly(bgimg, ppts, npt, 1, color, 8);

} 

你可能感兴趣的:(图像处理入门)