Mat - 基本的图像容器

目标

熟悉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));
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容