Reputation: 2079
I used the following code to access to Mat's elements and found this difficulty:
2 problems arise here:
So, when there isn't a good correspondence between the two, the function crashes. I hardly know which are even compatible. For example, does CV_16UC2 correspond with Vec2i ??
Question 1: Can this only be hard coded?
Question 2: How to know the compatible types?
Edit 01:
Sorry for lack of information! About Mat_ template, I've seen posts using Mat_ to define their own type for clarity and avoid using OpenCV
default types. Is this one answer to my 2nd question?
Upvotes: 1
Views: 1521
Reputation: 3988
There is a moment where you have to decide what kind of data you are storing in your matrix, and this has to be "hard-coded" somewhere.
If you decide that your data matrix is of type CV_16UC2, then you should check in all functions that access pixels in this matrix (e.g. using at<>) that the matrix is in the expected form:
void someFunction(cv::Mat &myMatrixOf16UC2) {
// using asserts
assert(myMatrixOf16UC2.type() == CV_16UC2);
// or using exceptions
if (myMatrixOf16UC2.type() != CV_16UC2)
throw someException;
// do the job
}
By the way, int are 32 bits, so Vec2i should deal with matrices allocated with CV_32SC2. CV_16UC2 -> cv::Vec2s, or even cv::Vec for which there is no predefined typedefs in OpenCV.
One useful pratice is to define somewhere for your program the data you are using with a typedef and a corresponding type() function, e.g. in a header:
typedef cv::Vec<unsigned short, 2> pixel_t;
and
int getOpenCVTypeForMyPixelType() { return CV_16UC2; }
Then if you want to change from short to int or float, you just have to modify those functions
Upvotes: 1