来源于thefutureisour的文档http://blog.csdn.net/thefutureisour/article/details/8455342,感觉很有用,就拿过来保存一下。
首先介绍2维点对Point_,它的是一个模板类。我们可以直接访问数据成员x,y。它不仅定了+、-、==、!=这4个基本的操作,还定义了点乘、叉乘等操作。特别的这个类还提供了inside函数来判断一个点是否在矩形区域内。此外,还定义了一些其他的类型转化函数,比如转化为1.X版本的CvPoint。
为了方便使用,opencv又对常用的类型进行了定义:
typedef Point_<int> Point2i;
typedef Point2i Point;
typedef Point_<float> Point2f;
typedef Point_<double> Point2d;
同理还有Point3_,只不过它是一个3维点(x,y,z)而已。它的常用类型是:
typedef Point3_<int> Point3i;
typedef Point3_<float> Point3f;
typedef Point3_<double> Point3d;
介绍完点,就可以介绍Size_了。它也是模板类。
typedef Size_<int> Size2i;
typedef Size2i Size;
typedef Size_<float> Size2f
Size能够访问的成员变量是height和width。还定义了area函数来求面积。其他的操作基本都是类型转化函数。
下来介绍Rect_模版类。它是由左上角点和长度、宽度定义的。在opecv中,一般定义为左开右闭区间。有意思的是,这个类竟然也提供了一个Rect+Point的函数,作用是对矩形的偏移,还有一个Rect + Size的函数,在左上角不变的情况下,重新调整矩形的大小。其他的操作还有与&和|,是求两个矩形的交集和并集。
除了基本的矩形之外,opecv还提供了一个可以旋转的矩形RotatedRect,它是由中心、变长、旋转角度决定的。你可以访问它的这三个成员,也可以使用points函数返回它的4个顶点,使用boundingRect求出它的外接矩形(非旋转),下面是一个例子:
- int main(void)
- {
- Mat bg(200,200,CV_8UC3,Scalar(0));
- imshow("",bg);
- RotatedRect rRect(Point2f(100,100),Size(100,100),40);
- Point2f vertices[4];
- rRect.points(vertices);
- for(int i = 0; i < 4;++i)
- line(bg,vertices[i],vertices[(i+1)%4],Scalar(0,255,0));
- Rect brect = rRect.boundingRect();
- rectangle(bg,brect,Scalar(255,0,0));
- imshow("",bg);
- waitKey();
- return 0;
- }
int main(void)
{
Mat bg(200,200,CV_8UC3,Scalar(0));
imshow("",bg);
RotatedRect rRect(Point2f(100,100),Size(100,100),40);
Point2f vertices[4];
rRect.points(vertices);
for(int i = 0; i < 4;++i)
line(bg,vertices[i],vertices[(i+1)%4],Scalar(0,255,0));
Rect brect = rRect.boundingRect();
rectangle(bg,brect,Scalar(255,0,0));
imshow("",bg);
waitKey();
return 0;
}
下面介绍Matx类,这也是一个模板类,用来记录一些小的矩形。这些矩形在编译前大小就固定了:
typedef Matx<float, 1, 2> Matx12f;
typedef Matx<double, 1, 2> Matx12d;
...
typedef Matx<float, 1, 6> Matx16f;
typedef Matx<double, 1, 6> Matx16d;
typedef Matx<float, 2, 1> Matx21f;
typedef Matx<double, 2, 1> Matx21d;
...
typedef Matx<float, 6, 1> Matx61f;
typedef Matx<double, 6, 1> Matx61d;
typedef Matx<float, 2, 2> Matx22f;
typedef Matx<double, 2, 2> Matx22d;
...
typedef Matx<float, 6, 6> Matx66f;
typedef Matx<double, 6, 6> Matx66d;
如果要使用灵活的矩形,还是用Mat吧。
下面介绍Vec类,它其实是元素较少的向量。
typedef Vec<uchar, 2> Vec2b;
typedef Vec<uchar, 3> Vec3b;
typedef Vec<uchar, 4> Vec4b;
typedef Vec<short, 2> Vec2s;
typedef Vec<short, 3> Vec3s;
typedef Vec<short, 4> Vec4s;
typedef Vec<int, 2> Vec2i;
typedef Vec<int, 3> Vec3i;
typedef Vec<int, 4> Vec4i;
typedef Vec<float, 2> Vec2f;
typedef Vec<float, 3> Vec3f;
typedef Vec<float, 4> Vec4f;
typedef Vec<float, 6> Vec6f;
typedef Vec<double, 2> Vec2d;
typedef Vec<double, 3> Vec3d;
typedef Vec<double, 4> Vec4d;
typedef Vec<double, 6> Vec6d;
它支持加、减、数乘、相等、不等、求范数等运算。
Scalar_类其实是用Vec<tp,4>派生下来的,也就是说,它是一个4元组:typedef Scalar_<double> Scalar;
他通常用来传递像素。
Range类用来指定连续的子序列。比如矩阵的一部分,比较简单,我们直接看定义:
- class CV_EXPORTS Range
- {
- public:
- Range();
- Range(int _start, int _end);
- Range(const CvSlice& slice);
- int size() const;
- bool empty() const;
- static Range all();
- operator CvSlice() const;
- int start, end;
- };
class CV_EXPORTS Range
{
public:
Range();
Range(int _start, int _end);
Range(const CvSlice& slice);
int size() const;
bool empty() const;
static Range all();
operator CvSlice() const;
int start, end;
};