Is there a simple reliable way to find out what equivalent type I should use for the .at function for a Mat of a given CV type?
For example, how can I tell that the blanks should be filled with ushort
, float
and Vec3b
?
Mat mat1(1, 2, CV_16UC1, 12345);
std::cout << mat1.at<___>(0, 1) << "\n";
Mat mat2(1, 2, CV_32FC1, 67.89);
std::cout << mat2.at<___>(0, 1) << "\n";
Mat mat3(1, 2, CV_8UC3, Scalar(65, 66, 67));
std::cout << mat3.at<___>(0, 1)[2] << "\n";
We can check the Data Type of a cv::Mat using “type()” method. This is a method you can use for checking the type of an cv::Mat.
The Mat class of OpenCV library is used to store the values of an image. It represents an n-dimensional array and is used to store image data of grayscale or color images, voxel volumes, vector fields, point clouds, tensors, histograms, etc.
Vec3b is the abbreviation for "vector with 3 byte entries" Here those byte entries are unsigned char values to represent values between 0 .. 255.
In OpenCV the main matrix class is called Mat and is contained in the OpenCV-namespace cv. This matrix is not templated but nevertheless can contain different data types. These are indicated by a certain type-number. Additionally, OpenCV provides a templated class called Mat_, which is derived from Mat.
Using data from
I constructed the following table:
C1 C2 C3 C4 C6 (not sure whether C6 subtype exists as a macro)
CV_8U uchar Vec2b Vec3b Vec4b
CV_8S char -
CV_16U ushort -
CV_16S short Vec2s Vec3s Vec4s
CV_32S int Vec2i Vec3i Vec4i
CV_32F float Vec2f Vec3f Vec4f Vec6f
CV_64F double Vec2d Vec3d Vec4d Vec6d
with uchar == unsigned char
and ushort == unsigned short
for missing types you could create your own typedef if you want to:
typedef Vec<ushort, 2> Vec2us;
or you just access it with am type of same size (Vec2s
) and convert it afterwards
But in the end I think understanding what the format means (32 bit floating point number with 3 channels = 3 float values per matrix element) if much better than looking at a table...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With