> 文章列表 > 【opencv】图像数字化——认识OpenCV中的Mat类( 7 访问多通道Mat对象中的值)

【opencv】图像数字化——认识OpenCV中的Mat类( 7 访问多通道Mat对象中的值)

【opencv】图像数字化——认识OpenCV中的Mat类( 7 访问多通道Mat对象中的值)

7 访问多通道Mat对象中的值

7.1使用成员函数at()

#include <opencv2/core/core.hpp>  
#include<iostream>  
using namespace std;
using namespace cv;
int main()
{Mat mm = (Mat_<Vec3f>(2, 2) << Vec3f(1, 11, 21), Vec3f(2, 12, 32), Vec3f(3, 13, 23), Vec3f(4, 24, 34));cout << mm << endl;for (int r = 0; r < mm.rows; r++) {for (int c = 0; c < mm.cols; c++) {cout << mm.at<Vec3f>(r, c) << ",";}cout << endl;}return 0;
}

在这里插入图片描述

7.2使用成员函数ptr()

#include <opencv2/core/core.hpp>  
#include<iostream>  
using namespace std;
using namespace cv;
int main()
{Mat mm = (Mat_<Vec3f>(2, 2) << Vec3f(1, 11, 21), Vec3f(2, 12, 32), Vec3f(3, 13, 23), Vec3f(4, 24, 34));cout << mm << endl;for (int r = 0; r < mm.rows; r++) {//每行首元素的地址Vec3f* ptr = mm.ptr<Vec3f>(r);for (int c = 0; c < mm.cols; c++) {cout << ptr[c] << ",";}cout << endl;}return 0;
}

在这里插入图片描述

7.3使用成员函数isContinuous()和ptr()

#include <opencv2/core/core.hpp>  
#include<iostream>  
using namespace std;
using namespace cv;
int main()
{Mat mm = (Mat_<Vec3f>(2, 2) << Vec3f(1, 11, 21), Vec3f(2, 12, 32), Vec3f(3, 13, 23), Vec3f(4, 24, 34));cout << mm << endl;if(mm.isContinuous()){//指向多通道矩阵的第一个元素的指针Vec3f* ptr = mm.ptr<Vec3f>(0);for (int c = 0; c < mm.cols*mm.rows; c++) {cout << ptr[c] << endl;}}return 0;
}

在这里插入图片描述

7.4使用成员变量step和data

#include <opencv2/core/core.hpp>  
#include<iostream>  
using namespace std;
using namespace cv;
int main(){Mat mm = (Mat_<Vec3f>(2, 2) << Vec3f(1, 11, 21), Vec3f(2, 12, 32), Vec3f(3, 13, 23), Vec3f(4, 24, 34));cout << mm << endl;for(int r=0;r<mm.rows;r++){for(int c=0;c<mm.cols;c++){Vec3f* ptr = (Vec3f*)(mm.data + mm.step[0] * r + c * mm.step[1]);cout << *ptr << ",";}cout << endl;}return 0;
}

在这里插入图片描述

7.5分离通道split(mm,vec)

  • 将所有向量第一个值组成的单通道矩阵作为第一通道,以此类推
#include <opencv2/core/core.hpp>  
#include<iostream>  
using namespace std;
using namespace cv;
int main(){Mat mm = (Mat_<Vec3f>(2, 2) << Vec3f(1, 11, 21), Vec3f(2, 12, 32), Vec3f(3, 13, 23), Vec3f(4, 24, 34));cout << mm << endl;vector<Mat> planes;split(mm, planes);cout << planes[0] << endl;//第一个通道cout << planes[1] << endl;//第二个通道cout << planes[2] << endl;//第三个通道return 0;
}

在这里插入图片描述

7.6合并通道merge( , , )

  • 将单通道矩阵存储在Mat中:
#include <opencv2/core/core.hpp>  
#include<iostream>  
using namespace std;
using namespace cv;
int main(){Mat planes0 = (Mat_<int>(2, 2) << 1,2,3,4);Mat planes1 = (Mat_<int>(2, 2) << 5, 6, 7, 8);Mat planes2 = (Mat_<int>(2, 2) << 9, 10, 11, 12);Mat planes[] = { planes0 ,planes1 ,planes2 };Mat mat;merge(planes,3,mat);cout << mat << endl;return 0;
}

在这里插入图片描述

  • 存储在vector容器中,使用merge重载函数:
#include <opencv2/core/core.hpp>  
#include<iostream>  
using namespace std;
using namespace cv;
int main(){Mat planes0 = (Mat_<int>(2, 2) << 1,2,3,4);Mat planes1 = (Mat_<int>(2, 2) << 5, 6, 7, 8);Mat planes2 = (Mat_<int>(2, 2) << 9, 10, 11, 12);vector<Mat> plane;plane.push_back(planes0);plane.push_back(planes1);plane.push_back(planes2);Mat mat;merge(plane, mat);cout << mat << endl;return 0;
}

在这里插入图片描述