绘制直线
line()
void cv::line(InputArray img,
Point pt1,
Point pt2,
const Scalar & color,
int thickness = 1,
lineType =
int LINE_8,
int shift = 0
)
·pt1:直线起始点在图像中的坐标。
·pt2:直线终点在图像中的坐标。
·color:图形的颜色,用三通道表示。(我们通过更改三个通道的参数,来对这条直线或者线段进行颜色更改)
·thickness:轮廓的宽度。(通过整型来赋予,因为直线不涉及其他的类型,宽度大于1时才有意义)
·lineType:边界的类型,可取值为FILLED,LINE_4,LINE_8和LINE_AA(是直线的线型,填充型四个宽度,四连接,以及八连接的形式,四连接和八连接是在一个3*3的区域内,以中心像素点为标准点,如果它是上下左右进行填充的,那么就是一个四连接,如果是这个像素所有邻近像素点进行连接填充,那么它就称为八连接。)
·shift:中心坐标和半径数值中的小数位数。
绘制圆形
circle()
vodi cv::circle(InputArray img,
Point center,
int radius,
const Scalar & color,
int thickness = 1,
lineType =
int LINE_8,
int shift = 0
)
·img:绘制圆形的图像。
·center:圆形的中心位置。
·radius:圆形的半径长度,单位为像素。
·color:圆形的颜色,用三通道表示。
绘制椭圆
ellipse()
void cv::ellipse(InputArray img,
Point center,
Size axes,
double angle,
double startAngle,
double endAngle,
const Scalar & color,
int thickness = 1,
lineType =
int LINE_8,
int shift = 0
)
·center:椭圆的中心坐标。
·axes:椭圆主轴大小的一半。
·angle:椭圆旋转的角度,单位为度。
·startAngle:椭圆弧起始的角度,单位为度。
·endAngle:椭圆弧终止的角度,单位为度。
绘制矩形
rectangle()
void cv::rectangle(InputOutputArray img,
Point pt1,
Point pt2,
const Scalar & color,
int thickness = 1,
lineType =
int LTNE_8,
int shift = 0
)
·pt1:左上角顶点
·pt2:右下角顶点
这连个顶点的坐标是一个整形的坐标,那么当我们需要绘制一个含有小数的坐标的时候,我们可以通过最后一个参数来实现。
绘制多边形
fillPoly()
void cv::fillPoly(InputOutputAttay img,
const Point** pts,
const int* npts,
int ncontous,
const Scalar & color,
lineType =
int LINE_8,
int shift = 0,
Point offset = Point()
)
·pt1:多边形顶点数组,可以存放多个多边形顶点坐标的数组。
·npts:每个多边形顶点数组中顶点个数。
·ncontours:绘制多边形的个数。
· offset:所有顶点的可选偏移。
绘制文字
putText()
void cv::putText(InputOutputArray img,
const String & text,
Point org,
int fontScare,
Scalar color,
int thickness = 1,
int lineType = LINE_8,
bool bottomLeftOrigin = false
)
·text:输出到图像中的文字,目前OpenCV4只支持英文。
·org:图像中文字字符串的左下角像素坐标。
·fontFace:字体类型的选择标志。
·fontScale:字体的大小。
·bottomLeftOrigin:图像数据原点的位置,默认为左上角,如果参数改为true,则原点为左下角。
示例
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv; //opencv的命名空间
using namespace std;
int main()
{
Mat img = Mat::zeros(Size(512, 512), CV_8UC3); //生成一个黑色图像用于绘制几何图形
//绘制圆形
circle(img, Point(50, 50), 25, Scalar(255, 255, 255), -1); //绘制一个实心圆
circle(img, Point(100, 50), 20, Scalar(255, 255, 255), 4); //绘制一个空心圆
//绘制直线
line(img, Point(100, 100), Point(200, 100), Scalar(255, 255, 255), 2, LINE_4, 0); //绘制一条直线
//绘制椭圆
ellipse(img, Point(300, 255), Size(100, 70), 0, 0, 100, Scalar(255, 255, 255), -1);//绘制实心椭圆的一部分
//绘制矩形
rectangle(img, Point(50, 400), Point(100, 450), Scalar(125, 125, 125), -1);
//绘制多边形
Point pp[2][6];
pp[0][0] = Point(72, 200);
pp[0][1] = Point(142, 204);
pp[0][2] = Point(226,263);
pp[0][3] = Point(172, 310);
pp[0][4] = Point(117, 319);
pp[0][5] = Point(15, 260);
pp[1][0] = Point(359,339);
pp[1][1] = Point(447, 351);
pp[1][2] = Point(504,349);
pp[1][3] = Point(484,433);
pp[1][4] = Point(418,449);
pp[1][5] = Point(354,402);
Point pp2[5];
pp2[0] = Point(350, 83);
pp2[1] = Point(436, 90);
pp2[2] = Point(500, 171);
pp2[3] = Point(421, 194);
pp2[4] = Point(338, 141);
const Point* pts[3] = { pp[0],pp[1],pp2 };//pts变量的生成
int npts[] = { 6,6,5 };//顶点个数数组的生成
fillPoly(img, pts, npts, 3, Scalar(125, 125, 125), 8);//绘制3个多边形
putText(img, "Learn OpenCV 4", Point(100, 400), 2, 1, Scalar(255, 255, 255));
imshow("", img);
waitKey(0);
return 0;
}