OpenCV画椭圆、实心圆、凹多边形、线段、矩形


author@jason_ql
http://blog.csdn.net/lql0716


1、 实例代码

#include <QCoreApplication>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;#define WINDOW_WIDTH 600 //定义窗口大小的宏
#define WINDOW_NAME1 "【绘制图1】" //为窗口标题定义的宏
#define WINDOW_NAME2 "【绘制图2】" //为窗口标题定义的宏//-------------------------------------------------------------
//         OpenCV画椭圆、实心圆、凹多边形、线段、矩形
//-------------------------------------------------------------//画椭圆
void drawEllipse(cv::Mat img, double angle){int thickness = 2;int lineType = 8;cv::ellipse(img,cv::Point(WINDOW_WIDTH/2, WINDOW_WIDTH/2),cv::Size(WINDOW_WIDTH/4, WINDOW_WIDTH/16),angle,0,360,cv::Scalar(255,129,0),thickness,lineType);
}//画实心圆
void drawFilledCircle(cv::Mat img, cv::Point center){int thickness = -1;int lineType = 8;cv::circle(img,center,WINDOW_WIDTH/32,cv::Scalar(0,0,255),thickness,lineType);
}//画凹多边形
void drawPolygon(cv::Mat img){int lineType = 8;//创建一些点cv::Point rookPoints[1][20];rookPoints[0][0] = cv::Point(WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8);rookPoints[0][1] = cv::Point(3 * WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8);rookPoints[0][2] = cv::Point(3 * WINDOW_WIDTH / 4, 13 * WINDOW_WIDTH / 16);rookPoints[0][3] = cv::Point(11 * WINDOW_WIDTH / 16, 13 * WINDOW_WIDTH / 16);rookPoints[0][4] = cv::Point(19 * WINDOW_WIDTH / 32, 3 * WINDOW_WIDTH / 8);rookPoints[0][5] = cv::Point(3 * WINDOW_WIDTH / 4, 3 * WINDOW_WIDTH / 8);rookPoints[0][6] = cv::Point(3 * WINDOW_WIDTH / 4, WINDOW_WIDTH / 8);rookPoints[0][7] = cv::Point(26 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);rookPoints[0][8] = cv::Point(26 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);rookPoints[0][9] = cv::Point(22 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);rookPoints[0][10] = cv::Point(22 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);rookPoints[0][11] = cv::Point(18 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);rookPoints[0][12] = cv::Point(18 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);rookPoints[0][13] = cv::Point(14 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);rookPoints[0][14] = cv::Point(14 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);rookPoints[0][15] = cv::Point(WINDOW_WIDTH / 4, WINDOW_WIDTH / 8);rookPoints[0][16] = cv::Point(WINDOW_WIDTH / 4, 3 * WINDOW_WIDTH / 8);rookPoints[0][17] = cv::Point(13 * WINDOW_WIDTH / 32, 3 * WINDOW_WIDTH / 8);rookPoints[0][18] = cv::Point(5 * WINDOW_WIDTH / 16, 13 * WINDOW_WIDTH / 16);rookPoints[0][19] = cv::Point(WINDOW_WIDTH / 4, 13 * WINDOW_WIDTH / 16);const cv::Point* ppt[1] = {rookPoints[0]};int npt[] = {20};cv::fillPoly(img,ppt,npt,1,cv::Scalar(255, 255, 255),lineType);}//画线
void drawLine(cv::Mat img, cv::Point start, cv::Point end){int thickness = 2;int lineType = 8;cv::line(img,start,end,cv::Scalar(0,0,0),thickness,lineType);
}int main(void){//创建空白的Mat图像cv::Mat atomImage = cv::Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);cv::Mat rookImage = cv::Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);//绘制椭圆drawEllipse(atomImage, 90);drawEllipse(atomImage, 0);drawEllipse(atomImage, 45);drawEllipse(atomImage, -45);//绘制圆心drawFilledCircle(atomImage, cv::Point(WINDOW_WIDTH / 2, WINDOW_WIDTH / 2));//绘制椭圆drawPolygon(rookImage);//绘制矩形cv::rectangle(rookImage,cv::Point(0, 7 * WINDOW_WIDTH / 8),cv::Point(WINDOW_WIDTH, WINDOW_WIDTH),cv::Scalar(0, 255, 255),-1,8);//绘制一些线段drawLine(rookImage, cv::Point(0, 15 * WINDOW_WIDTH / 16), cv::Point(WINDOW_WIDTH, 15 * WINDOW_WIDTH / 16));drawLine(rookImage, cv::Point(WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8), cv::Point(WINDOW_WIDTH / 4, WINDOW_WIDTH));drawLine(rookImage, cv::Point(WINDOW_WIDTH / 2, 7 * WINDOW_WIDTH / 8), cv::Point(WINDOW_WIDTH / 2, WINDOW_WIDTH));drawLine(rookImage, cv::Point(3 * WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8), cv::Point(3 * WINDOW_WIDTH / 4, WINDOW_WIDTH));//显示绘制出的图像cv::imshow(WINDOW_NAME1, atomImage);cv::moveWindow(WINDOW_NAME1, 0, 200);cv::imshow(WINDOW_NAME2, rookImage);cv::moveWindow(WINDOW_NAME2, WINDOW_WIDTH, 200);cv::waitKey(0);return(0);}

2、 ellipse()画椭圆

Draws a simple or thick elliptic arc or fills an ellipse sector.

C++: 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)

C++:void ellipse(Mat& img, const RotatedRect& box, const Scalar& color, int thickness=1, int lineType=8)

Python:cv2.ellipse(img, center, axes, angle, startAngle, endAngle, color[, thickness[, lineType[, shift]]]) → None

Python:cv2.ellipse(img, box, color[, thickness[, lineType]]) → None

C:void cvEllipse(CvArr* img, CvPoint center, CvSize axes, double angle, double start_angle, double end_angle, CvScalar color, int thickness=1, int line_type=8, int shift=0 )

Python: cv.Ellipse(img, center, axes, angle, start_angle, end_angle, color, thickness=1, lineType=8, shift=0) → None

C:void cvEllipseBox(CvArr* img, CvBox2D box, CvScalar color, int thickness=1, int line_type=8, int shift=0 )

Python:cv.EllipseBox(img, box, color, thickness=1, lineType=8, shift=0) → None
Parameters:

    img – Image.center – Center of the ellipse.axes – Half of the size of the ellipse main 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 . The figure below explains the meaning of the parameters.

3、 circle()画实心圆

Draws a circle.

C++:void circle(Mat& img, Point center, int radius, const Scalar& color, int thickness=1, int lineType=8, int shift=0)

Python: cv2.circle(img, center, radius, color[, thickness[, lineType[, shift]]]) → None

C:void cvCircle(CvArr* img, CvPoint center, int radius, CvScalar color, int thickness=1, int line_type=8, int shift=0 )

Python: cv.Circle(img, center, radius, color, thickness=1, lineType=8, shift=0) → None

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.

4、 fillpoly()画凹多边形

Fills the area bounded by one or more polygons.

C++:void fillPoly(Mat& img, const Point** pts, const int* npts, int ncontours, const Scalar& color, int lineType=8, int shift=0, Point offset=Point() )

Python:cv2.fillPoly(img, pts, color[, lineType[, shift[, offset]]]) → None

C:void cvFillPoly(CvArr* img, CvPoint** pts, const int* npts, int contours, CvScalar color, int line_type=8, int shift=0 )

Python: cv.FillPoly(img, polys, color, lineType=8, shift=0) → None

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.offset – Optional offset of all points of the contours.

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 their parts), and so forth.

5、 line()画线

Draws a line segment connecting two points.

C++:void line(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)

Python:cv2.line(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) → None

C:void cvLine(CvArr* img, CvPoint pt1, CvPoint pt2, CvScalar color, int thickness=1, int line_type=8, int shift=0 )

Python:cv.Line(img, pt1, pt2, color, thickness=1, lineType=8, shift=0) → None

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) .

6、 rectangle()画矩形

Draws a simple, thick, or filled up-right rectangle.

C++:void rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)

C++:void rectangle(Mat& img, Rect rec, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )

Python:cv2.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) → None

C:void cvRectangle(CvArr* img, CvPoint pt1, CvPoint pt2, CvScalar color, int thickness=1, int line_type=8, int shift=0 )

Python:cv.Rectangle(img, pt1, pt2, color, thickness=1, lineType=8, shift=0) → None

Parameters: img – Image.pt1 – Vertex of the rectangle.pt2 – Vertex of the rectangle opposite to pt1 .rec – Alternative specification of the drawn rectangle.color – Rectangle color or brightness (grayscale image).thickness – Thickness of lines that make up the rectangle. Negative values, like CV_FILLED , mean that the function has to draw a filled rectangle.lineType – Type of the line. See the line() description.shift – Number of fractional bits in the point coordinates.

The function rectangle draws a rectangle outline or a filled rectangle whose two opposite corners are pt1 and pt2, or r.tl() and r.br()-Point(1,1).

7、 更多相关函数见官方文档

Drawing Functions

OpenCV画椭圆、实心圆、凹多边形、线段、矩形相关推荐

  1. Python 用 OpenCV 画椭圆 (5)

    利用 OpenCV 自带的 ellipse() 函数画椭圆 并显示,其函数声明如下: cv2.ellipse(img, center, axes, rotateAngle, startAngle, e ...

  2. python怎么在散点图上画圆圈_Python如何在图片上画一个实心圆

    有时候我们需要对图片进行标记操作,即在原图的基础上进行画出一些图案,这里给出画一个实心圆的相关方法,基于Python. 工具/原料 win10系统64位 winPython集成软件 方法/步骤 1 集 ...

  3. Opencv画椭圆及扇形

    描述 使用opencv库,在imshow出来的窗口中,画出椭圆形状及扇形形状 主要函数为:cv::ellipse 参数解释:相比一个个解释,我还是上图来说明.每个参数的效果,通过控制变量的方式来呈现. ...

  4. opencv画白色实心矩形(去除上下黑边)

    import cv2 import os import numpy as npdir_1 = "./raw/" dir_2 = "./result/"# 获得文 ...

  5. css 圆形背景icon_css3画实心圆和圆角的方法

    当你要用到一个圆形icon的时候,你可能想到通过图片软件来制作,事实上也确实如此,在css3之前,大家都是通过图片+css来实现各种各样的样式效果.不过在css3出来之后,这种方法就显得有点落伍了,它 ...

  6. 用php绘制空心圆,html5使用canvas画空心圆与实心圆_html5教程技巧

    这里给大家分享的是一个学习canvas的时候做的画空心圆与实心圆的练习题,非常简单. 代码如下: var canvas=document.getElementById("canvas&quo ...

  7. java实心圆_html5使用canvas画空心圆与实心圆

    摘要:这篇HTML5栏目下的"html5使用canvas画空心圆与实心圆",介绍的技术点是"canvas.Html5.空心.使用.与",希望对大家开发技术学习和 ...

  8. html5中怎么使用canvas画空心圆与实心圆

    这篇文章主要介绍"html5中怎么使用canvas画空心圆与实心圆"的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇"html5中怎么 ...

  9. android利用shap画小圆圈(空心圆、实心圆)

    在做引导页面的时候需要用到小圆圈指示器,这里我们一般就采用shap画出来 实心圆: <?xml version="1.0" encoding="utf-8" ...

  10. html中空心圆圈怎么表示,html5使用canvas画空心圆与实心圆

    这里给大家分享的是一个学习canvas的时候做的画空心圆与实心圆的练习题,非常简单. 复制代码代码如下: var canvas=document.getElementById("canvas ...

最新文章

  1. debain安装lyx2.2.2,以及报错整理:
  2. suricata的简介以及安装过程
  3. python:程序猿的方式在元宵佳节之际,带你设计【东方明珠】动画浪漫烟花秀(python编程利用Tkinter+PIL库)
  4. Java开发环境!java工程师薪资行情
  5. 今天看到一篇小册子上的话
  6. C语言实验报告册-20163a,c语言实验报告册-20163a.doc
  7. c语言创建线程代码,如何用C语言实现多线程
  8. ant design loading 使用
  9. 支持全球科研抗疫,艾柏森成功研发Omicron变异株重组蛋白
  10. 小身材也有强劲性能,树莓派2代B型图形界面搞机
  11. MongoDB——数据类型详解
  12. 去哪儿网支付系统架构演进--转
  13. 【GO | 基于linux和Windows平台的下载安装及配置】
  14. 百度云svip高速下载
  15. 联想计算机网络同传速度很慢,使用网络同传时传输速度特别慢怎么回事?知识点分析及操作步骤...
  16. 浅显易懂的说清楚小游戏与H5游戏的区别
  17. 在少儿编程中使用easygui(5):ynbox/boolbox/ccbox
  18. 如何将Mindjet的宏放到自定义功能区
  19. ABAQUS隧道模拟,新建盾构隧道下穿既有隧道
  20. 用c语言统计注释数量,c语言代码统计器.doc

热门文章

  1. 【寒江雪】Go实现模版方法模式
  2. 小巷开店问题(第三题)
  3. Bandizip下载安装教程
  4. 【摘录】模拟物流快递系统程序设计
  5. HTML在线转换JS
  6. 爱国者p8880e java_口袋里的电脑 爱国者P8880E MID评测
  7. 两种方法在Qt中使用OpenGL库加载stl三维模型
  8. JavaScript核心之Document对象概述(document的属性,方法,事件)
  9. linux 关闭虚拟内存,Linux关于虚拟内存
  10. 清北学堂noip2018集训D2