OPENCV(2) —— Basic Structures(一)
阅读原文时间:2023年07月12日阅读:1

DataType

A primitive OpenCV data type is one of unsigned char, bool,signed char, unsigned short, signed short, int, float, double, or a tuple of values of one of these types, where all the values in the tuple have the same type.

类型的命名格式:

CV_{U|S|F}C()

A universal OpenCV structure that is able to store a single instance of such a primitive data type is Vec. Multiple instances of such a type can be stored in a std::vector, Mat, Mat_, SparseMat, SparseMat_, or any other container that is able to store Vec instances.

The DataType class is basically used to provide a description of such primitive data types without adding any fields or methods to the corresponding classes 。 The main purpose of this class is to convert compilation-time type information to an OpenCV-compatible data type identifier

// allocates a 30x40 floating-point matrix
Mat A(30, 40, DataType::type);

Mat B = Mat_ >(3, 3);
// the statement below will print 6, 2 /*, that is depth == CV_64F, channels == 2 */
cout << B.depth() << ", " << B.channels() << endl;

Point_

An instance of the class is interchangeable with C structures, CvPoint and CvPoint2D32f . There is also a cast operator to convert point coordinates to the specified type. The conversion from floating-point coordinates to integer coordinates is done by rounding.

For your convenience, the following type aliases are defined:

typedef Point_ Point2i;
typedef Point2i Point;
typedef Point_ Point2f;
typedef Point_ Point2d;

Point2f a(0.3f, 0.f), b(0.f, 0.4f);
Point pt = (a + b)*10.f;
cout << pt.x << ", " << pt.y << endl;

Point3_

Template class for 3D points specified by its coordinates , and . An instance of the class is interchangeable with the C structure CvPoint2D32f

typedef Point3_ Point3i;
typedef Point3_ Point3f;
typedef Point3_ Point3d;

Size_

Template class for specifying the size of an image or rectangle. The class includes two members called width andheight. The structure can be converted to and from the old OpenCV structures CvSize and CvSize2D32f .

typedef Size_ Size2i;
typedef Size2i Size;
typedef Size_ Size2f;

Rect_

Template class for 2D rectangles, described by the following parameters:

  • Coordinates of the top-left corner. This is a default interpretation of Rect_::x and Rect_::y in OpenCV. Though, in your algorithms you may count x and y from the bottom-left corner.
  • Rectangle width and height.

In addition to the class members, the following operations on rectangles are implemented:

  • (shifting a rectangle by a certain offset)
  • (expanding or shrinking a rectangle by a certain amount)
  • rect += point, rect -= point, rect += size, rect -= size (augmenting operations)
  • rect = rect1 & rect2 (rectangle intersection)
  • rect = rect1 | rect2 (minimum area rectangle containing rect2 and rect3 )
  • rect &= rect1, rect |= rect1 (and the corresponding augmenting operations)
  • rect == rect1, rect != rect1 (rectangle comparison)

RotatedRect

Each rectangle is specified by the center point (mass center), length of each side (represented by cv::Size2f structure) and the rotation angle in degrees.

Mat image(200, 200, CV_8UC3, Scalar(0));
RotatedRect rRect = RotatedRect(Point2f(100,100), Size2f(100,50), 30);

Point2f vertices[4];
rRect.points(vertices);
for (int i = 0; i < 4; i++)
line(image, vertices[i], vertices[(i+1)%4], Scalar(0,255,0));

Rect brect = rRect.boundingRect();
rectangle(image, brect, Scalar(255,0,0));

imshow("rectangles", image);
waitKey(0);

TermCriteria

The class defining termination criteria for iterative algorithms. You can initialize it by default constructor and then override any parameters, or the structure may be fully initialized using the advanced variant of the constructor.

终止条件  ——  迭代次数,阈值

TermCriteria::TermCriteria(int type, int maxCount, double epsilon)

Parameters:

  • type – The type of termination criteria: TermCriteria::COUNT, TermCriteria::EPS orTermCriteria::COUNT + TermCriteria::EPS.
  • maxCount – The maximum number of iterations or elements to compute.
  • epsilon – The desired accuracy or change in parameters at which the iterative algorithm stops.
  • criteria – Termination criteria in the deprecated CvTermCriteria format.

Matx

If you need a more flexible type, use Mat . The elements of the matrix M are accessible using the M(i,j) notation. Most of the common matrix operations (see also Matrix Expressions ) are available. To do an operation on Matx that is not implemented, you can easily convert the matrix to Mat and backwards.

template class Matx {…};

typedef Matx Matx12f;
typedef Matx Matx12d;

typedef Matx Matx16f;
typedef Matx Matx16d;

typedef Matx Matx21f;
typedef Matx Matx21d;

typedef Matx Matx61f;
typedef Matx Matx61d;

typedef Matx Matx22f;
typedef Matx Matx22d;

typedef Matx Matx66f;
typedef Matx Matx66d;

Matx33f m(1, 2, 3,
4, 5, 6,
7, 8, 9);
cout << sum(Mat(m*m.t())) << endl;

Vec

Template class for short numerical vectors, a partial case of Matx:

template class Vec : public Matx<_Tp, n, 1> {…};

typedef Vec Vec2b;
typedef Vec Vec3b;
typedef Vec Vec4b;

typedef Vec Vec2s;
typedef Vec Vec3s;
typedef Vec Vec4s;

typedef Vec Vec2i;
typedef Vec Vec3i;
typedef Vec Vec4i;

typedef Vec Vec2f;
typedef Vec Vec3f;
typedef Vec Vec4f;
typedef Vec Vec6f;

typedef Vec Vec2d;
typedef Vec Vec3d;
typedef Vec Vec4d;
typedef Vec Vec6d;

It is possible to convert Vec to/from Point_, Vec to/from Point3_ , and Vec to CvScalar or Scalar_. Useoperator[] to access the elements of Vec.

All the expected vector operations are also implemented:

  • v1 = v2 + v3
  • v1 = v2 - v3
  • v1 = v2 * scale
  • v1 = scale * v2
  • v1 = -v2
  • v1 += v2 and other augmenting operations
  • v1 == v2, v1 != v2
  • norm(v1) (euclidean norm)

Scalar_

Template class for a 4-element vector derived from Vec.

Being derived from Vec<_Tp, 4> , Scalar_ and Scalar can be used just as typical 4-element vectors. In addition, they can be converted to/from CvScalar . The type Scalar is widely used in OpenCV to pass pixel values.

四元组

Range

Template class specifying a continuous subsequence (slice) of a sequence.

序列片段

The class is used to specify a row or a column span in a matrix ( Mat ) and for many other purposes. Range(a,b) is basically the same as a:b in Matlab 。Such a half-opened interval is usually denoted as 区间左闭右开

The static method Range::all() returns a special variable that means “the whole sequence” or “the whole range”, just like ” : ”

Ptr

The Ptr<_Tp> class is a template class that wraps pointers of the corresponding type.

This class provides the following options:

The Ptr class treats the wrapped object as a black box. The reference counter is allocated and managed separately. The only thing the pointer class needs to know about the object is how to deallocate it. This knowledge is encapsulated in the Ptr::delete_obj() method that is called when the reference counter becomes 0. If the object is a C++ class instance, no additional coding is needed, because the default implementation of this method calls delete obj;. However, if the object is deallocated in a different way, the specialized method should be created. For example, if you want to wrap FILE, the delete_obj may be implemented as follows:

template<> inline void Ptr::delete_obj()
{
fclose(obj); // no need to clear the pointer afterwards,
// it is done externally.
}

// now use it:
Ptr f(fopen("myfile.txt", "r"));
if(f.empty())
throw …;
fprintf(f, ….);

// the file will be closed automatically by the Ptr destructor.

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章