opencv绘画图形

  • 直线的画法
  • 椭圆的画法
  • 正方形的画法
  • 多边形的画法
  • 等等

api:

  • 直线:line(srcImage, p1, p2, color, 1, LINE_8);
  • 椭圆:ellipse(srcImage,Point(srcImage.cols / 2, srcImage.rows / 3), Size(srcImage.cols / 4, srcImage.rows / 8), 90, 0, 360, color, 2, LINE_8);
  • 正方形:rectangle(srcImage, rect, color, 2, LINE_4);
  • 多边形:fillPoly(srcImage, ppts, npt, 1, color, 8);

例子:

#include<opencv2/opencv.hpp>
#include<iostream>

using namespace cv;
void Mylines();
void MyRect();
void MyEllipse();
void MyPloy();
Mat srcImage;

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

	

	srcImage = imread("E:/image/6.jpg");

	if (srcImage.empty()) {
		printf("没有找到图片");
		return -1;
	}
	Mylines();//直线
	MyRect();//正方形
	MyEllipse();//椭圆
	MyPloy();//多边形
	namedWindow("draw_image", WINDOW_AUTOSIZE);
	imshow("draw_image", srcImage);
	

	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);
	line(srcImage, p1, p2, color, 1, LINE_8);
}

void MyRect() {
	Rect rect = Rect(170, 150, 370, 370);
	Scalar color = Scalar(255, 0, 0);
	rectangle(srcImage, rect, color, 2, LINE_4);
}

void MyEllipse() {
	Scalar color = Scalar(0, 255, 0);
	ellipse(srcImage,Point(srcImage.cols / 2, srcImage.rows / 3), Size(srcImage.cols / 4, srcImage.rows / 8), 90, 0, 360, color, 2, LINE_8);
}

void MyPloy() {
	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 };
	Scalar color = Scalar(255, 100, 255);

	fillPoly(srcImage, ppts, npt, 1, color, 8);


}

效果图:画图操作