Mat - 基本的图像容器

2017-10-18  本文已影响0人  看风景的人_21744

目标

熟悉OPENCV如何存储及处理图像。

Mat

Mat由2部分组成:矩阵头和指向矩阵值的指针

Mat A, C;                          // creates just the header parts
A = imread(argv[1], IMREAD_COLOR); // here we'll know the method used (allocate matrix)
Mat B(A);                                 // Use the copy constructor
C = A;                                    // Assignment operator
Mat D (A, Rect(10, 10, 100, 100) ); // using a rectangle
Mat E = A(Range::all(), Range(1,3)); // using row and column boundaries

上述只是复制矩阵头和指向数据矩阵的指针。如果想要复制数据,可以

Mat F = A.clone();
Mat G;
A.copyTo(G);

Storing methods

灰度和彩色。他们的数据类型不同。

  • RGB is the most common as our eyes use something similar, however keep in mind that OpenCV standard display system composes colors using the BGR color space (a switch of the red and blue channel).
  • The HSV and HLS decompose colors into their hue, saturation and value/luminance components, which is a more natural way for us to describe colors. You might, for example, dismiss the last component, making your algorithm less sensible to the light conditions of the input image.
  • YCrCb is used by the popular JPEG image format.

Creating a Mat object explicitly

 Mat M(2,2, CV_8UC3, Scalar(0,0,255));
 cout << "M = " << endl << " " << M << endl << endl;

 int sz[3] = {2,2,2};
 Mat L(3,sz, CV_8UC(1), Scalar::all(0));

 M.create(4,4, CV_8UC(2));
 cout << "M = "<< endl << " "  << M << endl << endl;

 Mat E = Mat::eye(4, 4, CV_64F);
 Mat O = Mat::ones(2, 2, CV_32F);   
 Mat Z = Mat::zeros(3,3, CV_8UC1);

 Mat C = (Mat_<double>(3,3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);

 Mat R = Mat(3, 2, CV_8UC3);
 randu(R, Scalar::all(0), Scalar::all(255));  //随机初始值,只需要确定上下界

其他常见类型输出

Point2f P(5, 1);
Point3f P3f(2, 6, 7);
 
vector<float> v;
v.push_back( (float)CV_PI);   v.push_back(2);    v.push_back(3.01f);

vector<Point2f> vPoints(20);
    for (size_t i = 0; i < vPoints.size(); ++i)
        vPoints[i] = Point2f((float)(i * 5), (float)(i % 7));
上一篇下一篇

猜你喜欢

热点阅读