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

OpenCV 之 基本绘图的更多相关文章

  1. OpenCV中的绘图函数-OpenCV步步精深

    OpenCV 中的绘图函数 画线 首先要为画的线创造出环境,就要生成一个空的黑底图像 img=np.zeros((512,512,3), np.uint8) 这是黑色的底,我们的画布,我把窗口名叫做i ...

  2. python之OpenCv(三)---基本绘图

    opencv 提供了绘制直线.圆形.矩形等基本绘图的功能 1.绘直线 cv2.line(画布,起点坐标,终点坐标,颜色,宽度) 例如: cv2.line(image,(20,60),(300,400) ...

  3. 5、opencv中的绘图函数

    1.目标 a.学习使用 OpenCV 绘制不同几何图形 b. 你将会学习到这些函数: cv2.line(), cv2.circle(), cv2.rectangle(),cv2.ellipse(),c ...

  4. OpenCV中的绘图函数

    OpenCV可以用来绘制不同的集合图形,包括直线,矩形,圆,椭圆,多边形以及在图片上添加文字.用到的绘图函数包括 cv2.line(),cv2.circle(),cv2.rectangle() ,cv ...

  5. opencv学习(三)——绘图功能

    绘图功能 我们将学习以下函数:cv.line(),cv.circle(),cv.rectangle(),cv.ellipse(),cv.putText()等. 在这些功能中,有一些相同的参数: img ...

  6. OpenCV绘图

    OpenCV绘图 rectangle(Mat& img,Point pt1, Point pt2, const Scalar&color, int thickness=1,int li ...

  7. OpenCV绘图函数

    OpenCV几个绘图函数 矩形 rectangle(Mat& img,Point pt1, Point pt2, const Scalar&color, int thickness=1 ...

  8. OpenCV之响应鼠标(四):在图像上绘制出矩形并标出起点的坐标

    涉及到两方面的内容:1. 用鼠标画出矩形.2.在图像上绘制出点的坐标 用鼠标绘制矩形,涉及到鼠标的操作,opencv中有鼠标事件的介绍.需要用到两个函数:回调函数CvMouseCallback和注册回 ...

  9. OpenCV中cv2的用法

    一.读入图像 使用函数cv2.imread(filepath,flags)读入一副图片 filepath:要读入图片的完整路径 flags:读入图片的标志  cv2.IMREAD_COLOR:默认参数 ...

随机推荐

  1. IDENTITY_INSERT 设置为 OFF 时,不能为表中的标识列插入显式值 的解决方法一例

    如题 IDENTITY_INSERT 设置为 OFF 时,不能为表中的标识列插入显式值 很多网上的文章是设置表的 IDENTITY_INSERT 为 ON EF中还要对模型就行设置 [Column(N ...

  2. Java是如何实现自己的SPI机制的? JDK源码(一)

    注:该源码分析对应JDK版本为1.8 1 引言 这是[源码笔记]的JDK源码解读的第一篇文章,本篇我们来探究Java的SPI机制的相关源码. 2 什么是SPI机制 那么,什么是SPI机制呢? SPI是 ...

  3. Java基础语法(8)-数组中的常见排序算法

    title: Java基础语法(8)-数组中的常见排序算法 blog: CSDN data: Java学习路线及视频 1.基本概念 排序: 是计算机程序设计中的一项重要操作,其功能是指一个数据元素集合 ...

  4. UVA - 11426 欧拉函数(欧拉函数表)

    题意: 给一个数 N ,求 N 范围内所有任意两个数的最大公约数的和. 思路: f 数组存的是第 n 项的 1~n-1 与 n 的gcd的和,sum数组存的是 f 数组的前缀和. sum[n]=f[1 ...

  5. SpringCloud-Nacos/OpenFien/Gateway的基本介绍及快速上手

    一.Spring-Cloud-Alibaba-Nacos 注册中心 1.下载.安装 Nacos 下载地址:https://github.com/alibaba/nacos/releases 下载后解压 ...

  6. DNS 域名解析

    DNS域名解析 整个过程大体描述如下,其中前两个步骤是在本机完成的,后8个步骤涉及到真正的域名解析服务器:1.浏览器会检查缓存中有没有这个域名对应的解析过的IP地址,如果缓存中有,这个解析过程就结束. ...

  7. python使用镜像源安装库

    pip install django -i http://pypi.douban.com/simple --trusted-host pypi.douban.com 豆瓣 :http://pypi.d ...

  8. 尴尬,通篇使用 if

    以给博客园头部导航条链接添加图标为例, 接下来看看如何分别使用对象.数组.Map 优化它的. 前置 1.接下来给头部导航条添的图标包含: 博客园首页 新随笔 博客首页 联系 订阅 管理 2.这里封装了 ...

  9. Promise入门详解

    异步调用 异步 JavaScript的执行环境是单线程. 所谓单线程,是指JS引擎中负责解释和执行JavaScript代码的线程只有一个,也就是一次只能完成一项任务,这个任务执行完后才能执行下一个,它 ...

  10. HTML特殊转义字符——特殊符号

    干货,见下图: 后期我会陆续更一些JavaScript的文章,大家可以一起学习交流.