Các kiểu dữ liệu cơ bản

Một phần của tài liệu nghiên cứu thư viện opencv ứng dụng trong việc nhận dạng biển báo giao thông (Trang 20 - 24)

CHƯƠNG 2: CƠ SỞ LÝ THUYẾT

2.3.2. Các kiểu dữ liệu cơ bản

Lớp mẫu (Template class) cho một điểm 2 chiều trong toạ độ Oxy, chứa toạ độ x và y.

template<typename _Tp> class Point_ { public:

//! various constructors Point_();

Point_(_Tp _x, _Tp _y);

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

Do Point_ là tempate, nên OpenCV đặt bí danh (alias) để cho tiện trong việc sử dụng:

typedef Point_<int> Point2i;

typedef Point2i Point;

typedef Point_<float> Point2f;

typedef Point_<double> Point2d;

Ví dụ 2.1: Tính khoảng cách giữa 2 điểm trong không gian 2 chiều

#include <iostream>

#include "opencv2/opencv.hpp"

using namespace std;

using namespace cv;

double dis;

template<typename _T> inline _T sqr(_T const &num) { return num * num;

}

int main(int argc, char const* argv[]) { // First way to use

Point pt1(-2, 1);

// Second way to use Point pt2;

pt2.x = 1; pt2.y = 5;

dis = sqrt(sqr((pt2.x - pt1.x)) + sqr((pt2.y - pt1.y)));

cout << "Distance between 2 points: " << dis << endl;

return 0;

}

Rect_

Template class cho hình chữ nhật 2 chiều, chứa thông tin về toạ độ (x, y) của điểm ở trên, bên trái (top – left) và kích thước (width và height) của hình chữ nhật.

template<typename _Tp> class Rect_ { public:

Rect_();

Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height);

Rect_(const Point_<_Tp> &org, const Size_<_Tp> &sz);

Rect_(const Point_<_Tp> &pt1, const Point_<_Tp> &pt2);

//! 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;

//! checks whether the rectangle contains the point bool contains(const Point_<_Tp> &pt) const;

//< the top-left corner, as well as width and height of the rectangle _Tp x, y, width, height;

};

Bí danh:

typedef Rect_<int> Rect;

Scalar_

Template class đại diện cho một vector có 4 phần tử.

template<typename _Tp> class Scalar_ : public Vec<_Tp, 4> { public:

//! various constructors Scalar_();

Scalar_(_Tp v0, _Tp v1, _Tp v2 = 0, _Tp v3 = 0);

Scalar_(_Tp v0);

//! returns a scalar with all elements set to v0 static Scalar_<_Tp> all(_Tp v0);

// returns true iff v1 == v2 == v3 == 0 bool isReal() const;

};

Các bí danh:

typedef Scalar_<double> Scalar;

Ví dụ 2.2: Tạo một hằng số chứa thông tin của màu hồng

#include <iostream>

#include "opencv2/opencv.hpp"

using namespace std;

using namespace cv;

#define PINK_COLOR Scalar(203, 192, 255) int main(int argc, char const* argv[]) {

cout << "Value 3 channels of Pink color:" << endl;

cout << " - Blue: " << PINK_COLOR.val[0] << endl;

cout << " - Green: " << PINK_COLOR.val[1] << endl;

cout << " - Red: " << PINK_COLOR.val[2] << endl;

return 0;

}

Mat

Class thể hiện một ma trận nhiều chiều của một hoặc nhiều kênh; có thể lưu trữ thông tin cho hình ảnh, biểu đồ tần số, ma trận phức tạp,...

class CV_EXPORTS Mat { public:

Mat();

//! constructs 2D matrix of the specified size and type (CV_8UC1, CV_64FC3, CV_32SC(12) etc.)

Mat(int rows, int cols, int type);

//! constucts 2D matrix and fills it with the specified value _s.

Mat(int rows, int cols, int type, const Scalar &s);

//! creates a matrix header for a part of the bigger matrix Mat(const Mat &m, const Rect &roi);

//! allocates new matrix data unless the matrix already has specified size and type previous data is unreferenced if needed.

void create(int rows, int cols, int type);

void create(Size size, int type);

int type() const;

int depth() const;

int channels() const;

//! returns true if matrix data is NULL bool empty() const;

//! the number of rows and columns int rows, cols;

};

Trong đó,

• rows – Số dòng của ma trận

• cols – Số cột của ma trận

• type – Kiểu ma trận – được biểu thị bởi CV_<depth><U|S|F>C<channel(s)>

◦ depth và <U|S|F> dựa vào kiểu nguyên thuỷ (primitive type) gồm:

▪ 8-bit unsigned integer (uchar): CV_8U

▪ 8-bit signed integer (schar): CV_8S

▪ 16-bit unsigned integer (ushort): CV_16U

▪ 16-bit signed integer (short): CV_16S

▪ 32-bit signed integer (int): CV_32S

▪ 32-bit floating-point number (float): CV_32F

▪ 64-bit floating-point number (double): CV_64F

◦ channels(s) – Số kênh của ma trận

▪ Nếu số kênh từ 1 đến 4 có thể sử dụng các hằng số CV_8UC1, … CV_64FC4

▪ Nếu nhiều hơn 4 có thể sử dụng các macros: CV_8UC(n),... CV_64C(n) hoặc

CV_MAKETYPE(CV_8U, n),... CV_MAKETYPE(CV_64F, n)

• data – Dữ liệu của ma trận

Ví dụ 2.3: Một số cách tạo đối tượng Mat, trong ví dụ tạo một hình ảnh 3 kênh, có kích thước 500x500 pixels và một vùng nhỏ (Region of Interest – RoI) trong một hình ảnh lớn.

#include <iostream>

#include "opencv2/opencv.hpp"

using namespace std;

using namespace cv;

int main(int argc, char const* argv[]) { Mat image(500, 500, CV_8UC3);

Rect roi(0, 0, 50, 40);

Mat roi(image, roi);

return 0;

}

Một phần của tài liệu nghiên cứu thư viện opencv ứng dụng trong việc nhận dạng biển báo giao thông (Trang 20 - 24)

Tải bản đầy đủ (PDF)

(65 trang)