Opencv (2) -- Basic structures (1)

Source: Internet
Author: User
Datatype

A primitive opencv data type is oneUnsigned 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.

Type naming format:

CV _ <bit-depth> {u | S | f} C (<number_of_channels>)

A universal opencv structure that is able to store a single instance of such a primitive data type isVEC. Multiple instances of such a type can be stored inSTD: Vector,Mat,Mat _,Sparsemat,Sparsemat _, Or any other container that is able to storeVECInstances.

TheDatatypeClass 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 matrixMat A(30, 40, DataType<float>::type);Mat B = Mat_<std::complex<double> >(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,CvpointAndCvpoint2d32f. 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_<int> Point2i;typedef Point2i Point;typedef Point_<float> Point2f;typedef Point_<double> 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 structureCvpoint2d32f

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

Template Class for specifying the size of an image or rectangle. The class between des two members calledWidthAndHeight. The structure can be converted to and from the old opencv StructuresCvsizeAndCvsize2d32f.

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

 

Rect _

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

  • Coordinates of the top-left corner. This is a default interpretationRect _: xAndRect _: YIn opencv. Though, in your algorithms you may countXAndYFrom 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 containingRect2AndRect3)
  • 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.

Termination condition -- number of iterations, Threshold Value

Termcriteria ::Termcriteria(IntType, IntMaxcount, DoubleEpsilon)

Parameters:

  • Type-The type of termination criteria:Termcriteria: Count,Termcriteria: EPSOrTermcriteria: 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 deprecatedCvtermcriteriaFormat.

 

Matx

If you need a more flexible type, useMat. The elements of the matrixMAre accessible usingM (I, j)Notation. Most of the common matrix operations (see alsoMatrix expressions) Are available. To do an operation onMatxThat is not implemented, you can easily convert the MatrixMatAnd backwards.

template<typename _Tp, int m, int n> class Matx {...};typedef Matx<float, 1, 2> Matx12f;typedef Matx<double, 1, 2> Matx12d;...typedef Matx<float, 1, 6> Matx16f;typedef Matx<double, 1, 6> Matx16d;typedef Matx<float, 2, 1> Matx21f;typedef Matx<double, 2, 1> Matx21d;...typedef Matx<float, 6, 1> Matx61f;typedef Matx<double, 6, 1> Matx61d;typedef Matx<float, 2, 2> Matx22f;typedef Matx<double, 2, 2> Matx22d;...typedef Matx<float, 6, 6> Matx66f;typedef Matx<double, 6, 6> 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 caseMatx:

template<typename _Tp, int n> class Vec : public Matx<_Tp, n, 1> {...};typedef Vec<uchar, 2> Vec2b;typedef Vec<uchar, 3> Vec3b;typedef Vec<uchar, 4> Vec4b;typedef Vec<short, 2> Vec2s;typedef Vec<short, 3> Vec3s;typedef Vec<short, 4> Vec4s;typedef Vec<int, 2> Vec2i;typedef Vec<int, 3> Vec3i;typedef Vec<int, 4> Vec4i;typedef Vec<float, 2> Vec2f;typedef Vec<float, 3> Vec3f;typedef Vec<float, 4> Vec4f;typedef Vec<float, 6> Vec6f;typedef Vec<double, 2> Vec2d;typedef Vec<double, 3> Vec3d;typedef Vec<double, 4> Vec4d;typedef Vec<double, 6> Vec6d;

It is possible to convertVEC <t, 2>To/fromPoint _,VEC <t, 3>To/fromPoint3 _, AndVEC <t, 4>ToCvscalarOrScalar _. UseOPERATOR []To access the elementsVEC.

All the expected vector operations are also implemented:

  • V1 = V2 + v3
  • V1 = v2-V3
  • V1 = V2 * Scale
  • V1 = scale * v2
  • V1 =-V2
  • V1 + = v2And other augmenting operations
  • V1 = V2, V1! = V2
  • Norm (V1)(Euclidean norm)

 

Scalar _

Template class for a 4-element Vector Derived from Vec.

Being derived fromVEC <_ TP, 4>,Scalar _AndScalarCan be used just as typical 4-element vectors. In addition, they can be converted to/fromCvscalar. The typeScalarIs widely used in opencv to pass pixel values.

Tuples

Range

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

Sequential fragment

The class is used to specify a row or a column span in a matrix (Mat) And for processing other purposes.Range (A, B)Is basically the sameA: BIn Matlab. Such a half-opened interval is usually denoted as range left closed right open

The Static MethodRange: All ()Returns a special variable that means "the whole sequence" or "the whole range", just like":"

PTR

ThePTR <_ TP>Class is a template class that wraps pointers of the corresponding type.

This class provides the following options:

ThePTRClass 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 inPTR: 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 CILSDelete OBJ;. However, if the object is deallocated in a different way, the specialized method shoshould be created. For example, if you want to wrapFile,Delete_objMay be implemented as follows:

template<> inline void Ptr<FILE>::delete_obj(){    fclose(obj); // no need to clear the pointer afterwards,                 // it is done externally.}...// now use it:Ptr<FILE> f(fopen("myfile.txt", "r"));if(f.empty())    throw ...;fprintf(f, ....);...// the file will be closed automatically by the Ptr<FILE> destructor.

Opencv (2) -- Basic structures (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.