• 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


cv::Mat image1 = cv::imread("../data/1.png", cv::IMREAD_COLOR);
cv::Mat image2 = cv::imread("../data/2.png", cv::IMREAD_COLOR);

cv::Ptr<cv::ORB> orb = cv::ORB::create();
std::vector<cv::KeyPoint> keypoints1, keypoints2;
orb->detect(image1, keypoints1);
orb->detect(image2, keypoints2);

cv::Mat descriptors1, descriptors2;
orb->compute(image1, keypoints1, descriptors1);
orb->compute(image2, keypoints2, descriptors2);

std::vector<cv::DMatch> matches;
cv::Ptr<cv::DescriptorMatcher> matcher = cv::DescriptorMatcher::create("BruteForce-Hamming");
matcher->match(descriptors1, descriptors2, matches, cv::Mat());

cv::Mat output_image;
cv::drawMatches(image1, keypoints1, image2, keypoints2, matches, output_image);
cv::namedWindow("Display window", cv::WINDOW_AUTOSIZE);
cv::imshow("Display window", output_image);


// XML/YAML File Open and Close
string filename = "I.xml";
FileStorage fs(filename, FileStorage::WRITE);, 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
// ...

Line Features

Last updated