OpenCV Image Processing-Mat Object Common Method

Posted by pwnuspoints on Tue, 30 Jul 2019 06:28:17 +0200

OpenCV Image Processing-Mat Object Common Method

Preface

The environment used in this article is: Qt5.11 + OpenCV3.4.6
Environment Installation Reference Document: https://blog.csdn.net/z634863434/article/details/89950961

Mat object and IplImage object

Mat object is an image data structure introduced after OpenCV2.0. It is based on C++ object-oriented data structure. When it is generated, it allocates memory automatically and there is no memory leak. Header and data section.
IplImage object is an early data structure in C language style. It requires developers to allocate and manage memory themselves, which may lead to memory leak.

Common constructors for Mat objects

1.Mat()
Create a Mat object without doing anything at construction time

//Create a Mat object
Mat img;

2.Mat(int rows , int cols,int type)
Create a Mat object based on the number of rows, columns, and types specified by the user

//Create a 3*3 image with 8 bytes of uchar type per channel and 1 channel number of Mat objects
Mat img= Mat(3,3,CV_8UC1);

3.Mat(Size size , int type)
Create a Mat object based on the size and type specified by the user

//Read the local image and return it to Mat object src. According to the size of src, create a 3*3 image with 8 bytes of uchar type per channel and 1 channel number of Mat objects.
Mat src = imread("E:/OpenCV/OpenCVPicture/horse.png");
Mat img= Mat(src.size(),CV_8UC1);

4.Mat(int rows , int cols,int type,const Scalar &s)
Create a Mat object and initialize the value of each pixel according to the number of rows, columns and types specified by the user. The length of the vector must correspond to the number of channels.

//Create a 3*3 image with 8 bytes of uchar type per channel and 3 channels of Mat object, and set the initial color to (127,0,255)
Mat img= Mat(3,3,CV_8UC3,Scalar(127,0,255))

5.Mat(Size size , int type , const Scalar &s)
According to the size and type specified by the user, a Mat object is created and the value of each pixel is initialized. The length of the vector must correspond to the number of channels.

//Read the local image and return it to Mat object src. According to the size of src, create a 3*3 image. Each channel has 8 bytes of uchar type and 3 channels. Set the initial color to (127,0,255)
Mat src = imread("E:/OpenCV/OpenCVPicture/horse.png");
Mat img= Mat(src.size(),CV_8UC3,Scalar(127,0,255));

6. Array Construction
Constructing a Mat Object Using Matrix Data

//Composition 3*3
// 0  -1  0
// -1  5  -1
//  0  -1  0
//Mat image
Mat mat = (Mat_<double>(3,3)<<0,-1,0,-1,5,-1,0,-1,0);

Common Methods of Mat Objects

1.void copyTo(Mat mat)
Mat object deep copy

//Copy the image of src object to dst object
Mat src = imread("E:/OpenCV/OpenCVPicture/horse.png");
Mat dst;
src.copyTo(dst);

2.void converto(Mat dst,int type)
Mat Object Conversion Function

//Convert src to 8 byte length uchar type for each channel, and the number of channels is 3 Mat objects (which can be calculated with additional factors after the function)
Mat src = imread("E:/OpenCV/OpenCVPicture/horse.png");
Mat dst;
str.converto(dst,CV_8UC3);

3.Mat clone()
Mat object deep copy

//Copy the image of src object to dst object
Mat src = imread("E:/OpenCV/OpenCVPicture/horse.png");
Mat dst;
dst.clone(src);

4.int channels()
Number of channels to obtain the current image

//Get the number of channels for src
Mat src = imread("E:/OpenCV/OpenCVPicture/horse.png");
int channel = src.channels();

5.int depth()
Get the depth of the current image

//Get the depth of src
Mat src = imread("E:/OpenCV/OpenCVPicture/horse.png");
int channel = src.depth();

6.bool empty()
Judging whether the image is empty

Mat src = imread("E:/OpenCV/OpenCVPicture/horse.png");
Mat dst;
src.empty(); //false
dst.empty(); //true

7.uchar* ptr(i=0)
A pointer to the current image

//Getting the first line corresponding pointer of src image
Mat src = imread("E:/OpenCV/OpenCVPicture/horse.png");
const unchar* fisrtRow = src.ptr(0);

8.Mat zeros(int rows,int cols,int type)
Create Mat objects of all 0

//Create a 2*2 uchar type with 8 bytes per channel and 10 Mat objects with 1 channel number
Mat img = Mat::zeros(2,2,CV_8UC1)

9.Mat eye(int rows,int cols,int type)
Create a Mat object with a diagonal of 1

//Create a 2*2 uchar type with 8 bytes per channel and 1 Mat object line with 1 number of channels
//1  0
//0  1
Mat img = Mat::eye(2,2,CV_8UC1)

Topics: OpenCV Qt5 C