您的位置:首页 > 理论基础 > 数据结构算法

opencv学习(1)之基本数据结构

2017-07-14 22:54 295 查看
Opencv提供了多种基本数据类型,例如 Point(点),Size(尺寸),Rect(区域), Scalar(颜色表示)这些类型定义在opencv2\core\types.hpp中

cv::Point

应该说是opencv中最基本也是最简单的类型了。它表示一个二维坐标点。包含一个整型数据x和一个整型数据y。例如:

cv::Point pt(2, 5);等等

在types.hpp中还定义了一些变种类型,浮点型等。

typedef Point_<int> Point2i;
typedef Point_<int64> Point2l;
typedef Point_<float> Point2f;
typedef Point_<double> Point2d;
typedef Point2i Point;


三维坐标点有

typedef Point3_<int> Point3i;
typedef Point3_<float> Point3f;
typedef Point3_<double> Point3d;

例如 cv::Point3d pt(1,2,3);


cv::Size

用来表示一个尺寸量(宽,高)。例如 cv::Size(12,34);

在types.hpp中有如下定义:和point一样都是模板类

template<typename _Tp> class Size_
{
public:
typedef _Tp value_type;
//! various constructors
Size_();
Size_(_Tp _width, _Tp _height);
Size_(const Size_& 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;
// 基本属性
_Tp width, height;
};
typedef Size_<int> Size2i;
typedef Size_<int64> Size2l;
typedef Size_<float> Size2f;
typedef Size_<double> Size2d;
typedef Size2i Size;  //最常用


cv::Rect

用来表示一个矩形区域(x, y, width,height) 。其中x,y 为矩形的左上角坐标。 例如:cv::Rect rt(1,1, 20,30);

在types.hpp中有如下定义:

template<typename _Tp> class Rect_
{
public:
typedef _Tp value_type;
//构造
Rect_();
Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height);
Rect_(const Rect_& 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;

//!判断点是否在区域内
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
};
typedef Rect_<int> Rect2i;
typedef Rect_<float> Rect2f; // 浮点型
typedef Rect_<double> Rect2d;
typedef Rect2i Rect; // 最常用


cv::Scalar

用来表示颜色数据的类。

template<typename _Tp> class Scalar_ : public Vec<_Tp, 4>  // 包含4个数据的数组
{
public:
//! various constructors
Scalar_();
Scalar_(_Tp v0, _Tp v1, _Tp v2=0, _Tp v3=0);
Scalar_(_Tp v0);

template<typename _Tp2, int cn>
Scalar_(const Vec<_Tp2, cn>& v);

//! returns a scalar with all elements set to v0
static Scalar_<_Tp> all(_Tp v0);

//! conversion to another data type
template<typename T2> operator Scalar_<T2>() const;
//! per-element product
Scalar_<_Tp> mul(const Scalar_<_Tp>& a, double scale=1 ) const;

// returns (v0, -v1, -v2, -v3)
Scalar_<_Tp> conj() const;

// returns true iff v1 == v2 == v3 == 0
bool isReal() const;
};
typedef Scalar_<double> Scalar;  // 最常用


例如 cv::Scalar(b , g, r, a); // 一般我们默认只用前3个参数

它表示颜色重的RGB值 b:为blue, g为green,r 为red,a为alpha
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  opencv c++