CV_EXPORTS_W void line(InputOutputArray img, Point pt1, Point pt2, const Scalar& color,int thickness = 1, int lineType = LINE_8, int shift = 0);
参数如下
参数
含义
img(image)
绘制多边形的画布,数据类型Mat
pt1(point1)
端点1
pt2(point2)
端点2
color
绘制线条的颜色
thickness
绘制线条的粗细
lineType
绘制线条的连通类型
shift
坐标点小数点位数(not important)
1.2 连通类型
enum LineTypes {
FILLED = -1,
LINE_4 = 4, //!< 4-connected line
LINE_8 = 8, //!< 8-connected line
LINE_AA = 16 //!< antialiased line
};
LINE_4与LINE_8差别不大,而LINE_AA的抗锯齿效果显著
2.正矩形
2.1API
CV_EXPORTS_W void rectangle(InputOutputArray img, Point pt1, Point pt2,
const Scalar& color, int thickness = 1,
int lineType = LINE_8, int shift = 0);
参数如下
参数
含义
img(image)
绘制多边形的画布,数据类型Mat
pt1(point1)
左上角端点
pt2(point2)
右下角端点
color
绘制线条的颜色
thickness
绘制线条的粗细。若取负值,则表示进行填充
lineType
绘制线条的连通类型
shift
坐标点小数点位数(not important)
CV_EXPORTS_W void rectangle(InputOutputArray img, Rect rec,
const Scalar& color, int thickness = 1,
int lineType = LINE_8, int shift = 0);
参数如下
参数
含义
img(image)
绘制多边形的画布,数据类型Mat
rec(rect)
一个矩形
color
绘制线条的颜色
thickness
绘制线条的粗细。若取负值,则表示进行填充
lineType
绘制线条的连通类型
shift
坐标点小数点位数(not important)
3.圆形
3.1 API
CV_EXPORTS_W void circle(InputOutputArray img, Point center, int radius,
const Scalar& color, int thickness = 1,
int lineType = LINE_8, int shift = 0);
参数如下
参数
含义
img(image)
绘制多边形的画布,数据类型Mat
center
圆心坐标
radius
半径
color
绘制线条的颜色
thickness
绘制线条的粗细。若取负值,则表示进行填充
lineType
绘制线条的连通类型
shift
坐标点小数点位数(not important)
4.椭圆
4.1 API
CV_EXPORTS_W void ellipse(InputOutputArray img, Point center, Size axes,
double angle, double startAngle, double endAngle,
const Scalar& color, int thickness = 1,
int lineType = LINE_8, int shift = 0);
参数如下
参数
含义
img(image)
绘制多边形的画布,数据类型Mat
center
圆心坐标
axes
(x方向上半轴长,y方向上半轴长)
angle
顺时针偏角
startAngle
以x方向上的正半轴为起点,偏移一定角度后的起点,从此起点开始画椭圆
endAngle
以x方向上的正半轴为起点,偏移一定角度后的终点,到此为止结束画椭圆
color
绘制线条的颜色
thickness
绘制线条的粗细。若取负值,则表示进行填充
lineType
绘制线条的连通类型
shift
坐标点小数点位数(not important)
4.2 效果
Mat canvas(Size(1000,1000),CV_8U,Scalar(255));
ellipse(canvas,Point2f(500,500),Size(50,100),0,0,90,Scalar(0,0,0),5);
imshow("canvas",canvas);
waitKey();
Mat canvas(Size(1000,1000),CV_8U,Scalar(255));
ellipse(canvas,Point2f(500,500),Size(50,100),20,0,360,Scalar(0,0,0),5);
imshow("canvas",canvas);
waitKey();
Mat canvas(Size(1000,1000),CV_8U,Scalar(255));
ellipse(canvas,Point2f(500,500),Size(50,100),20,0,180,Scalar(0,0,0),5);
imshow("canvas",canvas);
waitKey();
5 斜矩形
5.1 API(通过RotatedRect类和line函数实现)
class CV_EXPORTS RotatedRect
{
public:
//! default constructor
RotatedRect();
/*center:质心坐标
size:(x方向上全边长,y方向上全边长)
angle:顺时针偏角
*/
RotatedRect(const Point2f& center, const Size2f& size, float angle);
/**
三点确定一矩形,记得要互相垂直
*/
RotatedRect(const Point2f& point1, const Point2f& point2, const Point2f& point3);
/** 返回四个角点坐标,要用Point2f类型的数组对象作为参数传入,不能是仅仅是Point类型的数组对象*/
void points(Point2f pts[]) const;
//! returns the minimal up-right integer rectangle containing the rotated rectangle
Rect boundingRect() const;
//! returns the minimal (exact) floating point rectangle containing the rotated rectangle, not intended for use with images
Rect_<float> boundingRect2f() const;
//! returns the rectangle mass center
Point2f center;
//! returns width and height of the rectangle
Size2f size;
//! returns the rotation angle. When the angle is 0, 90, 180, 270 etc., the rectangle becomes an up-right rectangle.
float angle;
};