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