- The cv::Point class represents the point x, y on the 2D plane
- Attributes x and y
- 即Point p; p.y=9; p.x=1;
- The cv::Scalar class represents a vector of four elements
- Scalar(b,g,r); represents the value of the three channels of bgr, which can represent color
- Draw line cv::line()
- line() parameters:
- LINE_4
- LINE_8
- LINE_AA zigzag line
- line(src0, p1, p2, color, 1, LINE_8, 0); //Parameters: background image, start point, end point, line color, thickness line thickness, line type LINE_8
- line() parameters:
- Draw an ellipse cv::ellipse()
- ellipse(src0, Point(src0.cols/2, src0.rows/2), Size(src0.cols/4, src0.rows/8),90, 0 ,360, color, LINE_8);
- Parameters: background image, center of ellipse, length and radius of ellipse, direction of ellipse lying, starting angle, color, line type
- Draw rectangle cv::rectangle()
- Create a rectangular object
- Rect rect = Rect(200, 100, 300, 300); //Parameter: left vertex coordinates x, y and width\height
- rectangle(src0, rect, color, 1, LINE_8); //Parameters: background image, rectangle object, color, thickness, line type
- Create a rectangular object
- Draw a circle cv::circle()
- circle(src0, center, radius, color, 1, LINE_8); //
- Parameters: background image, center, radius, color, thickness, line type
- Fill cv::fillPoly()
- Fill text putText()
- putText(src0, "hello", Point(src0.cols / 2, src0.rows / 2), FONT_HERSHEY_COMPLEX, 2, Scalar(122, 222, 31), 1, LINE_8);
- Parameters: background image, text content, cursor starting point, font style, font size, color, thickness, line type
- Draw lines randomly
- RNG type, random number generation
- RNG rng(seed);
- rng.uniform(double a, double b); The uniform function is used to determine the generation limit
#include "pch.h"
#include <iostream>
#include <opencv2/opencv.hpp>
#include <string>
using namespace std;
using namespace cv;
void MyLines();
void MyRectangle();
void Myellipse();
void MyCircle();
void RandomDemo();
Mat src0, src1;
int main(int argc, char ** argv)
{
src0 = imread("C:\\Users\\xujin\\Desktop\\test0.JPG");
if (!src0.data)
{
cout << "no image";
return -1;
}
MyLines();
MyRectangle();
Myellipse();
MyCircle();
putText(src0, "hello", Point(src0.cols / 2, src0.rows / 2), FONT_HERSHEY_COMPLEX, 2, Scalar(122, 222, 31), 1, LINE_8);
//参数:背景图, 文本内容, 光标起点, 字形,字体大小, 颜色, thickness,线型
namedWindow("src0_image", WINDOW_AUTOSIZE);
imshow("src0_image", src0);
RandomDemo();
waitKey(0);
return 0;
}
void MyLines()
{
Point p1 = Point(20, 30);
Point p2;
Scalar color = Scalar(0, 0, 255);
p2.x = 300;
p2.y = 300;
line(src0, p1, p2, color, 1, LINE_8, 0); //参数: 背景图,开始点,结束点, 线条颜色, thickness线条厚度, 线条类型LINE_8
}
void MyRectangle()
{
Rect rect = Rect(200, 100, 300, 300); //参数:左顶点坐标x,y和width\height
Scalar color = Scalar(233, 1, 22);
rectangle(src0, rect, color, 1, LINE_8); //参数:背景图,矩形对象, 颜色, thickness, 线型
}
void Myellipse()
{
Scalar color = Scalar(0, 222, 31);
ellipse(src0, Point(src0.cols/2, src0.rows/2), Size(src0.cols/4, src0.rows/8),90, 0 ,360, color, LINE_8);
//参数: 背景图, 椭圆中心,椭圆的长短半径, 椭圆躺着的方向, 起始角度,颜色,线型
}
void MyCircle()
{
Scalar color = Scalar(0, 222, 31);
Point center = Point(src0.cols / 2, src0.rows / 2);
int radius = 140;
circle(src0, center, radius, color, 1, LINE_8); //参数: 背景图, 圆心, 半径, 颜色, thickness, 线型
}
void RandomDemo()
{
RNG rng(12345);
Point p1;
Point p2;
Mat bg = Mat::zeros(src0.size(), src0.type());
namedWindow("bg_image", WINDOW_AUTOSIZE);
for (int i = 0; i < 100000; i++)
{
p1.x = rng.uniform(0, src0.cols);
p2.x = rng.uniform(0, src0.cols);
p1.y = rng.uniform(0, src0.rows);
p2.y = rng.uniform(0, src0.rows);
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
line(bg, p1, p2, color, 1, LINE_8);
imshow("bg_image", bg);
if (waitKey(50) > 0)
{
break;
}
}
}