opencv 2.x学习笔记(三)基本数据类型(一)
来源:互联网 发布:13种网络钓鱼诈骗类型 编辑:程序博客网 时间:2024/05/16 11:41
上面两篇文章中,简单的介绍了OpenCV的一些使用方法。以使我们更快的了解OpenCV。而下面我们将更多的去探讨OpenCV内部的细节。为了方便我们使用,OpenCV为我们定义了许多数据类模板。了解这些基本的数据结构,将是我们更好使用OpenCV的基础。其中常见的如下所示:
Point_
OpenCV分别为我们定义了2维点坐标和3维点坐标的类模板Point_和Point3_。类定义如下:
template<typename _Tp> class Point_{public: typedef _Tp value_type; // various constructors Point_(); Point_(_Tp _x, _Tp _y); Point_(const Point_& pt); Point_(const CvPoint& pt); Point_(const CvPoint2D32f& pt); Point_(const Size_<_Tp>& sz); Point_(const Vec<_Tp, 2>& v); Point_& operator = (const Point_& pt); //! conversion to another data type template<typename _Tp2> operator Point_<_Tp2>() const; //! conversion to the old-style C structures operator CvPoint() const; operator CvPoint2D32f() const; operator Vec<_Tp, 2>() const; //! dot product _Tp dot(const Point_& pt) const; //! dot product computed in double-precision arithmetics double ddot(const Point_& pt) const; //! cross-product double cross(const Point_& pt) const; //! checks whether the point is inside the specified rectangle bool inside(const Rect_<_Tp>& r) const; _Tp x, y; //< the point coordinates};从上面的代码我们不难看出Point_类封装了对应数据类型的x,和y坐标值。并且提供了大量的构造函数,以及和C语言结构中的CvPoint等的结构转换。并提供了两个点的点积和向量积运算,以及一个判断点是否在一个矩形内的方法。
除了上面所提供的类成员方法,下列关于点的操作也是可以使用的。
pt1 = pt2 + pt3;pt1 = pt2 - pt3;pt1 = pt2 * a;pt1 = a * pt2;pt1 += pt2;pt1 -= pt2;pt1 *= a;double value = norm(pt);pt1 == pt2;pt1 != pt2
同时,为了方便我们的使用,OpenCV为我们定义了许多别名。如下所示:
typedef Point_<int> Point2i;typedef Point2i Point;typedef Point_<float> Point2f;typedef Point_<double> Point2d;
Point3_和Point2_类似,只是封装了x,y,z坐标。在此不再赘述。
Size_
还有一个简单的类模板Size_,这个类模板为我们指定了矩阵或者图像的大小。类定义如下:
template<typename _Tp> class Size_{public: typedef _Tp value_type; //! various constructors Size_(); Size_(_Tp _width, _Tp _height); Size_(const Size_& sz); Size_(const CvSize& sz); Size_(const CvSize2D32f& sz); Size_(const Point_<_Tp>& pt); Size_& operator = (const Size_& sz); //! the area (width*height) _Tp area() const; //! conversion of another data type. template<typename _Tp2> operator Size_<_Tp2>() const; //! conversion to the old-style OpenCV types operator CvSize() const; operator CvSize2D32f() const; _Tp width, height; // the width and the height};
可以看到这个类封装了一个叫做宽度和高度的变量。同时提供了计算面积的成员方法。其他的和Point_类类似。没什么特别的。
Rect_
接下来我们看一下OpenCV提供的一个矩形类模板Rect_,类模板定义如下:
template<typename _Tp> class Rect_{public: typedef _Tp value_type; //! various constructors Rect_(); Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height); Rect_(const Rect_& r); Rect_(const CvRect& r); Rect_(const Point_<_Tp>& org, const Size_<_Tp>& sz); Rect_(const Point_<_Tp>& pt1, const Point_<_Tp>& pt2); Rect_& operator = ( const Rect_& r ); //! the top-left corner Point_<_Tp> tl() const; //! the bottom-right corner Point_<_Tp> br() const; //! size (width, height) of the rectangle Size_<_Tp> size() const; //! area (width*height) of the rectangle _Tp area() const; //! conversion to another data type template<typename _Tp2> operator Rect_<_Tp2>() const; //! conversion to the old-style CvRect operator CvRect() const; //! checks whether the rectangle contains the point bool contains(const Point_<_Tp>& pt) const; _Tp x, y, width, height; //< the top-left corner, as well as width and height of the rectangle};很简单,这个类指示封装了矩阵的左上角坐标,以及矩形的宽度和高度。其中宽度和高度即为矩阵的大小(Size_),同时也可用于计算面积(area())。另外我们在通过contains函数来计算一个点是否包含在矩阵中时,需要注意的是矩阵的顶部和左边的边界被包含,而不包含右边和底部的边界。
综合实例
下面看一个简单的例子:
#include <cv.h>#include <iostream>using namespace std;using namespace cv;int main(){Point2f p1(1.0, 2.0);Point_<float> p2(3.0, 4.0);float dotproduct;double crossproduct;dotproduct = p2.dot(p1);crossproduct = p1.cross(p2);cout << "dot product of p1 and p2 = " << dotproduct << endl;cout << "cross product of p1 and p2 = " << crossproduct << endl;p2 += p1;cout << "Now p2 = " << p2 << endl;Rect_<float> rect(1.0, 2.0, 3.0, 4.0);Size_<float> size;float area;size = rect.size();area = rect.area();cout << "rectangle's size is " << size << endl;cout << "rectangle's area is " << area << endl;cout << "rect contains p1 is " << rect.contains(p1) << endl;cout << "rect contains p2 is " << rect.contains(p2) << endl;return 0;}
运行结果
i
注:对于这些类型,我们都可以用<<操作符进行输出。其默认输出格式不再赘述。
0 0
- opencv 2.x学习笔记(三)基本数据类型(一)
- opencv 2.x学习笔记(四)基本数据类型(二)
- OpenCV学习笔记之一(OpenCV的基本数据类型)
- OpenCV学习笔记(1)opencv基本数据类型
- Kotlin学习笔记(一) 基本数据类型
- python学习笔记(一)基本数据类型
- OpenCV学习笔记 -- 基本数据类型
- OpenCV 学习笔记---基本数据类型
- 【OpenCV学习笔记】2.1OpenCV基本数据类型
- 计算机视觉与图像处理学习笔记(三)opencv的基本数据类型与简单图像处理函数
- C++学习笔记之三 数据处理(基本数据类型)
- 学习opencv(一)——扒一扒opencv中的一些基本数据类型
- opencv学习笔记(三)-绘制基本图形及文字
- Opencv学习笔记(二):Opencv中的基本数据类型与数据结构
- swift学习笔记(一)基本数据类型(上)
- swift学习笔记(一)基本数据类型(下)
- Java基础学习笔记(一)基本数据类型
- JavaScript学习笔记(一)基本概念及基本数据类型
- 2014.7.8 第一天学习hfss13
- 懂,你的App生,不懂,死!
- c++ primer 4th 习题6.12答案纠错及改进
- windows下绑定线程(进程)到指定的CPU核心
- CloudStack 配置不适用VRouter
- opencv 2.x学习笔记(三)基本数据类型(一)
- C++ Using recursion to process linked list, 深入理解递归的过程(part2)
- Android的学习之路(二)项目中原生文件的使用场景和文件介绍
- TCP连接中的异常断开情况处理
- Mac OS中使用Launchctl设置定时任务
- Android开发者必备的十大开发工具
- OCP 1Z0 053 117
- struts乱码问题
- 直方图结构体:CvHistogram