I used the following code to access to Mat’s elements and found this difficulty:

2 problems arise here:
- nzpxl Mat is hard-defined the type CV_16UC2 or it is passed in from this function’s caller
- nzpxl is accessed by at which is also a hard form
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?
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:
}
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:
and
Then if you want to change from short to int or float, you just have to modify those functions