继续core模块学习之路。
5 RotatedRect类
RotatedRect类和Rect类的用法基本一致,Rect表示存储无偏转的矩形信息,RotatedRect是用来存储旋转矩形信息的。
class CV_EXPORTS RotatedRect
{
public:
//! various constructors
RotatedRect();
RotatedRect(const Point2f& center, const Size2f& size, float angle);
RotatedRect(const CvBox2D& box);
//! returns 4 vertices of the rectangle
void points(Point2f pts[]) const;
//! returns the minimal up-right rectangle containing the rotated rectangle
Rect boundingRect() const;
//! conversion to the old-style CvBox2D structure
operator CvBox2D() const;
Point2f center; //< the rectangle mass center
Size2f size; //< width and height of the rectangle
float angle; //< the rotation angle. When the angle is 0, 90, 180, 270 etc., the rectangle be
};
以第二个构造函数说明RotatedRect(const Point2f& center, const Size2f& size, float angle);
初始化旋转矩形的时候需要指定矩形中心坐标(质心)用Point_类结构表示,矩形的大小用Size_类表示,和顺时针旋转角度θ用float类型表示,注意这里是用角度表示而不是弧度。
举例代码
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
Mat image(500, 500, CV_8UC3, Scalar(0));
RotatedRect rRect = RotatedRect(Point2f(250, 250), Size2f(200,130), 60);
Point2f vertices[4];
rRect.points(vertices);
for (int i = 0; i < 4; i++)
line(image, vertices[i], vertices[(i + 1) % 4], Scalar(0, 255, 121));
Rect brect = rRect.boundingRect();
rectangle(image, brect, Scalar(255, 150, 0));
imshow("rectangles", image);
waitKey(0);
}
结果绘制出一个正矩形和一个偏转矩形
6 Vec类 和Scalar_类
Vec类用来存储向量,在OpenCV中一般和Scalar_类的功能类似就是用来表示多通道像素类型。Vec模板类的定义如下
template<typename _Tp, int m, int n> class Matx {...}
_Tp 表示单个元素的数据类型,n 表示向量的维数。
下面是一些Vec模板类的容易记忆的别名alias
typedef Vec<uchar, 2> Vec2b; //二维uchar型向量
typedef Vec<uchar, 3> Vec3b; //三维uchar型向量
typedef Vec<uchar, 4> Vec4b;
typedef Vec<short, 2> Vec2s; //二维short型向量
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;
Scalar_类的定义如下class Scalar_
Template class for a 4-element vector derived from Vec.
template<typename _Tp> class CV_EXPORTS Scalar_ : public Vec<_Tp, 4>
{
public:
//! various constructors
Scalar_();
Scalar_(_Tp v0, _Tp v1, _Tp v2=0, _Tp v3=0);
Scalar_(const CvScalar& s);
Scalar_(_Tp v0);
//! returns a scalar with all elements set to v0
static Scalar_<_Tp> all(_Tp v0);
//! conversion to the old-style CvScalar
operator CvScalar() const;
//! conversion to another data type
template<typename T2> operator Scalar_<T2>() const;
//! per-element product
Scalar_<_Tp> mul(const Scalar_<_Tp>& t, double scale=1 ) const;
// returns (v0, -v1, -v2, -v3)
Scalar_<_Tp> conj() const;
// returns true iff v1 == v2 == v3 == 0
bool isReal() const;
};
Scalar_类的常用结构别名typedef Scalar_<double> Scalar;Scalar用来存储4维的向量,常用来表示像素通道类型或bgr颜色向量,虽然bgr颜色是三维向量,但OpenCV中Scalar会默认忽略第四个参数。 还有几种相互等价的结构,他们可以相互转换
Vec<T, 2> Point_
Vec<T, 3> Point3_
Vec<T, 4> CvScalar Scalar_
7Range类
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;
};
目的是取得某一向量[x, y, z, ...] 的[begin, end) 这一段连续子列,这一子列是个左闭右开区间。