Q:

How to allocate the elements of a matrix in c++ opencv?

I have always used the C API and am trying to convert a simple project to the C++ API to get a handle on it, but I can't seem to figure out how to allocate the elements of a matrix in C++.  For example, I have the following array:
double rotation[] = { 0, 1, 0,
                     -1, 0, 0,
                      0, 0, 1 };

I have a 3x3 Mat, allocated with Mat *rotation_vector = new Mat(3,3, CV_64FC1);
In C, it would simply be cvInitMatHeader(rotation_vector, 3, 3, CV_64FC1, rotation);.  How is this done in C++?

A:

You should use some variant of the cv::Mat object.
For small matrices where the size is known and fixed, you can use the cv::Matx object:
cv::Matx33d rotation(  0.0, 1.0, 0.0,
                      -1.0, 0.0, 0.0,
                       0.0, 0.0, 1.0 );

For dynamically sized, arrays, you use the cv::Mat object
cv::Mat aBigMatrix( 53, 71, cv::CV_64FC1, cv::Scalar::all( 0.0 ) );

This would create a 2d-matrix with width of 53, height of 71, each element is a 64 bit floating point single-channel value ( basically a double ), and all of the elements will be set to 0.
Alternatively, you may use the cv::Mat_ template class, and the benefit is a cleaner initialization ( among other things ):
cv::Mat_<double> anotherBigMatrix( 53, 71, 0.0 );

This would create an equivalent matrix to the one described above.
Finally, you can use the template matrix class with an initialization list similar to the way you would do it in regular C:
There are many trade-offs to using the different classes, so you should make sure that you read the OpenCV documentation so that you pick the right one

