先写图片显示代码,然后再对每一个对象细分,代码示范:
#include<opencv2\opencv.hpp>
#include
#include<math.h>
using namespace std;
using namespace cv;
Mat bgImage;
const char* drawdemo_win = “draw shape and text demo”;
void Mylines();//绘制直线
void drawRectangle();//绘制矩形
void drawEllipse();//绘制椭圆
void MyCircle();//绘制圆
void drawPolyon();//绘制填充多边形
int main(int argc, char** argv) {
bgImage = imread("E:/python image/15.jpg");//读取原图
if (bgImage.empty()){
cout << "could not find image resource...\n" << std::endl;
return -1;
}
Mylines();
drawRectangle();
drawEllipse();
MyCircle();
drawPolyon();
namedWindow("input_win", CV_WINDOW_AUTOSIZE);//新建窗口
imshow("input_win", bgImage);//显示原图
waitKey(0);
return 0;
}
void Mylines(){//绘制线条
Point p1 = Point(10, 60);//Point表示平面上的点,OpenCV中的这些坐标值都是相对于图像本身像素值的
Point p2;
p2.x = 300;
p2.y = 300;
Scalar color = Scalar(255, 0, 0);
line(bgImage, p1, p2, color, 1, LINE);
}
void drawRectangle()//绘制矩形
{
//OpenCV的坐标系,原点在屏幕左上角,x朝右正,y朝下正
Rect rect = Rect(350, 350, 200, 200);//起始位置x、y,宽,高
Scalar color = Scalar(255, 0, 0);
rectangle(bgImage, rect, color,1,LINE);//绘制矩形到Mat,自带圆角
}
void drawEllipse(){//绘制椭圆
int width = bgImage.cols;
int height = bgImage.rows;
Scalar color = Scalar(0, 255, 0);
//绘制椭圆,参数:中心点,椭圆的长短轴半径,椭圆的朝向,起始角度,终点角度
ellipse(bgImage, Point(width / 1.5, height / 3), Size(width / 8, height / 9), 0, 0, 360, color, 2, __LINE__);
}
void MyCircle(){//绘制圆
Scalar color = Scalar(0, 255, 255);
Point center = Point(bgImage.cols / 1.5, bgImage.rows / 3);
circle(bgImage, center, 150, color, 2, 8);
}
void drawPolyon(){//绘制填充的多边形
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, 255, 0);
//参数:Mat,顶点数据指针,顶点个数,1表示只绘制一个Contour(轮廓)
fillPoly(bgImage, ppts, npt, 1, color, LINE);
}
在这里插入图片描述