With C++ interface in OpenCV 2.0, Mat supports automatic memory management.
Mat has two data parts: the matrix header (constant size) and a pointer to the matrix containing the pixel values (varying size).
For computational efficiency, OpenCV uses a reference counting system.
Each Mat object has its own header, however the matrix may be shared.
The copy operators will only copy the headers and the pointer to the large matrix, not the data itself.
Making a modification using any of them will affect all the other ones as well.
To copy the matrix itself, use cv::Mat::clone() and cv::Mat::copyTo()
Color space options
RGB is the most common one (however OpenCV uses BGR instead)
The HSV and HLS decompose colors into their hue, saturation and value/luminance components (more natural way for us to describe colors)
YCrCb is used by the popular JPEG image format
CIE L*a*b* is a perceptually uniform color space
// cv::Mat::Mat Constructor
Mat M(2, 2, CV_8UC3, Scalar(0,0,255));
// Use C/C++ arrays and initialize via constructor
int sz[3] = {2,2,2};
Mat L(3, sz, CV_8UC(1), Scalar::all(0));
// MATLAB style initializer
Mat E = Mat::eye(4, 4, CV_64F);
Mat O = Mat::ones(2, 2, CV_32F);
Mat Z = Mat::zeros(3, 3, CV_8UC1);
// For small matrices you may use comma separated initializers
Mat C = (Mat_<double>(3,3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
// Create a new header for an existing Mat object
Mat RowClone = C.row(1).clone();
// You can fill out a matrix with random values
Mat R = Mat(3, 2, CV_8UC3);
randu(R, Scalar::all(0), Scalar::all(255));
// Other common OpenCV data structures
Point2f P(5, 1);
Point3f P3f(2, 6, 7);
std::vector<float> v(20); Mat(v);
std::vector<Point2f> vPoints(20);
// Typedefs in CV Core module
// typedef Mat_< Vec3f > cv::Mat3f
// typedef Matx< double, 3, 3 > cv::Matx33d
// XML/YAML File Open and Close
string filename = "I.xml";
FileStorage fs(filename, FileStorage::WRITE);
fs.open(filename, FileStorage::READ);
// Input and Output of text and numbers
fs << "iterationNr" << 100;
int itNr;
fs["iterationNr"] >> itNr;
itNr = (int) fs["iterationNr"];
// Input/Output of OpenCV Data structures
Mat R = Mat_<uchar >::eye (3, 3),
T = Mat_<double>::zeros(3, 1);
fs << "R" << R; // Write cv::Mat
fs << "T" << T;
fs["R"] >> R; // Read cv::Mat
fs["T"] >> T;
// Input/Output of vectors (arrays) and associative maps
// use cv::FileNode and cv::FileNodeIterator data structures
// Read and write your own data structures
// ...
provides the line descriptors as one of the contrib modules. Not sure if this implements the Line Band Descriptors (LBD) that is commonly used for line matching.