常用宏定义:
#define CV_RGB( r, g, b ) cvScalar( (b), (g), (r), 0 ) #define CV_FILLED -1 #define CV_AA 16
#define cvDrawRect cvRectangle #define cvDrawLine cvLine #define cvDrawCircle cvCircle #define cvDrawEllipse cvEllipse #define cvDrawPolyLine cvPolyLine
直线 cvLine()
/* Draws 4-connected, 8-connected or antialiased line segment connecting two points */ CVAPI(void) cvLine( CvArr* img, CvPoint pt1, CvPoint pt2, CvScalar color, int thickness CV_DEFAULT(1), int line_type CV_DEFAULT(8), int shift CV_DEFAULT(0) );
pt1、pt2:直线的两点 CvPoint()结构
typedef struct CvPoint { int x; int y; } CvPoint;
typedef struct CvScalar { double val[4]; } CvScalar;这种结构是四个双精度浮点型变量的集合,前三个分别代表红、绿、蓝通道
常用的便捷宏指令CV_RGB(r, g, b)
###############################################################
矩形 cvRectagle()
/* Draws a rectangle given two opposite corners of the rectangle (pt1 & pt2), if thickness<0 (e.g. thickness == CV_FILLED), the filled box is drawn */ CVAPI(void) cvRectangle( CvArr* img, CvPoint pt1, CvPoint pt2, CvScalar color, int thickness CV_DEFAULT(1), int line_type CV_DEFAULT(8), int shift CV_DEFAULT(0));
thickness参数可设置为CV_FILL,其值是-1,表示使用color填充内部 在圆形和矩阵等很多封闭图形函数都可用
################################################################
圆形 cvCircle()
/* Draws a circle with specified center and radius. Thickness works in the same way as with cvRectangle */ CVAPI(void) cvCircle( CvArr* img, CvPoint center, int radius, CvScalar color, int thickness CV_DEFAULT(1), int line_type CV_DEFAULT(8), int shift CV_DEFAULT(0));
radius:圆半径
##############################################################
椭圆 cvEllipse() cvEllipseBox
/* Draws ellipse outline, filled ellipse, elliptic arc or filled elliptic sector, depending on <thickness>, <start_angle> and <end_angle> parameters. The resultant figure is rotated by <angle>. All the angles are in degrees */ CVAPI(void) cvEllipse( CvArr* img, CvPoint center, CvSize axes, double angle, double start_angle, double end_angle, CvScalar color, int thickness CV_DEFAULT(1), int line_type CV_DEFAULT(8), int shift CV_DEFAULT(0));
typedef struct CvSize { int width; int height; } CvSize;
start_angle和end_angle表示弧线可是和结束位置的角度。因此,一个完整的椭圆必须分别将这两个值分别设为0度和360度
CV_INLINE void cvEllipseBox( CvArr* img, CvBox2D box, CvScalar color, int thickness CV_DEFAULT(1), int line_type CV_DEFAULT(8), int shift CV_DEFAULT(0) ) { CvSize axes; axes.width = cvRound(box.size.width*0.5); axes.height = cvRound(box.size.height*0.5); cvEllipse( img, cvPointFrom32f( box.center ), axes, box.angle, 0, 360, color, thickness, line_type, shift ); }
typedef struct CvBox2D { CvPoint2D32f center; /* Center of the box. */ CvSize2D32f size; /* Box width and length. */ float angle; /* Angle between the horizontal axis */ /* and the first side (i.e. length) in degrees */ } CvBox2D;
void paint(void) { IplImage *src=cvLoadImage("lena.jpg"); if (src == NULL) { exit(0); } cvLine(src, cvPoint(3, 3), cvPoint(3, 300), CV_RGB(255, 255, 255), 1, 8); cvRectangle(src, cvPoint(10, 10), cvPoint(100, 100), CV_RGB(255, 255, 255), 1); cvCircle(src, cvPoint(350, 350), 50, CV_RGB(255, 255, 255), 1); cvEllipse(src, cvPoint(200, 200), cvSize(100, 50), 90, 0, 360, CV_RGB(255, 255, 255), 1, 8); CvSize2D32f size=cvSize2D32f(100, 50); CvBox2D box; box.center.x = 100; box.center.y = 300; box.size = size; box.angle = 90; cvEllipseBox(src, box, CV_RGB(255, 255, 255), 1, 8, 0); cvNamedWindow("src"); cvShowImage("src", src); cvWaitKey(0); cvReleaseImage(&src); cvDestroyWindow("src"); }
######################################################################################
/* Fills convex or monotonous polygon. */ CVAPI(void) cvFillConvexPoly( CvArr* img, const CvPoint* pts, int npts, CvScalar color, int line_type CV_DEFAULT(8), int shift CV_DEFAULT(0));
pts:存储点的数组
npts:存储点的数目
void convexPoly(void) { IplImage *Image1; CvPoint PointArray1[6]; int PolyVertexNumber; int Shift; CvSize ImageSize1 = cvSize(1000,700); Image1 = cvCreateImage(ImageSize1, IPL_DEPTH_8U, 3); PointArray1[0]=cvPoint(200,200); PointArray1[1]=cvPoint(400,100); PointArray1[2]=cvPoint(650,230); PointArray1[3]=cvPoint(800,300); PointArray1[4]=cvPoint(900,550); PointArray1[5]=cvPoint(100,400); CvScalar Color=CV_RGB(255,255,255); PolyVertexNumber=6; Shift=0; cvFillConvexPoly(Image1,PointArray1,PolyVertexNumber,Color,CV_AA,Shift); cvNamedWindow("FillConvexPoly",0); cvShowImage("FillConvexPoly",Image1); cvWaitKey(0); cvSaveImage("poly.jpg",Image1); cvReleaseImage(&Image1); }
#######################################################
/* Fills an area bounded by one or more arbitrary polygons */ CVAPI(void) cvFillPoly( CvArr* img, CvPoint** pts, const int* npts, int contours, CvScalar color, int line_type CV_DEFAULT(8), int shift CV_DEFAULT(0) ); /* Draws one or more polygonal curves */ CVAPI(void) cvPolyLine( CvArr* img, CvPoint** pts, const int* npts, int contours, int is_closed, CvScalar color, int thickness CV_DEFAULT(1), int line_type CV_DEFAULT(8), int shift CV_DEFAULT(0) );
npts:由记数点构成的数组,与多边形对应
is_closed为true:则起点和终点连线;否则,不连线
void fillPoly(void) { IplImage *src=cvLoadImage("lena.jpg"); if (src == NULL) exit(0); CvPoint** pt=new CvPoint*[1]; pt[0]=new CvPoint[6]; pt[0][0]=cvPoint(100, 100); pt[0][1]=cvPoint(400, 50); pt[0][2]=cvPoint(500, 400); pt[0][3]=cvPoint(300, 500); pt[0][4]=cvPoint(200, 300); pt[0][5]=cvPoint(50, 400); int *npt=new int[1]; npt[0]=6; int contours=1; cvFillPoly(src, pt, npt, contours, CV_RGB(255, 255, 255), 8); //cvPolyLine(src, pt, npt, contours, 1, CV_RGB(255, 255, 255));//is_closed为true则将起点和终点连线 //cvPolyLine(src, pt, npt, contours, 0, CV_RGB(255, 255, 255));//is_closed为false则不将起点和终点连线 cvNamedWindow("src"); cvShowImage("src", src); cvWaitKey(0); cvReleaseImage(&src); cvDestroyWindow("src"); }