Basic operations of OpenCV Mat

Mat matrix creation

Method 1. Use Mat() constructor

cv::Mat M1(2,2,CV_8UC3,Scalar(0,0,255))

The meaning of this function is to create a Mat named M1. The size of the Mat is 2,2, and the type
is CV_8UC3, which is an 8-bit uchar type. The Mat The number of channels is 3. Each element of this mat
contains 3 channels or 3 values. Then assign a value to each element with 0,0,255.
The value of the 8-bit uchar type here is 0~255. In fact, if a Mat is used to represent an RGB image
, it should be declared as CV_8UC3< /span>

Mat type can define various types. You can press the F12 shortcut key to view the definition in the header file. The definition is as follows: CV_(digits) + (data type) + (number of channels)
For example, CV_32FC1 represents 32-bit float data and has the following types

  • CV_8UC1 CV_8UC2 CV_8UC3 CV_8UC4 CV_8SC1 CV_8SC2 CV_8SC3 CV_8SC4
  • CV_16UC1 CV_16UC2 CV_16UC3 CV_16UC4 CV_16SC1 CV_16SC2 CV_16SC3 CV_16SC4
  • CV_32SC1 CV_32SC2 CV_32SC3 CV_32SC4 CV_32FC1 CV_32FC2 CV_32FC3 CV_32FC4
  • CV_64FC1 CV_64FC2 CV_64FC3 CV_64FC4

Method 2: Use the create() function, the code is as follows

cv::Mat M3;
M3.create(3,4,CV_8UC3);

It means first declaring a mat type, named M3, whose size is 3 rows and 4 columns.

Method 3: Initialize using built-in functions

  • All initialized to 0
Mat initZero=Mat::zeros(2,2,CV_32F);
  • All initialized to 1
Mat initOne= Mat::ones(2,2,CV_32F);
  • Fully initialized to diagonal matrix
Mat initEye=Mat::eye(2,2,CV_32F);

Mat matrix copy

1. Shallow copy

cv::Mat srcM(2,2,CV_8UC3,Scalar(0,0,255))
cv::Mat dstM;
dstM=srcM;

means first declaring a mat named srcM and initializing it. Then declare a mat named dstM and copy srcM to dstM through "=". The matrix generated in this way just generates a new matrix header, and the data of dstM still points to the data of the matrix srcM, similar to the shallow copy in C++.
:

2. Deep copy

cv::Mat srcM(2,2,CV_8UC3,Scalar(0,0,255))
cv::Mat dstM;
srcM.copyTo(dstM);

Through the copyTo function, deep copying can be achieved. That is to say, dstM is a brand new matrix, and its address in memory is different from srcM. In addition, the copyTo function can also add mask parameters

Mat matrix copy traversal

When we want to read or modify any content of Mat, we can access Mat in the following ways.

1. Using pointers.ptr

int height= image.rows; //行数
Int width = image.cols * image.channels(); //每行元素的总元素数量
for (int j=0; j<height; j++) 
{
//定义指针data,其值为image的第j行的头地址
uchar* data= image.ptr<uchar>(j);
for (int i=0; i<width; i++) 
{
//----开始处理每个像素,每个元素的值减少到1/2-----
data[i]= data[i]/2;
//-------------结束像素处理------------------------
} //单行处理结束
}

2.Use .at

Loop through 3-channel RGB image
int height= image.rows; //行数
Int width = image.cols ; //每行元素的总元素数量
for (int j=0; j<height; j++) 
{
for (int i=0; i<width; i++) 
{
//----开始处理每个像素,每个元素的值减少到1/2-----
image.at<Vec3b>(j,i)[0]= image.at<Vec3b>(j,i)[0]/div*div + div/2;
image.at<Vec3b>(j,i)[1]= image.at<Vec3b>(j,i)[1]/div*div + div/2;
image.at<Vec3b>(j,i)[2]= image.at<Vec3b>(j,i)[2]/div*div + div/2;
//-------------结束像素处理------------------------
} //单行处理结束
}
Traverse a single channel grayscale image
int height= image.rows; //行数
Int width = image.cols * image.channels(); //每行元素的总元素数量
for (int j=0; j<height; j++) 
{
for (int i=0; i<width; i++) 
{
//----开始处理每个像素,每个元素的值减少到1/2-----
image.at<uchar>(j,i)= image.at<uchar>(j,i)/2;
//-------------结束像素处理------------------------
} //单行处理结束
}

3. Use iterators

Mat M = Mat(100, 150, CV_8UC3);
cout << "rows=" << M.rows << ",cols=" << M.cols << endl;

Mat_<Vec3b>::iterator it = M.begin<Vec3b>();//初始位置的迭代器
Mat_<Vec3b>::iterator itend = M.end<Vec3b>();//终止位置的迭代器
for (; it != itend; it++)
{
    //处理BGR三个通道
    (*it)[0] = 182;//B
    (*it)[1] = 194;//G
    (*it)[2] = 154;//R
}

Mat mathematical operations

Mat addition, subtraction, multiplication and division

Mat matrices of the same type can directly use operators for addition, subtraction, multiplication and division, in which the elements at the corresponding positions of the code matrix are added, subtracted, multiplied and divided.

#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
using namespace std;
using namespace cv;
int main() {
    Mat a=(Mat_<float>(2,2)<<1,1,1,1);
    Mat b=(Mat_<float>(2,2)<<1,2,3,4);
    Mat show;
    //1.add 
    //show=r.rows(0);
    show=a+b;
    cout<<show<<endl;
    //2.subtract
    show=a-b;
    cout<<show<<endl;
    //3., multiply
    show=a*b;
    cout<<show<<endl;
    //4.divide
    show=a/b;
    cout<<show<<endl;
    return 0;
}
结果如下:
[2, 3;
  4, 5]
[0, -1;
  -2, -3]
[4, 6;
  4, 6]
[1, 0.5;
  0.33333334, 0.25]

Mat bit operations

The bitwise operations of mat include AND or NOT, XOR operations, etc.

  • Bitwise AND: bitwise_and
  • Bitwise OR: bitwise_or
  • Bitwise XOR: bitwise_xor
  • Bitwise not: bitwise_not

Mat matrix inversion and transposition

  • Use inv() to find the inversion
  • Use t() to find the transpose
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main() {
    Mat r=(Mat_<float>(2,2)<<1,1,0,2);
    cout<<r<<endl;
    cout<<r.inv()<<endl;
    cout<<r.t()<<endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/EBDSoftware/article/details/128784515