1.绘制形状
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
Mat bgimage;
const char* drawdemo_win = "draw";
void MyLines(); //线
void MyRectangle();//矩形
void MyEllipse(); //椭圆
void MyCircle(); //圆
void MyPolygon(); //多边形
int main(int argc, char** argv)
{
bgimage = imread("1.jpg");
if (!bgimage.data)
{
printf({ "无\n" });
return -1;
}
MyLines();
MyRectangle();
MyEllipse();
MyCircle();
MyPolygon();
char name[] = "第一个";
namedWindow(name,WINDOW_FREERATIO);
imshow(name, bgimage);
waitKey(0);
return 0;
}
void MyLines()
{
Point p1 = Point(20, 30);
Point p2;
p2.x = 400;
p2.y = 400;
Scalar color = Scalar(0, 0, 255); //定义线的颜色
line(bgimage, p1, p2, color, 1, LINE_AA); //LINE_8正常显示,LINE_AA反锯齿
}
void MyRectangle()
{
Rect rect = Rect(200, 100, 300, 300);
Scalar color = Scalar(255, 0, 0); //定义矩形框颜色
rectangle(bgimage, rect, color, 2, LINE_8);
// 矩形数据,颜色
}
void MyEllipse()
{
Scalar color = Scalar(0,255, 0);
ellipse(bgimage, Point(bgimage.cols / 2, bgimage.rows / 2), Size(bgimage.cols / 4, bgimage.rows / 8), 90, 0, 180, color, 2,LINE_8);
//椭圆的位置 椭圆的数据 倾斜角度90°,0~360°,颜色
}
void MyCircle()
{
Scalar color = Scalar(0, 255, 255);
Point center = Point(bgimage.cols / 2, bgimage.rows / 2); //圆心位置
circle(bgimage, center, 100, color, 2, 8);
}
void MyPolygon()
{
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(10, 10);
const Point* ppts[] = { pts[0] };
int npt[] = { 5 };
Scalar color = Scalar(255, 12, 255);
fillPoly(bgimage, ppts, npt, 1, color, 8); //填充图
}
效果图
2.添加文字
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
Mat bgimage;
const char* drawdemo_win = "draw";
int main(int argc, char** argv)
{
bgimage = imread("1.jpg");
if (!bgimage.data)
{
printf({ "无\n" });
return -1;
}
char name2[] = "初始图";
namedWindow(name2, WINDOW_FREERATIO);
imshow(name2, bgimage);
Scalar color = Scalar(12, 255, 0);
putText(bgimage, "Hello", Point(300, 300), FONT_HERSHEY_COMPLEX, 1.0, color , 5, 8);
// 字体类型 , 大小 ,颜色, 加粗
char name[] = "增加后";
namedWindow(name,WINDOW_FREERATIO);
imshow(name, bgimage);
waitKey(0);
return 0;
}
效果图