opencv坐标点模板类Point 您所在的位置:网站首页 c语言point函数类型 opencv坐标点模板类Point

opencv坐标点模板类Point

2024-06-17 11:03| 来源: 网络整理| 查看: 265

有时候我们在实现一些图像处理算法的时候,往往会对图像像素点进行相关的运算和处理。传入或输出的坐标点往往是x,y的数组向量,如

1 2 3 4 5 6 7 8 9 //int 类型 std::vector pts; std::vector pts; //float类型 std::vector pts //double 类型 std::vector pts

opencv中内置了三种二维平面点坐标类型,这就带来一个问题:即我们在编写图像处理算法的时候有时候并不确定调用者需要哪一种坐标点数据类型,只写一种吧可能无法满足需要,三种都分别实现又费时耗力,且没利用好c++多态泛型的特性。而opencv中的模板类cv::Point_刚好可以解决这个问题,实际上以上三种类型的点都是cv::Point_的具体实例化,在opencv里转到定义就可以查看到

1 2 3 4 5 typedef Point_ Point2i; typedef Point_ Point2l; typedef Point_ Point2f; typedef Point_ Point2d; typedef Point2i Point;

所以实际上内置的三种类型点都是模板类的具体实例化,参考其代码理论上你还可以自己扩展新的点集类型,比如

1 2 3 typedef Point_ Point2s; typedef Point_ Point2l; //...

在opencv里面可以查看到cv::Point_的定义是这样子的:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 template class Point_ { public: typedef _Tp value_type; //! default constructor Point_(); Point_(_Tp _x, _Tp _y); Point_(const Point_& pt); Point_(const Size_& sz); Point_(const Vec& v); Point_& operator = (const Point_& pt); //! conversion to another data type template operator Point_() const; //! conversion to the old-style C structures operator Vec() 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_& r) const; _Tp x; //!< x coordinate of the point _Tp y; //!< y coordinate of the point };

比较简洁,里面就定义了构造函数、花括号列表初始化、点乘以及叉乘等操作。回到最开始的问题,如何利用模板类cv::Point_一次编写适用多种类型坐标点的算法呢。原理很简单,其实就是c++模板的用法。下面是一个最简单的c++使用方法

1 2 3 4 5 template inline T const& max (T const& a, T const& b) { return a


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有