Avoid confusion of matrix types

Hi,
as my code is growing (and the time since I wrote some of it increasing), I find myself more and more confronted to the problem of calling functions with the wrong matrix type (for example matrix of uchar instead of float), and spending quite some time to find the error.

The “problem” is that the standard cv::Mat says nothing about neither type, nor number of channels (well, working on grayscale images, that point isn’t an issue for me) nor matrix size (relevant only for small matrices with compile time size).
How do you handle this?
In Eigen, it is done through templates (each matrix is declared as Eigen::Matrix<type, nbr_rows, nbr_cols> (nbr_rows/cols can be set to dynamic, ie run-time).
For now, what I see for Opencv :

  • for small matrices only, using typedefs like Mat3f for a 3x3 matrix of floats
  • for the types, maybe using Mat_ : it seems however to be used very little in code I have seen so far : is it a good idea? Is it handled safely as return type for standard opencv functions?
  • using explicit names (ie post_fix all my matrix names with the type (and size if relevant))
  • using lots of comments and/or heavily documenting the classes
  • putting asserts on the types of all input arguments (and before calling any function)

How do you handle the different types of matrices in openCV without getting confused? What is considered best practice?

Thanks a lot in advance
Felix

For me, always use Mat_. It is convertible to Mat so you can use any OpenCV function waiting for a Mat. You are however not safe for conversions from Mat to Mat_ which can fail at runtime if the types mismatch.