opencv 2.x学习笔记(九)简单绘图
2014-07-12 22:54
393 查看
在这一篇中,我们简单的介绍一下,如何使用opencv提供的一些绘图函数,来简单的绘制一个简易的图形。
首先,我们需要了解几个基本的opencv绘图函数。
img:
要绘制线段的图像。
pt1: 线段的起点。
pt2:
线段的终点。
color: 线段的颜色,通过一个Scalar对象定义。
thickness: 线条的宽度。
lineType: 线段的类型。可以取值8, 4, 和CV_AA 分别代表8邻接连接线,4邻接连接线和反锯齿连接线。默认值为8邻接。为了获得更好地效果可以选用CV_AA(采用了高斯滤波)。
shift:
坐标点小数点位数。
img: 要绘制椭圆的图像。
center:
椭圆中心点坐标。
axes: 椭圆位于该Size决定的矩形内。(即定义长轴和短轴)。
angle: 椭圆旋转角度。
startAngle: 椭圆开始绘制时角度。
endAngle: 椭圆绘制结束时角度。(若绘制一个完整的椭圆,则startAngle=0, endAngle = 360)。
color:
椭圆的颜色。
thickness: 绘制椭圆线粗。负数表示全部填充。
lineType,shift:同上。
pt1: 矩形的左上角坐标。
pt2:
矩阵的右下角坐标。
其余同上。
center: 圆心坐标。
radius: 半径。
其余同上。
pts: 多边形定点集。
npts: 多边形的顶点数目。
ncontours:
要绘制多边形的数量。
offset:
所有点轮廓的可选偏移。
其余同上。
大概了解这些函数后,我们就可以利用这些函数来进行简单的绘制图像了,下面是一个简单的猫的绘制,本人画工很烂,敬请谅解。呵呵,不多少,上代码:
首先,我们需要了解几个基本的opencv绘图函数。
线段
opencv为我们提供了line()函数来对直线的绘制。其原型如下://! draws the line segment (pt1, pt2) in the image CV_EXPORTS_W void line(CV_IN_OUT Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0);
img:
要绘制线段的图像。
pt1: 线段的起点。
pt2:
线段的终点。
color: 线段的颜色,通过一个Scalar对象定义。
thickness: 线条的宽度。
lineType: 线段的类型。可以取值8, 4, 和CV_AA 分别代表8邻接连接线,4邻接连接线和反锯齿连接线。默认值为8邻接。为了获得更好地效果可以选用CV_AA(采用了高斯滤波)。
shift:
坐标点小数点位数。
椭圆
opencv为我们提供了ellipse()函数来绘制椭圆。其原型如下://! draws an elliptic arc, ellipse sector or a rotated ellipse in the image CV_EXPORTS_W void ellipse(CV_IN_OUT Mat& img, Point center, Size axes, double angle, double startAngle, double endAngle, const Scalar& color, int thickness=1, int lineType=8, int shift=0);
img: 要绘制椭圆的图像。
center:
椭圆中心点坐标。
axes: 椭圆位于该Size决定的矩形内。(即定义长轴和短轴)。
angle: 椭圆旋转角度。
startAngle: 椭圆开始绘制时角度。
endAngle: 椭圆绘制结束时角度。(若绘制一个完整的椭圆,则startAngle=0, endAngle = 360)。
color:
椭圆的颜色。
thickness: 绘制椭圆线粗。负数表示全部填充。
lineType,shift:同上。
矩形
opencv为我们提供了rectangle()函数来绘制矩形。其原型如下://! draws the rectangle outline or a solid rectangle with the opposite corners pt1 and pt2 in the image CV_EXPORTS_W void rectangle(CV_IN_OUT Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0);
pt1: 矩形的左上角坐标。
pt2:
矩阵的右下角坐标。
其余同上。
圆
opencv为我们提供了circle()函数来绘制圆。其原型如下://! draws the circle outline or a solid circle in the image CV_EXPORTS_W void circle(CV_IN_OUT Mat& img, Point center, int radius, const Scalar& color, int thickness=1, int lineType=8, int shift=0);
center: 圆心坐标。
radius: 半径。
其余同上。
填充多边形
opencv为我们提供了fillPoly()函数来绘制填充多边形。其原型如下://! fills an area bounded by one or more polygons CV_EXPORTS void fillPoly(Mat& img, const Point** pts, const int* npts, int ncontours, const Scalar& color, int lineType=8, int shift=0, Point offset=Point() );
pts: 多边形定点集。
npts: 多边形的顶点数目。
ncontours:
要绘制多边形的数量。
offset:
所有点轮廓的可选偏移。
其余同上。
大概了解这些函数后,我们就可以利用这些函数来进行简单的绘制图像了,下面是一个简单的猫的绘制,本人画工很烂,敬请谅解。呵呵,不多少,上代码:
实例:
#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #define w 400 using namespace cv; void MyCircle( Mat img, Point center ); void DrawHead( Mat img ); void DrawBody( Mat img ); void MyLine( Mat img, Point start, Point end ); void MyRect( Mat rect, Point left_top, Point right_bottom ); int main() { /* 初始化一个400*400的全黑图像 */ Mat cat = Mat::zeros( w, w, CV_8UC3 ); DrawHead( cat ); DrawBody( cat ); MyLine( cat, Point( 95, 355 ), Point( 100, 330 ) ); MyLine( cat, Point( 100, 330 ), Point( 130, 330 ) ); MyLine( cat, Point( 130, 330 ), Point( 180, 285 ) ); MyLine( cat, Point( 65, 185 ), Point( 95, 175 ) ); MyLine( cat, Point( 55, 200 ), Point( 105, 185 ) ); MyLine( cat, Point( 75, 210 ), Point( 107, 200 ) ); MyLine( cat, Point( 200, 150 ), Point( 230, 160 ) ); MyLine( cat, Point( 190, 160 ), Point( 235, 175 ) ); MyLine( cat, Point( 185, 175 ), Point( 220, 185 ) ); MyLine( cat, Point( 145, 150 ), Point( 155, 180 ) ); MyLine( cat, Point( 140, 185 ), Point( 170, 175 ) ); MyCircle( cat, Point( 95, 135 ) ); MyCircle( cat, Point( 175, 115 ) ); MyRect( cat, Point(0, 355), Point( w, w) ); namedWindow( "ownImg", CV_WINDOW_AUTOSIZE ); imshow( "ownImg", cat ); waitKey( 0 ); return 0; } void MyCircle( Mat img, Point center ) { circle( img, center, 15, Scalar( 0, 0, 0 ) ); } void DrawHead( Mat img ) { Point cat_head_points[1][8]; cat_head_points[0][0] = Point( 20, 105 ); cat_head_points[0][1] = Point( 60, 50 ); cat_head_points[0][2] = Point( 90, 80 ); cat_head_points[0][3] = Point( 170, 65 ); cat_head_points[0][4] = Point( 200, 30 ); cat_head_points[0][5] = Point( 240, 60 ); cat_head_points[0][6] = Point( 245, 205 ); cat_head_points[0][7] = Point( 60, 235 ); const Point * ppt[1] = { cat_head_points[0] }; int npt[] = { 8 }; fillPoly( img, ppt, npt, 1, Scalar( 0, 255, 255 ), CV_AA ); } void DrawBody( Mat img ) { Point cat_body_points[1][12]; cat_body_points[0][0] = Point( 95, 230 ); cat_body_points[0][1] = Point( 70, 330 ); cat_body_points[0][2] = Point( 50, 335 ); cat_body_points[0][3] = Point( 40, 355 ); cat_body_points[0][4] = Point( 300, 355 ); cat_body_points[0][5] = Point( 320, 210 ); cat_body_points[0][6] = Point( 345, 200 ); cat_body_points[0][7] = Point( 345, 180 ); cat_body_points[0][8] = Point( 300, 200 ); cat_body_points[0][9] = Point( 280, 305 ); cat_body_points[0][10] = Point( 275, 310 ); cat_body_points[0][11] = Point( 210, 210 ); const Point * ppt[1] = { cat_body_points[0] }; int npt[] = { 12 }; fillPoly( img, ppt, npt, 1, Scalar( 0, 255, 255 ), CV_AA ); } void MyLine( Mat img, Point start, Point end ) { line( img, start, end, Scalar( 0, 0, 0 ), 2, CV_AA ); } void MyRect( Mat img, Point left_top, Point right_bottom ) { rectangle( img, left_top, right_bottom, Scalar( 0, 255, 0 ), -1 ); }
运行结果
相关文章推荐
- python学习笔记2--用turtle简单绘图
- OpenCV 2 学习笔记(8): 利用邻域处理图像与简单的算术图像处理算法:图像滤波与加权和
- opencv-python 学习笔记1:简单的图片处理
- Python OpenCV学习笔记之:图像直方图反向投影(backprojection)原理简单实现
- OpenCV3学习笔记-数据结构和基本绘图
- 我的OpenCV学习笔记(七):绘图操作
- VS 学习笔记 简单绘图
- OpenCV_Python学习笔记--OpenCV中的绘图函数
- opencv-python 学习笔记1:简单的图片处理
- Python OpenCV学习笔记之:基本绘图
- 基础学习笔记之opencv(21):一个简单有趣的皮肤检测代码
- 《VC++深入详解》学习笔记[3]——第4章 简单绘图
- OpenCV学习笔记【4】:一个简单的变换
- OpenCV学习笔记(一):绘图操作
- 计算机视觉与图像处理学习笔记(三)opencv的基本数据类型与简单图像处理函数
- OpenCV 2 学习笔记(3): 一个简单的opencv程序:在Console上显示图像
- OpenCV学习笔记(8)VS2008 MFC下使用OpenCV2.0进行简单图像处理
- python学习笔记9--用turtle简单绘图2
- OpenCv学习笔记(七)---OpenCv中的基本绘图函数,圆,椭圆,线段,矩形,多边形的绘制(1)
- opencv笔记 2.一些简单的绘图函数使用