1. Point类
Point point;
point.x = 10;
point.y = 8;
Point point = Point(10, 8);
2. 颜色的表示:Scalar类
Scalar()表示具有4个元素的数组,在OpenCV中常用于传递像素值,如RGB。用不到第四个参数,可以不用写出来。Scalar(a, b, c); 定义红色分量为a……
3. 尺寸:size()
size(x, y); x:width y:height
4. 矩形:Rect()
Rect类的成员变量为:x, y, width, height,分别为左上角点的坐标,矩形的宽和高。
常用成员函数
Size():返回值为size
area():返回值为面积
contains(Point):判断点是否在矩形内
inside(Rect):判断矩形是否在该矩形内
tl():返回左上角点坐标
br():返回右下角点坐标
求矩形的交集和并集
Rect rect = rect1 & rect2
Rect rect = rect1 | rect2
矩形的平移和缩放
Rect rectShift = rect + point;
Rect rectScale = rect + size;
Point point = Point (10, 8);
Rect rect = Rect(10, 10, 100, 80);
Rect rectScale = rect + Size(5, 3);
Rect rectShift = rect + point;
cout << rect.size() << "\n" << rectScale.size() << "\n" << rectShift.size();
结果是:
为什么缩放没有显示出来,后面记得解决。
5. 颜色空间转换:cvtColor()函数
可以实现RGB颜色向HSV,HSI等颜色空间的转换,也可以转换为灰度图像
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn = 0)
code:颜色空间转换的标识符,dstCn为目标图像的通道数,若为0(默认),表示目标图像取源图像的通道数。
cvColor(srcImage, dstImage, CV_GRAY2BGR);//转换原始图像为灰度图像。
转换标识符可查表。
注意:OpenCV默认的图片通道存储顺序是BGR,不是RGB。
6. 其他常用知识点
显示文字相关的函数:getTextSize、cv_InitFont、putText
作图相关函数:circle、clipLine、ellipse、ellipse2Poly、line、rectangle、polylines、类LineIterator。
填充相关的函数有fillConvexPoly、fillPoly
OpenCV中RGN()函数的作用为初始化随机数状态生成器
7. 基本图形的绘制
示例代码
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
#define WINDOW_NAME1 "[绘制图1]"
#define WINDOW_NAME2 "[绘制图2]"
#define WINDOW_WIDTH 600
/************************************************************************/
/* DrawEllipse函数
自定义的绘制函数,实现了绘制不同角度、相同尺寸的椭圆*/
/************************************************************************/
void DrawEllipse(Mat img, double angle)
{
int thickness = 2;
int lineType = 8;
ellipse(img,
Point(WINDOW_WIDTH / 2, WINDOW_WIDTH / 2), //椭圆中心
Size(WINDOW_WIDTH / 4, WINDOW_WIDTH / 16), //大小位于矩形内
angle, //旋转角度
0,360, //扩展弧度
Scalar(255, 129, 0), // 图形颜色
thickness, lineType); // 线宽 线形
}
/************************************************************************/
/* DrawFilledCircle函数
自定义的绘制函数,实现了实心圆的绘制*/
/************************************************************************/
void DrawFilledCircle(Mat img, Point center)
{
int thickness = -1;
int lineType = 8;
circle(img,
center,//圆心
WINDOW_WIDTH / 32,//半径
Scalar(0, 0, 255),//颜色
thickness,lineType
);
}
/************************************************************************/
/* DrawPolygon函数
自定义的绘制函数,实现了凹多边形的绘制*/
/************************************************************************/
void DrawPolygon(Mat img)
{
int lineType = 8;
//创建一些点
Point rookPoints[1][20];
rookPoints[0][0] = Point( WINDOW_WIDTH/4, 7*WINDOW_WIDTH/8 );
rookPoints[0][1] = Point( 3*WINDOW_WIDTH/4, 7*WINDOW_WIDTH/8 );
rookPoints[0][2] = Point( 3*WINDOW_WIDTH/4, 13*WINDOW_WIDTH/16 );
rookPoints[0][3] = Point( 11*WINDOW_WIDTH/16, 13*WINDOW_WIDTH/16 );
rookPoints[0][4] = Point( 19*WINDOW_WIDTH/32, 3*WINDOW_WIDTH/8 );
rookPoints[0][5] = Point( 3*WINDOW_WIDTH/4, 3*WINDOW_WIDTH/8 );
rookPoints[0][6] = Point( 3*WINDOW_WIDTH/4, WINDOW_WIDTH/8 );
rookPoints[0][7] = Point( 26*WINDOW_WIDTH/40, WINDOW_WIDTH/8 );
rookPoints[0][8] = Point( 26*WINDOW_WIDTH/40, WINDOW_WIDTH/4 );
rookPoints[0][9] = Point( 22*WINDOW_WIDTH/40, WINDOW_WIDTH/4 );
rookPoints[0][10] = Point( 22*WINDOW_WIDTH/40, WINDOW_WIDTH/8 );
rookPoints[0][11] = Point( 18*WINDOW_WIDTH/40, WINDOW_WIDTH/8 );
rookPoints[0][12] = Point( 18*WINDOW_WIDTH/40, WINDOW_WIDTH/4 );
rookPoints[0][13] = Point( 14*WINDOW_WIDTH/40, WINDOW_WIDTH/4 );
rookPoints[0][14] = Point( 14*WINDOW_WIDTH/40, WINDOW_WIDTH/8 );
rookPoints[0][15] = Point( WINDOW_WIDTH/4, WINDOW_WIDTH/8 );
rookPoints[0][16] = Point( WINDOW_WIDTH/4, 3*WINDOW_WIDTH/8 );
rookPoints[0][17] = Point( 13*WINDOW_WIDTH/32, 3*WINDOW_WIDTH/8 );
rookPoints[0][18] = Point( 5*WINDOW_WIDTH/16, 13*WINDOW_WIDTH/16 );
rookPoints[0][19] = Point( WINDOW_WIDTH/4, 13*WINDOW_WIDTH/16 );
const Point* ppt[1] = { rookPoints[0] };
int npt[] = { 20 };
fillPoly( img,
ppt,//多边形的顶点集
npt,//要绘制的多边形定点数目
1,
Scalar( 255, 255, 255 ),//颜色
lineType );
}
/************************************************************************/
/* DrawLine函数
自定义的绘制函数,实现了线的绘制*/
/************************************************************************/
void DrawLine(Mat img, Point start, Point end)
{
int thickness = 8;
int lineType = 2;
line(img,
start,
end,
Scalar(0, 0, 0),
thickness,
lineType
);
}
int main()
{
//创建空白的mat图像
Mat atomImage = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);
Mat rookImage = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);
//绘制原子示例图
DrawEllipse(atomImage, 90);
DrawEllipse(atomImage, 0);
DrawEllipse(atomImage, 45);
DrawEllipse(atomImage, -45);
DrawFilledCircle(atomImage, Point(WINDOW_WIDTH / 2, WINDOW_WIDTH / 2));
DrawPolygon( rookImage );
rectangle( rookImage,
Point( 0, 7*WINDOW_WIDTH/8 ),
Point( WINDOW_WIDTH, WINDOW_WIDTH),
Scalar( 0, 255, 255 ),
-1,
8 );
DrawLine( rookImage, Point( 0, 15*WINDOW_WIDTH/16 ), Point( WINDOW_WIDTH, 15*WINDOW_WIDTH/16 ) );
DrawLine( rookImage, Point( WINDOW_WIDTH/4, 7*WINDOW_WIDTH/8 ), Point( WINDOW_WIDTH/4, WINDOW_WIDTH ) );
DrawLine( rookImage, Point( WINDOW_WIDTH/2, 7*WINDOW_WIDTH/8 ), Point( WINDOW_WIDTH/2, WINDOW_WIDTH ) );
DrawLine( rookImage, Point( 3*WINDOW_WIDTH/4, 7*WINDOW_WIDTH/8 ), Point( 3*WINDOW_WIDTH/4, WINDOW_WIDTH ) );
imshow( WINDOW_NAME1, atomImage );
moveWindow( WINDOW_NAME1, 0, 200 );
imshow( WINDOW_NAME2, rookImage );
moveWindow( WINDOW_NAME2, WINDOW_WIDTH, 200 );
waitKey( 0 );
return(0);
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。