opencv 2.x学习笔记(九)简单绘图

来源:互联网 发布:兔狗家装网怎么样知乎 编辑:程序博客网 时间:2024/05/17 19:57

在这一篇中,我们简单的介绍一下,如何使用opencv提供的一些绘图函数,来简单的绘制一个简易的图形。

首先,我们需要了解几个基本的opencv绘图函数。

线段

opencv为我们提供了line()函数来对直线的绘制。其原型如下:

//! draws the line segment (pt1, pt2) in the imageCV_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 imageCV_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 imageCV_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 imageCV_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 polygonsCV_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 400using 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 );}



运行结果



1 0
原创粉丝点击