在学习过程中,我们可以在图像中绘制一些几何图形,比如矩形,椭圆,线段,填充多边形等,这些函数都挺容易理解,下面简单看一下。
1.矩形 rectangle()
通过对角线上的两个顶点绘制矩形
void rectangle(inputoutputarray img, rect rec, const scalar& color, int thickness = 1, int linetype = line_8, int shift = 0); img 名称 rec pt1矩形的顶点 pt2与pt1相对的矩形顶点 color 颜色 也可以用像素存放类scalar thickness 宽度 如果是-1,就代表对改矩形进行填充 linetype 类型 shift 移位点坐标中的小数位数。
代码:
int main() { mat img = mat::ones(240, 240, cv_8uc3); rectangle(img, rect(20, 20, 100, 100), scalar(0, 0, 255),7); imshow("www", img); waitkey(0); }
效果图:
2. 圆 circle()
void circle(inputoutputarray img, point center, int radius, const scalar& color, int thickness = 1, int linetype = line_8, int shift = 0); img 名称 center 圆心坐标 radius 圆的半径 color 圆环颜色 thickness 正数,则表示圆轮廓的厚度 负数 对该圆填充颜色 linetype 类型 shift 移位中心坐标和半径值的小数位数。
代码:
int main() { mat img1=mat::zeros(100, 100, cv_8uc3); circle(img1, point(40, 40), 20, scalar(0, 0, 255),-1);//-1 填充 imshow("www", img1); waitkey(0); }
效果图:
3.椭圆 elliple()
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 名称 center 椭圆的中心。 axes 轴 椭圆主轴大小的一半。 angle 椭圆旋转角度。 startangle 椭圆弧的起始角,以度表示。 endangle 椭圆弧的结束角,以度数表示。 color 椭圆颜色。 thickness 正数 椭圆圆弧轮廓的厚度 负数 对椭圆进行填充。 linetype 椭圆边界类型。 shift 中心坐标和坐标轴值的小数位数。
代码:
int main() { mat img1 = mat::zeros(300, 300, cv_8uc3); ellipse(img1, point(100, 100), size(40, 25), 0, 0, 360, scalar(0, 0, 255),5); imshow("111", img1); waitkey(0); }
效果图:
以上就是c++ opencv实现几何图形绘制的详细内容,更多关于c++ opencv绘制几何图形的资料请关注其它相关文章!