此程序较简单,来自D:\OpenCv2.3.1\opencv\samples\cpp\tutorial_code\CxCore\Matrix,涉及到的一些画图函数会用就行。
用到的函数:
void line(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
|
Parameters: |
img – Image. pt1 – First point of the line segment. pt2 – Second point of the line segment. color – Line color. thickness – Line thickness. lineType – Type of the line: 8 (or omitted) - 8-connected line. 4 - 4-connected line. CV_AA - antialiased line. shift – Number of fractional bits in the point coordinates. |
The function line draws the line segment between pt1 and pt2 points in the image. The line is clipped by the image boundaries. For non-antialiased lines with integer coordinates, the 8-connected or 4-connected Bresenham algorithm is used. Thick lines are drawn with rounding endings. Antialiased lines are drawn using Gaussian filtering. To specify the line color, you may use the macro CV_RGB(r, g, b) .
void ellipse(Mat& img, Point center, Size axes, double angle, double startAngle, double endAngle, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
|
Parameters: |
img – Image. center – Center of the ellipse. axes – Length of the ellipse axes. angle – Ellipse rotation angle in degrees. startAngle – Starting angle of the elliptic arc in degrees. endAngle – Ending angle of the elliptic arc in degrees. box – Alternative ellipse representation via RotatedRect or CvBox2D. This means that the function draws an ellipse inscribed in the rotated rectangle. color – Ellipse color. thickness – Thickness of the ellipse arc outline, if positive. Otherwise, this indicates that a filled ellipse sector is to be drawn. lineType – Type of the ellipse boundary. See the line() description. shift – Number of fractional bits in the coordinates of the center and values of axes. |
The functions ellipse with less parameters draw an ellipse outline, a filled ellipse, an elliptic arc, or a filled ellipse sector. A piecewise-linear curve is used to approximate the elliptic arc boundary. If you need more control of the ellipse rendering, you can retrieve the curve using ellipse2Poly() and then render it with polylines() or fill it with fillPoly() . If you use the first variant of the function and want to draw the whole ellipse, not an arc, pass startAngle=0 and endAngle=360 .
void circle(Mat& img, Point center, int radius, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
|
Parameters: |
img – Image where the circle is drawn. center – Center of the circle. radius – Radius of the circle. color – Circle color. thickness – Thickness of the circle outline, if positive. Negative thickness means that a filled circle is to be drawn. lineType – Type of the circle boundary. See the line() description. shift – Number of fractional bits in the coordinates of the center and in the radius value. |
The function circle draws a simple or filled circle with a given center and radius.
void fillPoly(Mat& img, const Point** pts, const int* npts, int ncontours, const Scalar& color, int lineType=8, int shift=0, Point offset=Point() )¶
|
Parameters: |
img – Image. pts – Array of polygons where each polygon is represented as an array of points. npts – Array of polygon vertex counters. ncontours – Number of contours that bind the filled region. color – Polygon color. lineType – Type of the polygon boundaries. See the line() description. shift – Number of fractional bits in the vertex coordinates. |
The function fillPoly fills an area bounded by several polygonal contours. The function can fill complex areas, for example, areas with holes, contours with self-intersections (some of thier parts), and so forth.
代码:
// 028 基本绘图.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#define w 400
using namespace cv;
/// Function headers
void MyEllipse( Mat img, double angle );
void MyFilledCircle( Mat img, Point center );
void MyPolygon( Mat img );
void MyLine( Mat img, Point start, Point end );
int main( int argc, char **argv ){
/// 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.a. Creating ellipses
MyEllipse( atom_image, 90 );
MyEllipse( atom_image, 0 );
MyEllipse( atom_image, 45 );
MyEllipse( atom_image, -45 ); //相当于MyEllipse( atom_image, 135 );
/// 1.b. Creating circles
MyFilledCircle( atom_image, Point( w/2.0, w/2.0) );
/// 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.0 ),
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 );
cvMoveWindow( atom_window, 0, 200 );
imshow( rook_window, rook_image );
cvMoveWindow( rook_window, w, 200 );
waitKey( 0 );
return(0);
}
/// Function Declaration
//函数 ellipse 按照以下规则绘制椭圆:
//椭圆将被画到图像 img 上
//椭圆中心为点 (w/2.0, w/2.0) 并且大小位于矩形 (w/4.0, w/16.0) 内
//椭圆旋转角度为 angle
//椭圆扩展的弧度从 0 度到 360 度
//图形颜色为 Scalar( 255, 255, 0) ,既蓝色
//绘椭圆的线粗为 thickness ,此处是2
void MyEllipse( Mat img, double angle )
{
int thickness = 2;
int lineType = 8;
//int lineType = 4;
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 );
}
// circle 函数的参数意义如下:
//圆将被画到图像 ( img )上
//圆心由点 center 定义
//圆的半径为: w/32.0
//圆的颜色为: Scalar(0, 0, 255) ,按BGR的格式为 红色
//线粗定义为 thickness = -1, 因此次圆将被填充
void MyFilledCircle( Mat img, Point center )
{
//int thickness = 1; //空心圆
int thickness = -1;
int lineType = 8;
circle( img,
center,
w/32.0,
Scalar( 0, 0, 255 ),
thickness,
lineType );
}
//用fillPoly绘制多边形
//多边形将被画到图像 img 上
//多边形的顶点集为 ppt
//要绘制的多边形顶点数目为 npt
//要绘制的多边形数量仅为 1
//多边形的颜色定义为 Scalar( 255, 255, 255), 既BGR值为 白色
void MyPolygon( Mat img )
{
int lineType = 8;
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 );
}
// MyLine 调用函数 line 来实现以下操作:
//画一条从点 start 到点 end 的直线段
//此线段将被画到图像 img 上
//线的颜色由 Scalar( 0, 0, 0) 来定义,在此其相应RGB值为 黑色
//线的粗细由 thickness 设定(此处设为 2)
//此线为8联通 (lineType = 8)
void MyLine( Mat img, Point start, Point end )
{
int thickness = 2;
int lineType = 8;
// int lineType = 4;
line( img,
start,
end,
Scalar( 0, 0, 0 ),
thickness,
lineType );
}
运行结果: