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

本文介绍了OpenCV中几个核心的数据类型,包括Point、Size、Rect和Scalar,并详细阐述了它们的定义、用途及变种类型。Point用于表示二维坐标点;Size用于表示尺寸;Rect用于表示矩形区域;Scalar则用于表示颜色数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值