OpenCV 虽是开源的计算机视觉库,但里面也有一些基础的绘图函数,本文将介绍几种常用绘图函数:直线、圆、椭圆、长方形、多边形等。

1  数据结构

1.1  二维向量

cv::Point 代表的是二维点 (int 型),可用来表示图像坐标 (x, y)

// one way
Point pt;
pt.x = ;
pt.y = ; // another way
Point pt = Point(, );

OpenCV 中,二维点类型可分为 Point2i, Point2l, Point2f, Point2d 四种,各自定义如下:

// 4 type of Point
typedef Point_<int> cv::Point2i
typedef Point_<int64> cv::Point2l
typedef Point_<float> cv::Point2f
typedef Point_<double> cv::Point2d // cv::Point
typedef Point2i cv::Point

1.2  四维向量

cv::Scalar 代表的是四维向量,常用来传递像素值,尤其是 BGR 通道的像素值 (最后一个元素不用,则不定义)

$\texttt{Scalar} (blue \_ component, green \_ component, red \_ component)$

2  绘图函数

2.1  line()

OpenCV 中,绘制直线段较简单,就是过两点画一条直线,函数为 line()

// pt1, first point
// pt2, second point
void cv::line ( InputOutputArray img, Point pt1, Point pt2, const Scalar& color, int thickness = , int lineType = LINE_8, int shift = )

2.2 circle() 和 ellipse()

知道圆心和半径,就可以绘制圆了,函数为 circle()

void cv::circle (
InputOutputArray img,
Point center, // center of the circle
int radius, // radius of the circle
const Scalar & color,
int thickness = ,
int lineType = LINE_8,
int shift =
)

椭圆稍微复杂,椭圆中心,长、短轴半径,以及椭圆弧的旋转角度,则可得到一段椭圆弧 ellipse()

void cv::ellipse (
InputOutputArray img,
Point center, // center of the ellipse
Size axes, // half size of the main axes
double angle, // ellipse rotation angle in degrees
double startAngle,
double endAngle,
const Scalar & color,
int thickness = ,
int lineType = LINE_8,
int shift =
)

2.3  rectangle()

长方形的绘制,主要是靠其对角线上的两个点 pt1 和 pt2,函数为 rectangle()

void cv::rectangle (
InputOutputArray img,
Point pt1, // vertex of the rectangle
Point pt2, // vertex of the rectangle opposite to pt1
const Scalar & color,
int thickness = ,
int lineType = LINE_8,
int shift =
)

2.4 fillpoly()

void cv::fillPoly (
InputOutputArray img,
const Point ** pts, //
const int * npts, //
int ncontours,
const Scalar & color,
int lineType = LINE_8,
int shift = ,
Point offset = Point()
)

3 代码示例

3.1  直线和长方形

#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp> using namespace cv; #define w 300 int main()
{
// creat a white background image
Mat img;
img.create(w,w,CV_8UC3);
img.setTo(Scalar(,,)); // draw lines
line(img, Point(w/,w/), Point(*w/, w/), Scalar(, , ));
line(img, Point(w/,w/), Point(*w/, w/), Scalar(, , ));
line(img, Point(w/,*w/), Point(*w/, *w/), Scalar(, , )); // draw rectangle
rectangle(img,Point(w/,w/),Point(*w/,*w/),Scalar(,,)); // show lines in the image
imshow("line and rectangle", img); waitKey();
}

3.2  圆和椭圆

// draw circle and ellipse
circle(img, Point(w/,w/), , Scalar(, , ));
ellipse(img, Point(w/,w/), Size(,), , , , Scalar(, , ));

3.3  多边形

Point rook_points[][];
rook_points[][] = Point( w/, *w/ );
rook_points[][] = Point( *w/, *w/ );
rook_points[][] = Point( *w/, *w/ );
rook_points[][] = Point( *w/, *w/ );
rook_points[][] = Point( *w/, *w/ );
rook_points[][] = Point( *w/, *w/ );
rook_points[][] = Point( *w/, w/ );
rook_points[][] = Point( *w/, w/ );
rook_points[][] = Point( *w/, w/ );
rook_points[][] = Point( *w/, w/ );
rook_points[][] = Point( *w/, w/ );
rook_points[][] = Point( *w/, w/ );
rook_points[][] = Point( *w/, w/ );
rook_points[][] = Point( *w/, w/ );
rook_points[][] = Point( *w/, w/ );
rook_points[][] = Point( w/, w/ );
rook_points[][] = Point( w/, *w/ );
rook_points[][] = Point( *w/, *w/ );
rook_points[][] = Point( *w/, *w/ );
rook_points[][] = Point( w/, *w/ );
const Point* ppt[] = { rook_points[]};
int npt[] = { };
// draw polygon
fillPoly(img, ppt, npt, , Scalar(, , ));

3.4  显示效果

       

参考资料:

OpenCV Tutorials / imgproc module / Basic Drawing

最新文章

  1. 第三次个人作业—“K米”评测
  2. angularjs移除不必要的$watch
  3. java设计模式(三)--抽象工厂模式
  4. 在Excel中使用频率最高的函数的功能和使用方法
  5. java 实现https请求
  6. js 实现继承相关
  7. HBase(二): c#访问HBase之股票行情Demo
  8. 下拉框的选择跳转事件(jqure)
  9. list add元素覆盖之前元素问题思考
  10. vmware完整克隆(linux)
  11. Web基础学习
  12. Oauth2手册 第一章
  13. qq截图存放在电脑的哪个文件夹
  14. c# NPOI 导出EXCEL (在下方显示图片)
  15. 移动端不利用HTML5和echarts开发一样可以实现大数据展示及炫酷统计系统(产品技术综合)
  16. week2--操作系统是如何工作的
  17. delphi services允许跨域访问
  18. centos7默认安装没有连接到网络
  19. 说说HTML5中label标签的可访问性问题——张鑫旭
  20. Linux java Tomcat 项目中 new Date 获取时间 8小时 时差

热门文章

  1. settings源码
  2. PTA 创建计算机类
  3. 加油站问题 Gas Station
  4. windows常用系统命令
  5. 使用datasest属性改变样式
  6. 微信小程序生成带参数的二维码(小程序码)独家asp.net的服务端c#完整代码
  7. Java 运行时数据区
  8. Debug 是门艺术
  9. JMeter中聚合报告的各项参数详解
  10. 泛型代码中的 default 关键字