OpenCV 之 基本绘图
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 之 基本绘图的更多相关文章
- OpenCV中的绘图函数-OpenCV步步精深
OpenCV 中的绘图函数 画线 首先要为画的线创造出环境,就要生成一个空的黑底图像 img=np.zeros((512,512,3), np.uint8) 这是黑色的底,我们的画布,我把窗口名叫做i ...
- python之OpenCv(三)---基本绘图
opencv 提供了绘制直线.圆形.矩形等基本绘图的功能 1.绘直线 cv2.line(画布,起点坐标,终点坐标,颜色,宽度) 例如: cv2.line(image,(20,60),(300,400) ...
- 5、opencv中的绘图函数
1.目标 a.学习使用 OpenCV 绘制不同几何图形 b. 你将会学习到这些函数: cv2.line(), cv2.circle(), cv2.rectangle(),cv2.ellipse(),c ...
- OpenCV中的绘图函数
OpenCV可以用来绘制不同的集合图形,包括直线,矩形,圆,椭圆,多边形以及在图片上添加文字.用到的绘图函数包括 cv2.line(),cv2.circle(),cv2.rectangle() ,cv ...
- opencv学习(三)——绘图功能
绘图功能 我们将学习以下函数:cv.line(),cv.circle(),cv.rectangle(),cv.ellipse(),cv.putText()等. 在这些功能中,有一些相同的参数: img ...
- OpenCV绘图
OpenCV绘图 rectangle(Mat& img,Point pt1, Point pt2, const Scalar&color, int thickness=1,int li ...
- OpenCV绘图函数
OpenCV几个绘图函数 矩形 rectangle(Mat& img,Point pt1, Point pt2, const Scalar&color, int thickness=1 ...
- OpenCV之响应鼠标(四):在图像上绘制出矩形并标出起点的坐标
涉及到两方面的内容:1. 用鼠标画出矩形.2.在图像上绘制出点的坐标 用鼠标绘制矩形,涉及到鼠标的操作,opencv中有鼠标事件的介绍.需要用到两个函数:回调函数CvMouseCallback和注册回 ...
- OpenCV中cv2的用法
一.读入图像 使用函数cv2.imread(filepath,flags)读入一副图片 filepath:要读入图片的完整路径 flags:读入图片的标志 cv2.IMREAD_COLOR:默认参数 ...
随机推荐
- hdu2066多源最短路
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/2066/ SPFA可以高效过,代码如下: #include<bits/stdc++.h> using ...
- P5020 货币系统 题解
原题链接 简要题意: 求一个长度最小的货币系统与给出的货币系统等价.求这个货币系统的长度.等价的定义详见题目,不再赘述. 本文可能用到一些集合论,请放心食用. 算法一 \(n=2\) 时,只需判断两个 ...
- 报错:Error updating database. Cause: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'gender' at row 1
我遇到这个错误是解决办法:我修改了对应的数据库字段的长度:原本我设置的是char型,但是存进去的是一个string型,结果就报了这个报了这个错误 下面就是数据库修改之后的结果. 以上都是我学习过程中遇 ...
- NSObject常用方法
类 @interface NSObject <NSObject> { Class isa OBJC_ISA_AVAILABILITY; } // 初始化加载 + (void)load; / ...
- mac 根目录下新建文件夹并赋予权限
在根目录中,你会发现你无法创建文件夹,即使使用命令也无法创建目录: 1.修改auto_master 编译 /etc/auto_master 文件,注释掉或者移除以 /home 开头的那一行,保存. 终 ...
- python:<class 'numpy.ndarray'>的学习
在学习opencv-python的时候,给出图片地址再调用cv2.imread("地址"),发现出创建的是numpy类型的ndarray对象,用来存放多维数组的对象 # 导入cv2 ...
- 5个最佳WordPress通知栏插件
作者:品博客 链接:https://blog.pingbook.top/328/ 来源:品博客 著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. WordPress通知栏可有效地将 ...
- 【php】面向对象(四)
知识点:ai一. a => abstract(抽象类) a) 抽象类的修饰符,修饰类和成员方法 b) 注意:被修饰的类不能被实例化,被修饰的方法不能有程序体 c) 如果某一个类使用abstrac ...
- readthedocs网托管持多语言文档
希望在readthedocs上创建支持多语言的文档,效果类似: 通过语言选项,可以切到到不同的语言版本:实现这个目标包含两个主要步骤: 在本地对文档进行翻译 在readthedocs.org上配置翻译 ...
- 微信小程序 使用include导入wxml文件注意的问题
(1)使用inlucde的时,要注意将最后的终止符 / 添加上去,否则不能正常的导入界面内容 <include src="header.wxml"/> (2)引入文件注 ...