Point表示2D平面上的一个点(x,y)
Point P;
p.x=10;
p.y=8;
or
p=point(10,8);
Scalar表示四个元素的向量
Scalar(a,b,c); //a=blue;b=green;c=red表示rgb三个通道。
画线cv://line(LINE_4/LINE_8/LINE_AA) 用LINE_AA画出来的线不会有锯齿
画椭圆cv::ellipse
画矩形cv::rectangle
画圆cv::circle
画填充:cv::fillPoly
随机数生成cv::RNG
(1) 生成高斯随机数gaussian(double sigma)
(2) 生成正态分布随机数uniform(int a,int b)
代码演示:
#include <opencv2/opencv.hpp>#include <iostream>#include <math.h>using namespace std;using namespace cv;Mat src;void Mylines() { Point p1 = Point(20, 30); Point p2; p2.x = 300; p2.y = 300; Scalar color = Scalar(0, 0, 255); line(src,p1,p2,color,1,LINE_8);}void MyRectngle() { Rect rect = Rect(200, 100, 300, 300); //参数说明:1矩形左上角点的横坐标 2矩形左上角点的纵坐标 3矩形的长 4矩形的宽 Scalar color = Scalar(0, 255, 0); rectangle(src, rect, color, 2, LINE_8); //2表示线的宽度}void MyEllipse() { Scalar color = Scalar(255, 0, 0); ellipse(src, Point(src.cols / 2, src.rows / 2), Size(src.cols / 4, src.rows / 8), 90, 0, 360,color,2,LINE_8); //参数说明:0源图像 1椭圆中心 2椭圆的规格(长轴,短轴) 3椭圆的倾斜角度 4起始角度 5终止角度 6颜色 7线条宽度 8线的类型}void MyCircle() { Scalar color = Scalar(0, 255, 255); Point center = Point(src.cols / 2, src.rows / 2); //圆心 circle(src, center, 200, color, 2, LINE_AA);}void RandomLineDemo() { Mat src1 = Mat::zeros(src.size(), src.type()); RNG rng(12345); Point pt1; Point pt2; //namedWindow("random line demo", WINDOW_AUTOSIZE); for (int i = 0; i < 100000; i ) { pt1.x = rng.uniform(0, src.cols); //pt1中x的取值范围 rng.uniform表示取随机数 pt2.x = rng.uniform(0, src.cols); //pt2中x的取值范围 pt1.y = rng.uniform(0, src.rows);//pt1中y的取值范围 pt2.y = rng.uniform(0, src.rows);//pt2中y的取值范围 Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)); if (waitKey(50) > 0) { break; } line(src1, pt1, pt2, color, 1, LINE_AA); imshow("random line demo", src1); } }void MyPolygon() { Point pts[1][6]; pts[0][0] = Point(100, 100); pts[0][1] = Point(100, 200); pts[0][2] = Point(300, 200); pts[0][3] = Point(200, 300); pts[0][4] = Point(300, 100); pts[0][5] = Point(100, 100); const Point* ppts[] = { pts[0] }; int npt[] = { 6 }; Scalar color = Scalar(255, 12, 255); fillPoly(src, ppts, npt, 1,color,LINE_8); //1表示只有一个轮廓}int main(int argc, char** argv) { src = imread("D:/image/img1.jpg"); if (src.empty()) { printf("could not load image ... \n"); return -1; } Mylines(); MyRectngle(); MyEllipse(); MyCircle(); MyPolygon(); putText(src,"hello world",Point(200,200),FONT_HERSHEY_COMPLEX,1.0,Scalar(12,255,200),1,LINE_8); //参数说明:1源图像 2显示内容 3显示起始位置 4字体 5缩放比例 6颜色 7线条宽度 8线条类型 RandomLineDemo(); namedWindow("src", WINDOW_AUTOSIZE); imshow("src", src); waitKey(0); return 0;}
来源:https://www.icode9.com/content-1-461451.html
联系客服