Opencv数据类型(一): Point类、Scalar类、Size类

一、Point类

1、Point类的一些别名,如Point2i,Point3f等等

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;

2、Point类支持的运算符重载

cv::Point2i pointl32a(2, 0);
cv::Point2i pointl32b(3, 4);
cv::Point2i pointl32c;

double f64val = norm(pointl32a); // L2 norm

pointl32c = pointl32a + pointl32b;
pointl32c = pointl32a - pointl32b;
pointl32c = 2 * pointl32b;
pointl32c = pointl32b / 3;
pointl32c += pointl32b;
pointl32c -= pointl32b;
pointl32c *= 2;
pointl32c /= 2;

bool b = pointl32a == pointl32b;
bool c = pointl32a == pointl32b;

3、Point_ 类模板

template<typename _Tp> class Point_
{
public:
    typedef _Tp value_type;

    //! default constructor 默认构造函数
    Point_();
    Point_(_Tp _x, _Tp _y);
    Point_(const Point_& 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
    // 转换成老式的C结构体
    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; //!< x coordinate of the point
    _Tp y; //!< y coordinate of the point
};

4、举例

cv::Point2i pointl32a(2, 2);
cv::Point2i pointl32b(3, 4);

cv::Point2f pointf32a(2.0, 1.0);
cv::Point2f pointf32b(3.1, 4.2);
//
double f64d = pointf32a.ddot(pointf32b);  // P1.x * p2.x + p1.y * p2.y
std::cout << f64d << std::endl;

double f6d = pointl32a.cross(pointl32b);  // P1.x*P2.y - P1.y * P2.x;
std::cout << f6d << std::endl;

二、Scalar类

1、Scalar类

cv::Scalar是一个四维点类,且是从固定向量模板中继承过来的,因为具备所有的向量操作,成员访问函数,如操作符[],简单的可以看成四维双精度向量的快速表示

typedef struct Scalar
{
     double val[4];
}Scalar;

下表是一些Scalar类直接支持的才操作。
在这里插入图片描述2、举例
例1:支持 共轭 conj(),元素相乘等操作


int main() {
    cv::Scalar a(1,2,3,4);
    cv::Scalar b(4,5,6,7);
//    std::cout << a[2] << std::endl;
    cv::Scalar c = b.mul(a);
    for(int i=0; i<4; ++i)
    {
        std::cout << " " << c[i]; // cout 4 10 18 28 
    }

    std::cout << std::endl;

    for(int i=0; i<4; ++i)
    {
        std::cout << " " << b.conj()[i]; // s0, -s1, -s2, -s3
    }

    std::cout << std::endl;
    std::cout << "hello world" << std::endl;
    return 0;
}

例2:Mat初始化

Mat M(4,4,CV_32FC3,Scalar(1,2,3)); //值为 1,2,3,大小4*4

三、Size类

相比Point类,
1.数据成员不同,Size类成员为width 和 height
2.不能转换成固定大小的Vector类,但Vector类和Point类可以转换成Size类

Size类存在三种别名 Size 、Size2i、Size2f,Size2d前面两个是等价的

typedef Size_<int> Size2i;
typedef Size_<int64> Size2l;
typedef Size_<float> Size2f;
typedef Size_<double> Size2d;
typedef Size2i Size;

3、Size_类模板

template<typename _Tp> class Size_
{
public:
    typedef _Tp value_type;

    //! default constructor
    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;
    //! true if empty
    bool empty() const;

    //! conversion of another data type.
    template<typename _Tp2> operator Size_<_Tp2>() const;

    _Tp width; //!< the width
    _Tp height; //!< the height
};

3、举例

int main() {
    cv::Size sz(3,5);
    std::cout << sz.width << " " << sz.height << std::endl;
    std::cout << sz.area() << std::endl;
    std::cout << sz.empty() << std::endl;
    std::cout << "hello world" << std::endl;
    return 0;
}
  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值