Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

OpenCV for Android 核心資料結構解析:從型別宏到 Point、Size、Rect

編輯:關於 Android 開發

本文程式碼基於 OpenCV for Android 3.0。OpenCV 在 C++ 端定義了大量型別與幾何資料結構,理解它們的組織方式,讀範例程式碼時會順暢許多。

一、矩陣型別的構造原理

OpenCV 的矩陣型別定義在 opencv2/core/cvdef.h 中:

#define CV_CN_MAX 512
#define CV_CN_SHIFT 3
#define CV_DEPTH_MAX (1 << CV_CN_SHIFT)
#define CV_8U 0
#define CV_8S 1
#define CV_16U 2
#define CV_16S 3
#define CV_32S 4
#define CV_32F 5
#define CV_64F 6
#define CV_USRTYPE1 7
#define CV_MAT_DEPTH_MASK (CV_DEPTH_MAX - 1)
#define CV_MAT_DEPTH(flags) ((flags) & CV_MAT_DEPTH_MASK)
#define CV_MAKETYPE(depth,cn) (CV_MAT_DEPTH(depth) + (((cn)-1) << CV_CN_SHIFT))
#define CV_MAKE_TYPE CV_MAKETYPE
#define CV_8UC1 CV_MAKETYPE(CV_8U,1)
#define CV_8UC2 CV_MAKETYPE(CV_8U,2)
#define CV_8UC3 CV_MAKETYPE(CV_8U,3)
#define CV_8UC4 CV_MAKETYPE(CV_8U,4)
#define CV_8UC(n) CV_MAKETYPE(CV_8U,(n))
#define CV_16UC3 CV_MAKETYPE(CV_16U,3)
#define CV_32FC1 CV_MAKETYPE(CV_32F,1)
#define CV_32FC3 CV_MAKETYPE(CV_32F,3)
#define CV_64FC1 CV_MAKETYPE(CV_64F,1)
#define CV_64FC(n) CV_MAKETYPE(CV_64F,(n))

可以看出這一整批型別都由 CV_MAKETYPE 宏生成,它有兩個參數。

第一個參數:資料位深度,對應的含義如下:

CV_8U    8 位元無符號整數 (0-255)
CV_8S    8 位元有符號整數 (-128-127)
CV_16U   16 位元無符號整數 (0-65535)
CV_16S   16 位元有符號整數 (-32768-32767)
CV_32S   32 位元有符號整數
CV_32F   32 位元浮點數
CV_64F   64 位元浮點數

Depth 最大值為 8(0 到 7,即 CV_8U 到 CV_USRTYPE1),這可以從下面兩行推出:

#define CV_CN_SHIFT 3
#define CV_DEPTH_MAX (1 << CV_CN_SHIFT)

1 左移 3 等於 8,也就是說深度這個資訊需要佔 3 個位元。

第二個參數:每個元素的通道數,至少為 1。直接寫 CV_8U 這樣的型別代表單通道。而從:

#define CV_CN_MAX 512

可知通道數上限是 512,需要佔 9 個位元。

於 CV_MAKETYPE 做的事就很清楚了:把深度放在低 3 位、通道數放在高 9 位,合共 12 個位元組成一個 type 值,也就是矩陣型別。整個過程只需要看 CV_MAKETYPE、CV_MAT_DEPTH、CV_MAT_DEPTH_MASK 三個宏即可理解。

有意思的是,這套「用位元組包兩個資訊」的手法,和 Android 中的 MeasureSpec 簡直如出一轍。

二、DataType 樣板類

DataType 定義在 opencv2/core/traits.hpp,作用是讓基本資料型別能對應到 OpenCV 的矩陣型別。它用到了 C++ 的 traits 技巧(有興趣可搜尋「C++ traits」):

template<typename _Tp>
class DataType
{
public:
    typedef _Tp value_type;
    typedef value_type work_type;
    typedef value_type channel_type;
    typedef value_type vec_type;
    enum {
        generic_type = 1,
        depth = -1,
        channels = 1,
        fmt = 0,
        type = CV_MAKETYPE(depth, channels)
    };
};

有了它,就能用 DataType<T>::type 這樣的寫法取得對應的矩陣型別。

三、Point_ 樣板類

定義在 opencv2/core/types.hpp,可視為一個點的封裝,內含 x、y 兩個座標屬性,並重載了大量運算子:

template<typename _Tp>
class Point_
{
public:
    typedef _Tp value_type;
    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);

    template<typename _Tp2> operator Point_<_Tp2>() const;
    operator Vec<_Tp, 2>() const;

    _Tp dot(const Point_& pt) const;              // 點積
    double ddot(const Point_& pt) const;          // 雙精度點積
    double cross(const Point_& pt) const;         // 叉積
    bool inside(const Rect_<_Tp>& r) const;       // 是否在矩形內

    _Tp x, y;
};

並以 typedef 針對三種數值型別各定義別名,預設使用的 Point 是整數:

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

為了相容 C,另外定義了對應的結構體,C++ 環境下會在 #ifdef __cplusplus 區塊中補上建構子與型別轉換:

typedef struct CvPoint
{
    int x;
    int y;
#ifdef __cplusplus
    CvPoint(int _x = 0, int _y = 0): x(_x), y(_y) {}
    template<typename _Tp>
    CvPoint(const cv::Point_<_Tp>& pt): x((int)pt.x), y((int)pt.y) {}
    template<typename _Tp>
    operator cv::Point_<_Tp>() const {
        return cv::Point_<_Tp>(cv::saturate_cast<_Tp>(x), cv::saturate_cast<_Tp>(y));
    }
#endif
} CvPoint;

浮點與雙精度版本分別是 CvPoint2D32f 與 CvPoint2D64f。

四、三維座標 Point3_

多帶一個 z 軸即可表達立體空間座標,C++ 版本:

template<typename _Tp>
class Point3_
{
public:
    typedef _Tp value_type;
    Point3_();
    Point3_(_Tp _x, _Tp _y, _Tp _z);
    Point3_(const Point3_& pt);
    explicit Point3_(const Point_<_Tp>& pt);
    Point3_(const Vec<_Tp, 3>& v);
    Point3_& operator = (const Point3_& pt);

    template<typename _Tp2> operator Point3_<_Tp2>() const;
    operator Vec<_Tp, 3>() const;

    _Tp dot(const Point3_& pt) const;
    double ddot(const Point3_& pt) const;
    Point3_ cross(const Point3_& pt) const;

    _Tp x, y, z;
};

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

對應的 C 結構體是 CvPoint2D32f(含 x、y、z 三個 float)與 CvPoint2D64f。

五、Size_ 樣板類

結構與 Point_ 幾乎相同,只是兩個屬性換成 width 與 height,並多了一個求面積的 area():

template<typename _Tp>
class Size_
{
public:
    typedef _Tp value_type;
    Size_();
    Size_(_Tp _width, _Tp _height);
    Size_(const Size_& sz);
    Size_(const Point_<_Tp>& pt);
    Size_& operator = (const Size_& sz);

    _Tp area() const;
    template<typename _Tp2> operator Size_<_Tp2>() const;

    _Tp width, height;
};

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

C 端對應 CvSize 與 CvSize2D32f。

六、Rect_ 樣板類

具備 Point 與 Size 的所有資訊——給定左上角座標加上寬高,矩形就被完整描述出來了:

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);

    Point_<_Tp> tl() const;      // 左上角
    Point_<_Tp> br() const;      // 右下角
    Size_<_Tp>  size() const;    // 尺寸
    _Tp area() const;             // 面積
    template<typename _Tp2> operator Rect_<_Tp2>() const;
    bool contains(const Point_<_Tp>& pt) const;

    _Tp x, y, width, height;
};

typedef Rect_<int>    Rect2i;
typedef Rect_<float>  Rect2f;
typedef Rect_<double> Rect2d;
typedef Rect2i Rect;

C 端對應 CvRect。

七、Scalar_ 與 Vec

Scalar_ 本質上是一個四維向量,繼承自 Vec。最典型的用途是表示顏色——一個含 alpha 的 ARGB 顏色剛好四個分量:

template<typename _Tp>
class Scalar_ : public Vec<_Tp, 4>
{
public:
    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);

    static Scalar_<_Tp> all(_Tp v0);
    template<typename T2> operator Scalar_<T2>() const;
    Scalar_<_Tp> mul(const Scalar_<_Tp>& a, double scale = 1) const;
    Scalar_<_Tp> conj() const;
    bool isReal() const;
};

typedef Scalar_<double> Scalar;

C 端對應 CvScalar,內部是 double val[4]。

而 Vec 定義在 opencv2/core/matx.hpp,繼承自 Matx,表示向量,支援 1 到 10 個元素的建構子,並提供 mul(逐元素相乘)、conj(共軛)與 cross(僅三維向量可用的叉積)等操作。

template<typename _Tp, int cn>
class Vec : public Matx<_Tp, cn, 1>
{
public:
    typedef _Tp value_type;
    enum {
        depth = Matx<_Tp, cn, 1>::depth,
        channels = cn,
        type = CV_MAKETYPE(depth, channels)
    };
    // 支援 1 ~ 10 個元素的建構子
    Vec(_Tp v0);
    Vec(_Tp v0, _Tp v1);
    Vec(_Tp v0, _Tp v1, _Tp v2);
    Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3);
    // ...
    explicit Vec(const _Tp* values);

    static Vec all(_Tp alpha);
    Vec mul(const Vec<_Tp, cn>& v) const;
    Vec conj() const;
    Vec cross(const Vec& v) const;
    template<typename T2> operator Vec<T2, cn>() const;

    const _Tp& operator [](int i) const;
    _Tp& operator[](int i);
};

以 typedef 預先定義好的常用組合非常多,涵蓋 uchar、short、ushort、int、float、double 等型別的 2 到 8 維向量,例如:

typedef Vec<uchar, 3>  Vec3b;
typedef Vec<short, 2>  Vec2s;
typedef Vec<int, 2>    Vec2i;
typedef Vec<float, 3>  Vec3f;
typedef Vec<double, 4> Vec4d;

結語

除了上述幾個,這些標頭檔中還定義了大量其他基礎型別。掌握「位元組包型別資訊」與「樣板 + typedef 產生各精度版本」這兩個設計思路,再回頭看 OpenCV 的原始碼,就不容易迷路了。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved