OpenCV 4 Computer Vision Application Programming Cookbook(Fourth Edition)
上QQ阅读APP看书,第一时间看更新

The cv::Mat_ template class

Using the at method of the cv::Mat class can sometimes be cumbersome because the returned type must be specified as a template argument in each call. In cases where the matrix type is known, it is possible to use the cv::Mat_ class, which is a template subclass of cv::Mat. This class defines a few extra methods but no new data attributes so that pointers or references to one class can be directly converted to the other class. Among the extra methods, there is operator(), which allows direct access to matrix elements. Therefore, if an image is a cv::Mat variable that corresponds to a uchar matrix, then you can write the following code:

  // use image with a Mat_ template 
  cv::Mat_<uchar> im2(image); 
   im2(50,100)= 0; // access to row 50 and column 100 

Since the type of the cv::Mat_ elements are declared when the variable is created, the operator() method knows at compile time which type is to be returned. Other than the fact that it is shorter to write, using the operator() method provides exactly the same result as the at method.