要点:
LINE_4/LINE_8都有锯齿,LINE_AA无锯齿;
新建一个空白图:
Mat t1;
t1.create(500, 500, CV_8UC3);
t1 = Scalar(255, 255, 255);
或者Mat t1(500,500,CV_8UC3,Scalar(255,255,255);
新建直线:
line(t1, p1, p2, color, 1, LINE_8);
//背景图 起始点和终点 线宽 线型
新建矩形:
Rect rect = Rect(100, 100, 300, 300);//起点;长宽
Scalar color = Scalar(55, 55, 55);
rectangle(t1, rect, color, 2, LINE_8);
新建圆:
circle(t1, Point(50, 50), 50, Scalar(200, 50, 5), 2, LINE_8);
// 圆心 半径 颜色
新建椭圆:
新建多边形:
生成随机数:
RNG rng(12345);//生成随机数种子
详解:https://blog.csdn.net/electech6/article/details/45769351
新建文字:
详解:https://blog.csdn.net/awkwardgirl/article/details/45032739
#include
#include
using namespace cv;
using namespace std;
RNG rng(123456);
//Mat t1, t2;
int main(int agrc, char** agrv) {
Mat t2 = imread("test.png");
if (!t2.data ) {
cout << "WRONG";
return -1;
}
char *c2 = "new";
Mat t1 = Mat::zeros(500, 500, CV_8UC3);
line(t1, Point(0, 0), Point(500, 500), Scalar(100, 105, 200), 3, 8);
rectangle(t1, Rect(100, 100, 150, 150), Scalar(150, 200, 170), 2, 8);
putText(t1, "WHY", Point(150, 150), 1, 2, Scalar(200, 45, 160), 2, 8, false);
imshow("t1", t1);
while (1) { //*****
if (waitKey(50) > 0) break;//******
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255),
rng.uniform(0, 255));
Point p1(rng.uniform(0, 500), rng.uniform(0, 500)),
p2(rng.uniform(0, 500), rng.uniform(0, 500));
line(t2, p1, p2, color, 1, 8);
namedWindow(c2, 0);//****
imshow(c2, t2);//****
}
waitKey(0);
}
代码如下:
#include
#include
using namespace cv;
using namespace std;
Mat t1;
void Mylines();
void Myrectangle();
void Myellipse();
void Mycircle();
void Mypolygon();
void Randomlinedemo();
int main(int argc, char** argv)
{
t1.create(500, 500, CV_8UC3);
t1 = Scalar(255, 255, 255);
Mylines();
Myrectangle();
Myellipse();
Mycircle();
Mypolygon();
Randomlinedemo();
putText(t1, "HELLO KITTY", Point(150, 250), CV_FONT_HERSHEY_COMPLEX, 1.0, Scalar(80, 200, 200), 2, 8);
// 文字 文字起点 正常大小无衬线字体 缩放因子 粗细
const char *cc = "MY";
imshow(cc, t1);
waitKey(0);
return 0;
}
void Mylines() {
Point p1 = Point(0,0);//起点
Point p2 = Point(500, 250);//终点
Scalar color = Scalar(0, 0, 255);
line(t1, p1, p2, color, 1, LINE_8);
//背景图 起始点和终点 线宽 线型
}
void Myrectangle() {
Rect rect = Rect(100, 100, 300, 300);//起点;长宽
Scalar color = Scalar(55, 55, 55);
rectangle(t1, rect, color, 2, LINE_8);
}
void Myellipse() {
Scalar color = Scalar(180, 90, 200);
ellipse(t1, Point(t1.cols / 2, t1.rows / 2), Size(60, 180),45, 0, 360, color, 1, LINE_8);
// 图像 中心点 短轴,长轴 起始角度及范围 线宽
}
void Mycircle() {
circle(t1, Point(50, 50), 50, Scalar(200, 50, 5), 2, LINE_8);
// 圆心 半径 颜色
}
void Mypolygon() {
Point pts[1][5];
pts[0][0] = Point(100, 100);
pts[0][1] = Point(80, 50);
pts[0][2] = Point(20, 10);
pts[0][3] = Point(20, 25);
pts[0][4] = Point(100, 100);
const Point* pp[] = { pts[0] };
int np[] = { 5 };//点数
Scalar color = Scalar(220, 250, 220);
fillPoly(t1, pp, np, 1, color,8);//1代表画1个轮廓
}
void Randomlinedemo() {
RNG rng(12345);//生成随机数种子
Point p1;
Point p2;
//Mat r(800, 800, CV_8UC3, Scalar(255, 255, 255));
Mat r = imread("wenhao.jpg");
for (int i = 0; i < 500; i++){
p1.x = rng.uniform(0,r.cols);
p2.x = rng.uniform(0,r.cols);
p1.y = rng.uniform(0,r.cols);
p2.y = rng.uniform(0,r.cols);
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
if (waitKey(50) > 0) {
break;
}
line(r, p1, p2, color, 2, 8);
namedWindow("HA", CV_WINDOW_NORMAL);
imshow("HA",r);
}
}