#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
void DrawLine();//线段
void DrawRectangle(int x, int y, int width, int height);//矩形
void DrawEllipse();//椭圆
void DrawCircle();//圆形
void FillPolygon();//填充多边形
Mat img;
int main(int argc, char** argv)
{
img = imread("D:/Learn/OpenCV/Info/PIC/zx.jpg");
DrawLine();
DrawRectangle(250, 130, 100, 100);
DrawRectangle(555, 245, 100, 100);
DrawRectangle(800, 210, 100, 100);
DrawEllipse();
DrawCircle();
FillPolygon();
putText(img, "ZHU XIAN", Point(500, 50), CV_FONT_HERSHEY_COMPLEX, 1.0, Scalar(0, 255, 255), 2, LINE_8);//已测试,本版本此处暂不能绘制中文
imshow("新图像", img);
waitKey(0);
return 0;
}
void DrawLine()
{
Point pStart = Point(500, 70);
Point pEnd;
pEnd.x = 660;
pEnd.y = 70;
Scalar color = Scalar(255, 0, 0);
line(img, pStart, pEnd, color, 1, LINE_8);//斜线会有锯齿,LINE_AA可以反锯齿,但反据此会增大CPU开销,因此必要时再使用
}
void DrawRectangle(int x, int y, int width, int height)
{
Rect rec = Rect(x, y, width, height);
rectangle(img, rec, Scalar(0, 255, 0), 2, LINE_8);
}
void DrawEllipse()
{
Point pCenter = Point(348, 393);//椭圆中心点
Size axes = Size(240, 33);//长轴,短轴
int angle = 0;//倾角
int startAngle = 0;//起始角度
int endAngle = 360;//结束角度
Scalar color = Scalar(255, 0, 0);//颜色向量
ellipse(img, pCenter, axes, angle, startAngle, endAngle, color, 2);
}
void DrawCircle()
{
Point pCenter = Point(330, 353);//圆心坐标
Scalar color = Scalar(255, 255, 0);//颜色向量
int radius = 30;//半径
circle(img, pCenter, radius, color, 2, LINE_8);
}
void FillPolygon()
{
Point ptsArray[2][5];
ptsArray[0][0] = Point(1120, 550);
ptsArray[0][1] = Point(1170, 550);
ptsArray[0][2] = Point(1170, 600);
ptsArray[0][3] = Point(1120, 600);
ptsArray[0][4] = Point(1120, 550);
ptsArray[1][0] = Point(1145, 600);
ptsArray[1][1] = Point(1120, 650);
ptsArray[1][2] = Point(1170, 650);
const Point* pPts[] = { ptsArray[0],ptsArray[1] };
int count[] = { 5,3 };//每个多边形的坐标数量组成的数组
Scalar color = Scalar(255, 0, 255);
fillPoly(img, pPts, count, 2, color, LINE_8);
}
原始图像:
绘制后图像: