1 Opencv数据类型 (一)

1.前言

  跟学习大多数编程编程语言一样,我们首先学习Opencv数据类型。在Opencv中,设计了大量的数据类型,来方便我们直观地处理一些计算机视觉中的概念。主要分为三大类:
- 基础数据类型。主要是C++的内建类型(int, float,等)和一些简单的vector和matrices,它们用来表示一些简单的几何概念,比如:点、矩形、尺寸等。
- 帮助类型。用来表示一些抽象的概念,比如:指针类的垃圾回收器、用来切片操作的范围对象等。
- 大数组类型,用来存储高维数据类,比如cv::Mat.

2. 基础数据类型

  基础数据类型主要由点类(point class)、张量类(Scalar class)、尺寸类(Size class)、矩形类(Rect class)、固定矩阵类(The fixed matrix class)、固定向量类(The fixed vector class)和复数类(The complex number classes)组成。

  • 点类
      点类是基础数据类型中最简单的,它是基于C++模板实现的,所以支持任何数据类型(int, float, double)的点。实际上它有两套模板,一个是二维点类,一个是三维点类,通过源码可知,点类是通过别名来实例化的。
typedef Point_<int> Point2i;
typedef Point_ Point2l;
typedef Point_<float> Point2f;
typedef Point_<double> Point2d;
typedef Point2i Point;

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

注:其中 i i 代表是int, l l 代表long, f f 代表float, d代表double。点类默认是二维点

实际操作

//二维点类默认构造函数
    Point2i p2i;
    Point2f p2f;
    Point2d p2d;

    //三维点类默认构造函数
    Point2i p3i;
    Point2f p3f;
    Point2d p3d;

    //二维点类构造函数
    Point2i pi(1, 2);
    Point2f pf(3.1, 4.1);
    Point2d pd(5.1, 6.1);

    cout << "二维点类构造函数" << endl;
    cout << pi << endl;
    cout << pf << endl;
    cout << pd << endl;

    //三维点类构造函数
    Point3i pi3(1, 2, 3);
    Point3f pf3(3.1, 4.1, 5.1);
    Point3d pd3(5.1, 6.1, 6.1);

    cout << "三维点类构造函数" << endl;
    cout << pi3 << endl;
    cout << pf3 << endl;
    cout << pd3 << endl;

1 Opencv数据类型 (一)_第1张图片
由经验可知,一个二维点是由 x x y y 两个坐标组成,从源码可知,点类内置了这两个成员。

_Tp x, y; //< the point coordinates

_Tp x, y, z; //< the point coordinates

实际操作

//输出二维点类的坐标
    cout << "输出二维坐标\n";
    cout << "x: " << pi.x << " y: " << pi.y << endl;

    //输出三维点类的坐标
    cout << "输出三维坐标\n";
    cout << "x: " << pi3.x << " y: " << pi3.y << " z: " << pi3.z << endl;

这里写图片描述
完整代码

#include
#include

using namespace cv;
using namespace std;

int main()
{
    //二维点类默认构造函数
    Point2i p2i;
    Point2f p2f;
    Point2d p2d;

    //三维点类默认构造函数
    Point2i p3i;
    Point2f p3f;
    Point2d p3d;

    //二维点类构造函数
    Point2i pi(1, 2);
    Point2f pf(3.1, 4.1);
    Point2d pd(5.1, 6.1);

    cout << "二维点类构造函数" << endl;
    cout << pi << endl;
    cout << pf << endl;
    cout << pd << endl;

    //三维点类构造函数
    Point3i pi3(1, 2, 3);
    Point3f pf3(3.1, 4.1, 5.1);
    Point3d pd3(5.1, 6.1, 6.1);

    cout << "三维点类构造函数" << endl;
    cout << pi3 << endl;
    cout << pf3 << endl;
    cout << pd3 << endl;

    //输出二维点类的坐标
    cout << "输出二维坐标\n";
    cout << "x: " << pi.x << " y: " << pi.y << endl;

    //输出三维点类的坐标
    cout << "输出三维坐标\n";
    cout << "x: " << pi3.x << " y: " << pi3.y << " z: " << pi3.z << endl;

    std::cin.get();

    return 0;
}
  • 尺寸类
    尺寸类实际上和点类类似,二者的区别在于点类的数据成员是横坐标( x x )和纵坐标( y y ), 而尺寸类的数据成员则为宽(width)和高(height).
typedef Size_<int> Size2i;
typedef Size_ Size2l;
typedef Size_<float> Size2f;
typedef Size_<double> Size2d;
typedef Size2i Size;

注:默认为int

#include
#include

using namespace cv;
using namespace std;

int main()
{
    //默认构造函数
    Size sz;
    Size2f sz1;

    //构造函数
    Size szi(3, 4);
    Size szf(5.0, 6.0);

    cout << szi << endl;
    cout << szf << endl;

    //输出宽和高
    cout << "输出宽和高" << endl;
    cout << "width :" << szi.width << " height: " << szi.height << endl;
    cout << "width :" << szf.width << " height: " << szf.height << endl;

    //输出面积
    cout << "输出面积" << endl;
    cout << "szi area: " << szi.area() << endl;
    cout << "szf area: " << szf.area() << endl;


    std::cin.get();

    return 0;
}

1 Opencv数据类型 (一)_第2张图片

你可能感兴趣的:(opencv3.0)