- OpenCV By Example
- Prateek Joshi David Millán Escrivá Vinícius Godoy
- 508字
- 2025-02-20 19:20:49
Other basic object types
We have learned about the Mat and Vec3b classes, but we need to learn about other classes as well.
In this section, we will learn about the most basic object types required in most of the projects:
Vec
Scalar
Point
Size
Rect
RotatedRect
The vec object type
vec
is a template class that is used mainly for numerical vectors. We can define any type of vectors and a number of components:
Vec<double,19> myVector;
Or we can use any of the predefined types:
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;
Note
All the expected vector operations are also implemented, as follows:
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)
The Scalar object type
The Scalar
object type is a template class derived from Vec with four elements. The Scalar
type is widely used in OpenCV to pass and read pixel values.
To access the values of Vec
and Scalar
, we use the [] operator.
The Point object type
Another very common class template is Point
. This class defines a 2D point specified by its x
and y
coordinates.
Note
Like the Point object type, there is a Point3 template class for 3D point support.
Like the Vec
class, OpenCV defines the following Point
aliases for our convenience:
typedef Point_<int> Point2i; typedef Point2i Point; typedef Point_<float> Point2f; typedef Point_<double> Point2d;
Note
The following operators are defined for points:
pt1 = pt2 + pt3; pt1 = pt2 - pt3; pt1 = pt2 * a; pt1 = a * pt2; pt1 = pt2 / a; pt1 += pt2; pt1 -= pt2; pt1 *= a; pt1 /= a; double value = norm(pt); // L2 norm pt1 == pt2; pt1 != pt2;
The Size object type
Another template
class that is very important and used in OpenCV is the template
class used to specify the size of an image or rectangle, Size. This class adds two members: the width and height and a useful area()
function.
The Rect object type
Rect
is another important template class used to define 2D rectangles by the following parameters:
- The coordinates of the top-left corner
- The width and height of a rectangle
The Rect
template class can be used to define a ROI (region of interest) of an image.
RotatedRect object type
The last useful class is a particular rectangle called RotatedRect
. This class represents a rotated rectangle specified by a center point, the width and height of a rectangle, and the rotation angle in degrees:
RotatedRect(const Point2f& center, const Size2f& size, float angle);
An interesting function of this class is boundingBox
; this function returns a Rect
that contains the rotated rectangle:
