一 学习内容
1 绘制直线
2 绘制曲线
3 添加文本
4 绘制N条随机直线
二 代码
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
Mat dst;
void MyLines();
void Rectangle();
void MyEllipse();
void MyCircle();
void MyPolygon();
void MyRandomLines();
int main(int argc, char** argv) {
Mat src2;
src2 = imread("e:/2 opencv_projects_zl/desk4.jpg");
if (!src2.data) {
cout << "Load Image Desk2 Error...\n" << endl;
return -1;
}
char Figure_name[] = "Figure1";
namedWindow(Figure_name, CV_WINDOW_AUTOSIZE);
imshow(Figure_name, src2);
// 学习新内容
//1. 生成一张背景图,并在背景图上绘制线
//dst = Mat::zeros(src2.size(),src2.type());
dst = Mat(src2.size(), src2.type());
dst = Scalar(50, 80, 20);
//绘线
MyLines();
Rectangle();
MyEllipse();
MyCircle();
MyPolygon();
//添加文本
putText(dst,"World!", Point(dst.cols / 2 -50, dst.rows / 2),CV_FONT_HERSHEY_COMPLEX,1.0,Scalar(200,220,60),3,8);
char Figure_name2[] = "Figure2";
namedWindow(Figure_name2, CV_WINDOW_AUTOSIZE);
imshow(Figure_name2, dst);
//绘制随机直线
MyRandomLines();
waitKey(0);
return 0;
}
void MyLines() {//绘制直线
Point p1 = Point(20, 30);
Point p2;
p2.x = 20;
p2.y = 300;
Scalar color = Scalar(0, 0, 255);
//line(dst,p1,p2,color,1,LINE_8);
line(dst, p1, p2, color, 1, LINE_AA);//更改线型
}
void Rectangle() {//绘制矩形
Rect rect = Rect(20,30,50,30);//起始位置为x=20,y=30,宽和高分别为50,30
Scalar color_rect = Scalar(255, 0, 0);
rectangle(dst,rect,color_rect,1,LINE_4);
}
void MyEllipse() {//绘制椭圆
Scalar color_ellipse = Scalar(0, 255, 0);
ellipse(dst,Point(dst.cols/2,dst.rows/2),Size(dst.cols/4,dst.rows/8),0,0,360, color_ellipse,2,LINE_8);// 倾斜度0度,圆弧起始度为0,结束角度为360
ellipse(dst, Point(dst.cols / 2, dst.rows / 2), Size(dst.cols / 4, dst.rows / 8), 45, 0, 360, color_ellipse, 2, LINE_8);// 倾斜度45度
ellipse(dst, Point(dst.cols / 2, dst.rows / 2), Size(dst.cols / 4, dst.rows / 8), 90, 0, 360, color_ellipse, 2, LINE_8);// 倾斜度90度
ellipse(dst, Point(dst.cols / 2, dst.rows / 2), Size(dst.cols / 4, dst.rows / 8), 135, 0, 360, color_ellipse, 2, LINE_8);// 倾斜度135度
}
void MyCircle() {//绘制圆
Scalar color_circle = Scalar(0, 255, 255);
Point Central_Point = Point(dst.cols / 2 , dst.rows / 2 );
int radius = dst.cols / 4;
circle(dst,Central_Point, radius, color_circle,3,LINE_4);
}
void MyPolygon() {//绘制多边形
Point pts[1][5];
pts[0][0] = Point(60,80);
pts[0][1] = Point(60, 100);
pts[0][2] = Point(80, 100);
pts[0][3] = Point(80, 80);
pts[0][4] = Point(60, 80);
const Point* ppts[] = {pts[0]};
int npt[]= {5};
Scalar color_poly = Scalar(255, 255, 0);
fillPoly(dst,ppts,npt,1,color_poly,8);
}
void MyRandomLines() {//绘制随机直线
RNG rng(123456);//随机数总值?
Point pt1;
Point pt2;
Mat dst2 = Mat::zeros(dst.size(),dst.type());
char Figure_name3[] = "Figure3";
namedWindow(Figure_name3, CV_WINDOW_AUTOSIZE);
for (size_t i = 0; i < 100; i++)
{
pt1.x = rng.uniform(0,dst.cols);
pt2.x = rng.uniform(0, dst.cols);
pt1.y = rng.uniform(0, dst.rows);
pt2.y = rng.uniform(0, dst.rows);
Scalar color_random = Scalar(rng.uniform(0,255), rng.uniform(0, 255), rng.uniform(0, 255));
if (waitKey(50)>0) {
break;
}
line(dst2,pt1,pt2,color_random,1,8);
imshow(Figure_name3, dst2);
}
}
三 调试结果