本文主要是介绍08-绘制形状、文和 随机线,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
08-绘制形状、文和 随机线:
#include<opencv2/opencv.hpp>;
#include<iostream>;
using namespace std;
using namespace cv;Mat source1, change1; //全局变量
const char* source1Title = "source1title ";
const char* change1Title = "change1title ";
void MyLines(); //函数声明:绘制线段
void MyRectangle();//绘制矩形
void MyEllipse(); //绘制椭圆弧度
void MyCircle(); //绘制圆
void MyfillPoly(); //绘制多边形
void MyPutText(); //文字输出int main(int argc, char* srgv) { //08- 绘制形状与文字source1 = imread("F:\\OpenCV-Test\\TestPicture\\SourcePicture1\\1.PNG");if (source1.empty()) {printf("can not load image ... \n");return -1;}imshow(source1Title, source1);// API 绘制线,椭圆,矩形,圆,填充 ; 文字source1.copyTo(change1);MyLines(); //调用绘制线段函数MyRectangle();//调用绘制矩形函数MyEllipse(); //调用绘制椭圆函数MyCircle(); //调用绘制圆函数MyfillPoly(); //调用绘制多边形函数MyPutText(); //调用文字输出函数imshow(change1Title, change1);waitKey(0);return 0;
}
void MyLines() { //绘制线段函数 Point p1 = Point(10, 20); //线起点Point p2 = Point(50,80); //线终点Scalar MyLinesColor = Scalar(0,0,255); //线红色line(change1,p1,p2,MyLinesColor,3,LINE_AA); // 图像,起点,终点,颜色,线宽,线反锯齿(稍微耗CPU)
}
void MyRectangle() { //绘制矩形函数 Rect rect = Rect(80,30,150,40); //起点坐标, 终点坐标Scalar rectColor = Scalar(255, 0, 0); //线蓝色颜色rectangle(change1,rect,rectColor,2,LINE_8);//图像,方法,颜色,线宽,线锯齿
}
void MyEllipse() { //绘制椭圆函数Scalar ellipseColor = Scalar(0, 255, 0); //线绿色ellipse(change1,Point(100,150),Size(20,40),45,0,360,ellipseColor,2,LINE_8);//图像,椭圆中心点,椭圆大小长短轴 椭圆倾斜度 0-360度(弧度0-360度之间) 椭圆颜色,椭圆线宽,反锯齿//ellipse(change1, Point(change1.rows/2, change1.cols/2), Size(change1.rows/4, change1.cols/8), 90, 0, 360, ellipseColor, LINE_8);
}
void MyCircle() { //调用绘制圆函数Point CircleCenter = Point(50,200);//Point CircleCenter = Point(change1.rows / 2, change1.cols / 2);Scalar CircleColor = Scalar(0, 255, 255); //线颜色指定circle(change1,CircleCenter,40,CircleColor,2,8);// 图像,圆中心点,圆半径, 圆颜色,圆线宽,反锯齿
}
void MyfillPoly() { //绘制多边形函数Point pts[1][5]; //二维数组1*5pts[0][0] = Point(15, 15); //数组点坐标pts[0][1] = Point(45, 150);pts[0][2] = Point(35, 90);pts[0][3] = Point(35, 45);pts[0][4] = Point(15, 15);const Point* ppts[] = { pts[0] }; //int npt[] = { 5 };Scalar FillPolyColor = Scalar(255,50,255);fillPoly(change1,ppts,npt,1, FillPolyColor,8);// 图像,起点坐标,终点坐标, 数组宽度为1(pts[1][5],颜色,反锯齿)
}
void MyPutText() { //调用文字输出函数const char* PutTexts = "Hello OpenCV !";Scalar TextputColor = Scalar(30, 30, 255);putText(change1,PutTexts,Point(50,250),FONT_HERSHEY_SIMPLEX,1,TextputColor,2,8);//图像,文字内容,坐标,字体类型,文字大小,文字颜色,字体宽度,反锯齿
}
演示效果:
绘制随机线代码:
#include<opencv2/opencv.hpp>;
#include<iostream>;
using namespace std;
using namespace cv;Mat source1; //全局变量
const char* source1Title = "source1title ";void RandomLine();//绘制 随机线
int main(int argc, char* srgv) { //08- 绘制形状与文字source1 = imread("F:\\OpenCV-Test\\TestPicture\\SourcePicture1\\1.PNG");if (source1.empty()) {printf("can not load image ... \n");return -1;}imshow(source1Title, source1);RandomLine();//绘制随机线waitKey(0);return 0;
}
void RandomLine() { //绘制随机线RNG rng(12345);Point pt1;Point pt2;Mat bg = Mat::zeros(source1.size(),source1.type());for (int i = 0; i < 1000;i++) {pt1.x = rng.uniform(0, source1.cols); // 随机数函数.uniformpt1.y = rng.uniform(0, source1.rows);pt2.x = rng.uniform(0, source1.cols);pt2.y = rng.uniform(0, source1.rows);Scalar FreeLineColor = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));if (waitKey(50)>0) {break;}line(bg, pt1, pt2, FreeLineColor, 1, 8);imshow("RandomLine", bg);}
}
代码演示效果:
这篇关于08-绘制形状、文和 随机线的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!