Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out what type to use for OpenCV .at function in C++?

Tags:

c++

opencv

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";
like image 611
Gnubie Avatar asked Jun 02 '15 12:06

Gnubie


People also ask

How do I know what type of CV mat I have?

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.

What is use of mat class in OpenCV?

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.

What is Vec3b OpenCV?

Vec3b is the abbreviation for "vector with 3 byte entries" Here those byte entries are unsigned char values to represent values between 0 .. 255.

What is a CV :: mat?

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.


1 Answers

Using data from

  • Types: http://ninghang.blogspot.de/2012/11/list-of-mat-type-in-opencv.html
  • Vectors: http://docs.opencv.org/modules/core/doc/basic_structures.html#vec

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...

like image 114
Micka Avatar answered Sep 21 '22 18:09

Micka