基本的图形绘制

来源:互联网 发布:sougou输入法windows版 编辑:程序博客网 时间:2024/05/01 12:17

目标:

在该教程中你将学会如何去:

         使用Point在图像中定义一个二维点;

         使用Scalar以及为什么它有用;

         使用OpenCV函数line绘制一条直线;

         使用ellipse函数绘制一个椭圆

         使用rectangle函数绘制矩形;

         使用circle函数绘制圆形;

         使用fillPoly绘制一个填充的多边形;

OpenCV理论:

         在这个教程中,我们将着重使用两个结构:PointScalar

Point:

         它表示一个二维点,用图像的xy坐标来表示。定义如下:

         Pointpt;

pt.x = 10;

pt.y = 8;

或者:

Point pt=Point(10,8);

Scalar

         表示一个有4个元素的vector容器。ScalarOpenCV中通常用来传递像素值。

         在这个教程中,我们将把它扩展到去表示BGR颜色值(3个参数),最后一个参数如果没有用它就没必要去定义它。

         让我们来看一个例子,如果我们要求一个颜色参数,那么我们给出下面的代码:

         Scalara,b,c

         我们可以定义一个BGR颜色,例如:Blue=a,Green=b,Red=c

代码:

#include <opencv2/core.hpp>#include <opencv2/imgproc.hpp>#include <opencv2/highgui.hpp>#define w 400using namespace cv;/// Function headersvoid MyEllipse( Mat img, double angle );void MyFilledCircle( Mat img, Point center );void MyPolygon( Mat img );void MyLine( Mat img, Point start, Point end );/** * @function main * @brief Main function */int main( void ){  /// Windows names  char atom_window[] = "Drawing 1: Atom";  char rook_window[] = "Drawing 2: Rook";  /// Create black empty images  Mat atom_image = Mat::zeros( w, w, CV_8UC3 );  Mat rook_image = Mat::zeros( w, w, CV_8UC3 );  /// 1. Draw a simple atom:  /// -----------------------  /// 1.a. Creating ellipses  MyEllipse( atom_image, 90 );  MyEllipse( atom_image, 0 );  MyEllipse( atom_image, 45 );  MyEllipse( atom_image, -45 );  /// 1.b. Creating circles  MyFilledCircle( atom_image, Point( w/2, w/2) );  /// 2. Draw a rook  /// ------------------  /// 2.a. Create a convex polygon  MyPolygon( rook_image );  /// 2.b. Creating rectangles  rectangle( rook_image,         Point( 0, 7*w/8 ),         Point( w, w),         Scalar( 0, 255, 255 ),         -1,         8 );  /// 2.c. Create a few lines  MyLine( rook_image, Point( 0, 15*w/16 ), Point( w, 15*w/16 ) );  MyLine( rook_image, Point( w/4, 7*w/8 ), Point( w/4, w ) );  MyLine( rook_image, Point( w/2, 7*w/8 ), Point( w/2, w ) );  MyLine( rook_image, Point( 3*w/4, 7*w/8 ), Point( 3*w/4, w ) );  /// 3. Display your stuff!  imshow( atom_window, atom_image );  moveWindow( atom_window, 0, 200 );  imshow( rook_window, rook_image );  moveWindow( rook_window, w, 200 );  waitKey( 0 );  return(0);}/// Function Declaration/** * @function MyEllipse * @brief Draw a fixed-size ellipse with different angles */void MyEllipse( Mat img, double angle ){  int thickness = 2;  int lineType = 8;  ellipse( img,       Point( w/2, w/2 ),       Size( w/4, w/16 ),       angle,       0,       360,       Scalar( 255, 0, 0 ),       thickness,       lineType );}/** * @function MyFilledCircle * @brief Draw a fixed-size filled circle */void MyFilledCircle( Mat img, Point center ){  int thickness = -1;  int lineType = 8;  circle( img,      center,      w/32,      Scalar( 0, 0, 255 ),      thickness,      lineType );}/** * @function MyPolygon * @function Draw a simple concave polygon (rook) */void MyPolygon( Mat img ){  int lineType = 8;  /** Create some points */  Point rook_points[1][20];  rook_points[0][0]  = Point(    w/4,   7*w/8 );  rook_points[0][1]  = Point(  3*w/4,   7*w/8 );  rook_points[0][2]  = Point(  3*w/4,  13*w/16 );  rook_points[0][3]  = Point( 11*w/16, 13*w/16 );  rook_points[0][4]  = Point( 19*w/32,  3*w/8 );  rook_points[0][5]  = Point(  3*w/4,   3*w/8 );  rook_points[0][6]  = Point(  3*w/4,     w/8 );  rook_points[0][7]  = Point( 26*w/40,    w/8 );  rook_points[0][8]  = Point( 26*w/40,    w/4 );  rook_points[0][9]  = Point( 22*w/40,    w/4 );  rook_points[0][10] = Point( 22*w/40,    w/8 );  rook_points[0][11] = Point( 18*w/40,    w/8 );  rook_points[0][12] = Point( 18*w/40,    w/4 );  rook_points[0][13] = Point( 14*w/40,    w/4 );  rook_points[0][14] = Point( 14*w/40,    w/8 );  rook_points[0][15] = Point(    w/4,     w/8 );  rook_points[0][16] = Point(    w/4,   3*w/8 );  rook_points[0][17] = Point( 13*w/32,  3*w/8 );  rook_points[0][18] = Point(  5*w/16, 13*w/16 );  rook_points[0][19] = Point(    w/4,  13*w/16 );  const Point* ppt[1] = { rook_points[0] };  int npt[] = { 20 };  fillPoly( img,        ppt,        npt,            1,        Scalar( 255, 255, 255 ),        lineType );}/** * @function MyLine * @brief Draw a simple line */void MyLine( Mat img, Point start, Point end ){  int thickness = 2;  int lineType = 8;  line( img,    start,    end,    Scalar( 0, 0, 0 ),    thickness,    lineType );}

解释:

1、因为我们打算去绘制两幅图像(一个atom和一个rook),我们需要去创建2个图像以及两个窗口去显示他们。

/// Windows nameschar atom_window[] = "Drawing 1: Atom";char rook_window[] = "Drawing 2: Rook";/// Create black empty imagesMat atom_image = Mat::zeros( w, w, CV_8UC3 );Mat rook_image = Mat::zeros( w, w, CV_8UC3 );

2、然后我们创建函数去绘制不同的几何形状;

MyEllipse( atom_image, 90 );MyEllipse( atom_image, 0 );MyEllipse( atom_image, 45 );MyEllipse( atom_image, -45 );MyFilledCircle( atom_image, Point( w/2.0, w/2.0) );

3、绘制rook的时候我们调用MyLine,rectangleMyPolygon

MyPolygon( rook_image );/// 2.b. Creating rectanglesrectangle( rook_image,           Point( 0, 7*w/8.0 ),<pre name="code" class="cpp">void MyLine( Mat img, Point start, Point end ){  int thickness = 2;  int lineType = 8;  line( img,        start,        end,        Scalar( 0, 0, 0 ),        thickness,        lineType );}

Point( w, w), Scalar( 0, 255, 255 ), -1, 8 );/// 2.c. Create a few linesMyLine( rook_image, Point( 0, 15*w/16 ), Point( w, 15*w/16 ) );MyLine( rook_image, Point( w/4, 7*w/8 ), Point( w/4, w ) );MyLine( rook_image, Point( w/2, 7*w/8 ), Point( w/2, w ) );MyLine( rook_image, Point( 3*w/4, 7*w/8 ), Point( 3*w/4, w ) );

4、让我们来看看这些函数里面是什么:

void MyLine( Mat img, Point start, Point end ){  int thickness = 2;  int lineType = 8;  line( img,        start,        end,        Scalar( 0, 0, 0 ),        thickness,        lineType );}

正如我们所看到的,MyLine函数仅仅调用了函数line:

从起点start到终点end之间绘制一条直线;

这条线在图像img中显示;

线的颜色被Scalar定义为黑色;

线宽被设置为2

线的类型属于8连通的。

void MyEllipse( Mat img, double angle ){  int thickness = 2;  int lineType = 8;  ellipse( img,           Point( w/2.0, w/2.0 ),           Size( w/4.0, w/16.0 ),           angle,           0,           360,           Scalar( 255, 0, 0 ),           thickness,           lineType );}

椭圆在img中显示;

椭圆中心在点(w/2,w/2)并且被一个大小为(w/4,w/16)的矩形所包络;

这个椭圆旋转了angle角度;

这个椭圆外延了0-360°之间的弧度;

图像的颜色被定义为Scalar(255,0,0),在BGR中表示Blue蓝色;

这个椭圆的线宽为2

void MyFilledCircle( Mat img, Point center ){ int thickness = -1; int lineType = 8; circle( img,         center,         w/32.0,         Scalar( 0, 0, 255 ),         thickness,         lineType );}

图像在Img中显示;

圆形的圆心在center

圆的半径是w/32

圆的颜色定义为Scalar(00225),BGR中表示红色;

Thickness=-1,表示圆形将被填充。

void MyPolygon( Mat img ){  int lineType = 8;  /** Create some points */  Point rook_points[1][20];  rook_points[0][0] = Point( w/4.0, 7*w/8.0 );  rook_points[0][1] = Point( 3*w/4.0, 7*w/8.0 );  rook_points[0][2] = Point( 3*w/4.0, 13*w/16.0 );  rook_points[0][3] = Point( 11*w/16.0, 13*w/16.0 );  rook_points[0][4] = Point( 19*w/32.0, 3*w/8.0 );  rook_points[0][5] = Point( 3*w/4.0, 3*w/8.0 );  rook_points[0][6] = Point( 3*w/4.0, w/8.0 );  rook_points[0][7] = Point( 26*w/40.0, w/8.0 );  rook_points[0][8] = Point( 26*w/40.0, w/4.0 );  rook_points[0][9] = Point( 22*w/40.0, w/4.0 );  rook_points[0][10] = Point( 22*w/40.0, w/8.0 );  rook_points[0][11] = Point( 18*w/40.0, w/8.0 );  rook_points[0][12] = Point( 18*w/40.0, w/4.0 );  rook_points[0][13] = Point( 14*w/40.0, w/4.0 );  rook_points[0][14] = Point( 14*w/40.0, w/8.0 );  rook_points[0][15] = Point( w/4.0, w/8.0 );  rook_points[0][16] = Point( w/4.0, 3*w/8.0 );  rook_points[0][17] = Point( 13*w/32.0, 3*w/8.0 );  rook_points[0][18] = Point( 5*w/16.0, 13*w/16.0 );  rook_points[0][19] = Point( w/4.0, 13*w/16.0) ;  const Point* ppt[1] = { rook_points[0] };  int npt[] = { 20 };  fillPoly( img,            ppt,            npt,            1,            Scalar( 255, 255, 255 ),            lineType ); }

多边形在img中显示;

多边形的定点被设置为ppt中的点;

顶点的总数在npt中;

多边形的数量是1,即只绘制一个多边形;

颜色被设置为白色。

rectangle( rook_image,           Point( 0, 7*w/8.0 ),           Point( w, w),           Scalar( 0, 255, 255 ),           -1,           8 );

图像在rook_image中显示;

对角线上的顶点坐标被Point(0,7*w/8)Point(w,w)定义;

颜色设置为黄色;

Thickness=-1,表示图像将被填充

 效果:










0 0
原创粉丝点击