Common array operations of opencv on c + +

1. Image reading and Mat initialization

-1 represents the original format (BGR for color images), 0 represents the grayscale image, and 1 represents the BGR image

In addition to reading images, you can also use Mat::zeros,   Mat::ones,    Mat::eye and other functions initialize mat

string path= "image.jpg";
cv::Mat m= imread(path, -1);

Mat::zeros(m.rows, m.cols, CV_8UC1);

2. Type conversion

Only the data type of mat will be changed, and the value of mat will not be changed. Mat loaded by imread is CV_8UC3 format, 8 represents 8 bits (in addition to 16 bits, 32 bits and 64 bits), U represents unsigned integer (in addition to short integer S and floating point F), C3 represents 3 channels (in addition, there are one channel C1, two channel C2, four channel C4 and N channel C(n))

You can get the type information through mat.type()

m.convertTo(m, CV_32FC3);
int dtype = m.type();//dtype==CV_32FC3

3. Color space conversion

There are HSV, LAB, XYZ, Luv and other color spaces that can be converted.

After color space conversion, please check whether the value range of the target mat corresponds to the theoretical value range. If not, convert the data type of the original mat, such as CV_32FC3 [the maximum value of BRG image under this type should be 1], CV_8UC1 [the maximum value of BRG image under this type should be 255]

cv::Mat hsv;
cvtColor(m/255, hsv, cv::COLOR_BGR2HSV);

4. Mathematical operation

Addition, subtraction, multiplication and division can be performed between cv::Mat, and its operation method is no different from that of normal variables

It is better to convert mat type to 64 bit float during operation, so as to avoid unnecessary trouble caused by operation result overflow. For example: in CV_ 1-255 = 254 under the type of 8uc1 (subtraction only represents the distance of the value)

Mat mat9 = Mat::ones(9, 9, CV_16FC1)*9;
Mat mat2 = Mat::ones(9, 9, CV_16FC1)*2;
Mat result_18 = mat2.mul(mat9);//Multiply pixel by pixel to get a matrix
Mat resutl_7=mat9-mat2;
Mat resutl_11=mat9+mat2;
Mat resutl_4_5=mat9/mat2;

5. Separation and merging of Mat channels

Multi channel Mat is separated into vector < Mat > array, and vector < Mat > array can be combined into multi-channel Mat

//Divide the lab three channel mat into three single channel vector s
vector<Mat> mv;
cv::split(lab, mv);
mv[0] = (mv[0] -50)/2;//Move the coordinate origin of LAB color space to the middle of the ball model

//Merge multiple mats in the vector into one multi-channel mat
cv::merge(mv, lab);

6. Traversal and value assignment

The value assignment needs to distinguish between single channel mat and multi-channel mat. Secondly, pay attention to whether the element type of mat is floating point number or integer

Mat mat = Mat::ones(9, 9, CV_8UC1)*9; //Single channel uchar
Mat mat3 = Mat::ones(9, 9, CV_16FC3)*9;//Multichannel float
for (i = 0;i < mat.rows;i++) {
    for (j = 0;j < mat.cols;j++) {
        int kk= mat.at<uchar>(i,j);//Single channel uchar
        flaot f1=dis.at<flaot>(i, j)[0];//3-channel float
        flaot f2=dis.at<flaot>(i, j)[1];//3-channel float
        flaot f3=dis.at<flaot>(i, j)[2];//3-channel float
     }
}

7. Interception of value range

The interception of the value range under opencv needs to be realized through the threshold function. Through the threshold function, the part greater than the threshold can be set to 0 or the part less than the threshold can be set to 0

As shown in the following code, the value range of [0.05,0.3] in im is extracted through combination operation, and the value outside the target value range is set to 0

//Set the part greater than 0.3 to 0
Mat new_mat= cut_value(im, .3, 1);
//Set the part less than 0.05 to 0
new_mat = cut_value(dis_lab, .05, 0);

//int type:0 gets the part greater than the threshold, 1 gets the part less than the threshold
//This function is only applicable to float mat with a value range of 0,1, because the threshold function only supports CVs_ 8uc1, so the value range is scaled accordingly.
//For mat without value range, you need to adjust the parameters by yourself
Mat cut_value(Mat im,float thres_,int type) {
    Mat result,tmp;
    thres_ = thres_ * 255;
    tmp = im * 255;
    tmp.convertTo(tmp, CV_8UC1);
    if (type == 0) {
        threshold(tmp, result, thres_, 255,THRESH_BINARY);//Greater than the threshold returns a maximum value of 255 and less than 0
    }
    else {
        threshold(tmp, result, thres_, 255, THRESH_BINARY_INV);//Less than the threshold returns a maximum value of 255 less than 0
    }
    result.convertTo(result, CV_32FC1);
    result = result / 255;
    return result;
}
//Set the part greater than 0.3 to 0
Mat new_mat= cut_value(im, .3, 1);
//Set the part less than 0.05 to 0
new_mat = cut_value(dis_lab, .05, 0);

8. Calculation of maximum, minimum, mean, sum and other statistical values

Where the maximum and minimum values are for all channels. The scope of sum and mean functions is calculated as a single channel, so there are multiple results for multiple channels

double minv = 0.0, maxv = 0.0;
double* minp = &minv;
double* maxp = &maxv;
cv::minMaxIdx(mat, minp, maxp);
//Suppose mat is three channels
printf_s("channel 1  sum:%f  mean:%f ,channel 2  sum:%f  mean:%f ,channel 3  sum:%f  mean:%f ,min:%f  max:%f\n", sum(mat)[0], mean(mat)[0],sum(mat)[1], mean(mat)[1],sum(mat)[2], mean(mat)[2], minv, maxv);

Tags: OpenCV Algorithm Computer Vision image processing

Posted on Wed, 01 Dec 2021 02:10:14 -0500 by FUEL