Mat image container base

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_39504764/article/details/100550620

A: Use Mat structure

  • Mat categories:
  1. You need to manually open up space for
  2. You do not have to immediately release space when not needed
  • Mat image is a container class, the data structure consists of two parts:

    • 1- matrix head - that class Mat classes are instantiated class object opened up space inside to store data - is the message of this matrix, when we Mat object; this statement when the class object, it is only a creation Mat head of information and does not create a matrix body, that is to say, we do not have to open up the image to be stored corresponding space.
    • 2- Head matrix - comprising:
      1- matrix size : for example, the data members of the class class Mat rows, cols can specify the size of the image.
      2- storage method: Mat corresponding to various constructors
      3- storage address
      pointer pointing to the 4- and all the pixel values stored in a matrix
  • OpenCV use the counting mechanism : Mat so that each object has its own header, but share the same matrix. This is achieved by having the matrix pointer to the same address. The copy constructor header information and copy only the matrix pointer, without copying the matrix.

  • If you belong to more than Mat matrix object, then when no longer needed it, who is responsible for cleaning it?

The last use of its object. It is achieved through the counting mechanism. Whenever a copy Mat object header, the matrix will increase the number of citations. Conversely, when a head is released, the count is decremented; when the count value is 0, the matrix will be cleared.

  • Copy matrix itself (and not just the head matrix pointer information), may be used clone () or the copyTo () .
#include <opencv2/opencv.hpp>
using namespace cv;

int main()
{
    Mat A,C; //仅创建信息头部分
    A = imread("1.jpg",CV_LOAD_IMAGE_COLOR); //为矩阵开辟内存
    Mat B(C); //拷贝构造函数
    C = A; //赋值运算符
    Mat D(A,Rect(10,10,100,100)); //使用矩阵界定
    Mat E = A(Range::all(),Range(1,3)); //用行和列来界定
    Mat F = A.clone();
    Mat G;
    A.copyTo(G); //改变F或者G就不会影响Mat信息头所指向的矩阵
}

to sum up:

  1. OpenCV function of the output image memory allocation is done automatically (if it is not specified).
  2. C ++ using OpenCV's no need to consider the release of the memory problems interfaces.
  3. Assignment operator copies only the copy constructor and the message header.
  4. Using the function clone () or the copyTo () to copy an image matrix.

II: The method of storing pixel values

显式创建Mat对象的七种方法

1. Mat () Constructor
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main()
{
    Mat M(3,2,CV_8UC3,Scalar(0,0,255));
    /*
       3 行 2 列
       CV_[位数][带符号与否][类型前缀]C[通道数]
       CV_8UC3 : 表示使用8位的unsigned char型,每个像素由三个元素组成三通道
       Scalar : short类型的向量
     */
    cout << "M = " << endl << " " << M << endl << endl;
    return 0;
}

Here Insert Picture Description

2. initialized in C / C ++ constructor by
//创建一个超过二维的矩阵;指定维数,然后传递一个指向一个数组的指针,这个数组包含每个维度的尺寸。
 int sz[3] = {2,2,2};
 Mat M(3,sz,CV_8UC3,Scalar::all(0));
3. Create a header for the IplImage pointer already exists

For solutions directly IplImage type pointer initialization error of an object Mat: View

IplImage* img = cvLoadImage("1.jpg",1);
Mat mtx = cvarrToMat(img);
4. Use Create () function
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main()
{
    Mat M;
    M.create(4, 4, CV_8UC(2));
    cout << "M = " << endl << " " << M << endl << endl;
    return 0;
}

Here Insert Picture Description

The initialization method using Matlab formula
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main()
{
    Mat M = Mat::eye(4,4,CV_64F);
    cout << "Z = " << endl << " " << Z << endl << endl;
    
    Mat O = Mat::ones(2, 2, CV_32F);
    cout << "O = " << endl << " " << O << endl << endl;
    
    Mat Z = Mat::zeros(3, 3, CV_8UC1);
    cout << "Z = " << endl << " " << Z << endl << endl;
    return 0;
}

Here Insert Picture Description

6. submatrix comma separated initialization function formula
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main()
{
    Mat C = (Mat_<double>(3,3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
    cout << "C = " << endl << " " << C << endl << endl;
    return 0;
}

Here Insert Picture Description

7. Create a new header for an object that already exists
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main()
{
    Mat C = (Mat_<double>(3,3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
    cout << "C = " << endl << " " << C << endl << endl;
    Mat RowClone = C.row(1).clone();
    cout << "RowClone = " << endl << " " << RowClone << endl << endl;
    return 0;
}

Here Insert Picture Description

Three: Method formatted output of OpenCV

1. OpenCV default style
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main()
{
    Mat r = Mat(10, 3, CV_8UC3);
    randu(r, Scalar::all(0), Scalar::all(255));
    cout << "r(openCV默认风格) = "<< r <<";" <<endl <<endl;

    return 0;
}

Here Insert Picture Description

2. Python style
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main()
{
    Mat r = Mat(10, 3, CV_8UC3);
    randu(r, Scalar::all(0), Scalar::all(255));
    cout << "r(Python风格) = "<< format(r,Formatter::FMT_PYTHON) <<";" <<endl <<endl;

    return 0;
}

Here Insert Picture Description

3. Comma Separated style
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main()
{
    Mat r = Mat(10, 3, CV_8UC3);
    randu(r, Scalar::all(0), Scalar::all(255));
    cout << "r(逗号分隔风格) = "<< format(r,Formatter::FMT_CSV) <<";" <<endl <<endl;

    return 0;
}

Here Insert Picture Description

4. Numpy style
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main()
{
    Mat r = Mat(10, 3, CV_8UC3);
    randu(r, Scalar::all(0), Scalar::all(255));
    cout << "r(Numpy风格) = "<< format(r,Formatter::FMT_NUMPY) <<";" <<endl <<endl;

    return 0;
}

Here Insert Picture Description

5. C language style
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main()
{
    Mat r = Mat(10, 3, CV_8UC3);
    randu(r, Scalar::all(0), Scalar::all(255));
    cout << "r(C语言风格) = "<< format(r,Formatter::FMT_C) <<";" <<endl <<endl;

    return 0;
}

Here Insert Picture Description

4: To other structures commonly used OpenCV

1. Definitions and output two-dimensional point
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main()
{
    Point2f p(6.2,2);
    cout << p <<endl;
    return 0;
}

//[6.2, 2]
2. Define and output based on the Mat std :: vector
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main()
{
    vector<float> v;
    v.push_back(1.2);
    v.push_back(2.1);
    v.push_back(3);
    cout << "【基于Mat的vector】shortvec = \n" << Mat(v) << endl ;
    return 0;
}

Here Insert Picture Description

3. Define std :: vector and output points
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main()
{
    vector<Point2f> points(20);
    for(int i = 0; i < points.size();i++)
    {
        points[i] = Point2f(i*5, i%7);
    }
    cout << "【二维点向量】points = \n" << points <<endl;
    return 0;
}

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/qq_39504764/article/details/100550620