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));
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容