Mat class template foundation (instance) under OpenCV

Table of contents

1. Knowledge points related to Mat class templates

2. Create cv::Mat class object

3. Basic operation of cv::Mat class object

(1) Read image, display image, store image


CodeBlocks under Windows configures the Opencv environment

OpenCV data structure

1. Knowledge points related to Mat class templates

  • The Mat class template can be used to save other n-dimensional arrays, mainly used to save image data. By default, when defining a cv::Mat class object, its size is 0 x 0 (you can also provide appropriate parameters to the constructor) like:
    • cv:Mat img(240,320,CV_8U,cv::Scalar(100))
    • Among them, CV_8U means that each pixel in the image is represented by 1 byte (8bit), and U means an unsigned integer.
    • For color images, use 3 channels (channels), where CV_8UC3.
    • 16-bit or 32-bit integer CV_8SC3, 32 or 64-bit floating-point CV_32F can also be used.
  • After the cv::Mat class object leaves the scope, it will be automatically destroyed, so memory leaks are avoided.
  • The cv::Mat class provides a reference counter and shallow copy mechanism to copy one image to another image, instead of copying its data, the two images will share the same data area, especially when the parameters are passed by value and functions When an image is returned.
  • A reference counter ensures that the data is not actually destroyed until all references are destroyed.

2. Create cv::Mat class object

cv::Mat class object
grammar describe
double m[2][2] = { {1.0,2.0},{3.0,4.0}};Mat M(2,2,CV_32F,m) Create a 2 x 2-dimensional matrix M from a 2-dimensional array m
Mat M(100,100,CV_32FC2,Scalar(1,3)) Create a 100 x 100 2-channel matrix, fill the first channel with 1's and the second channel with 3's
M.create(300,300,CV_8UC(15)) Create a 300 x 300 matrix of 15 channels
int sizes[3] = {7,8,9};Mat M(3,sizes,CV_8U,Scalar::all(0)) Create multidimensional data with a dimension of 3, and the scale of each dimension is specified by the sizes array
Mat M = Mat::eye(7,7,CV_32F) Create a 7 x 7 identity matrix where each element is a 32-bit float
Mat M = Mat:zeros(7,7,CV_64F) Create a 7 x 7 matrix of zeros, each element is a 64-bit float
Mat M = Mat:ones(7,7,CV_64F) Create a 7 x 7 matrix of all 1s, each element is a 64-bit floating point number

Access the elements of cv::Mat class object
grammar describe
M.at<double>(i,j) Access the element in row i, column j, the element type is double, note that the count starts from 0
M.row(1) access line 1
M.col(3) access column 3
M.rowRange(1,4) access row 1 to row 4
M.colRange(1,4) Access columns 1 to 4
M.rowRange(2,5).colRange(1,3) Access row 2 to row 5, column 1 to column 3
M.diag() Visit Diagonal

Expressions for objects of class cv::Mat
grammar describe
Mat M2 = M1.clone() Create copy data M2 of M1
Mat M2;M1.copyTo(M2) Copy data from M1 to M2
Mat M1 = Mat::zeros(9,3,CV_32FC3);Mat M2 = M1.reshape(0,3) M2 is generated after adjusting the data of M1
Mat M2 = M1.t() Transpose of M1
Mat M2 = M1.inv() Inverse of M1
Mat M3 = M1 * M2 M1 times M2
Mat M2 = M1 + s M1 plus a scalar

3. Basic operation of cv::Mat class object

(1) Read image, display image, store image

#include <iostream>
#include<opencv2/core.hpp>
#include<opencv2/highgui.hpp>

using namespace std;
using namespace cv;

int main()
{
    //创建两个个Mat对象
    Mat image,result;
    //显示矩阵的大小
    cout<<"image.width = "<<image.size().width<<" image.height = "<<image.size().height<<endl;

    //读取图片
    image = imread("../images/GuiZhou.jpg");

    //判断图像是否读取成功
    if(!image.data){
        cout<<"read image failed!"<<endl;
    }

    //显示原始图像

    namedWindow("Original image!");
    imshow("Original image!",image);

    //对图像进行水平翻转,并且将翻转之后的结果存放在result矩阵中
    flip(image,result,1);//1-表示水平翻转,0-表示垂直翻转,负数表示上下左右同时翻转

    //显示翻转之后的图像

    namedWindow("flip image!");
    imshow("flip image!",image);

    //将翻转之后的图像保存下来
    imwrite("flip.png",result);

    waitKey(0);
    return 0;
}

The original image

Image after flipping

 

reference book:

"Digital Image Processing"

Guess you like

Origin blog.csdn.net/Keep_Trying_Go/article/details/130440286