#include<opencv2/core/core.hpp>#include<iostream>usingnamespace std;usingnamespace cv;intmain(){
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;}return0;}
7.2使用成员函数ptr()
#include<opencv2/core/core.hpp>#include<iostream>usingnamespace std;usingnamespace cv;intmain(){
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;}return0;}
7.3使用成员函数isContinuous()和ptr()
#include<opencv2/core/core.hpp>#include<iostream>usingnamespace std;usingnamespace cv;intmain(){
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;}}return0;}
7.4使用成员变量step和data
#include<opencv2/core/core.hpp>#include<iostream>usingnamespace std;usingnamespace cv;intmain(){
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;}return0;}
7.5分离通道split(mm,vec)
将所有向量第一个值组成的单通道矩阵作为第一通道,以此类推
#include<opencv2/core/core.hpp>#include<iostream>usingnamespace std;usingnamespace cv;intmain(){
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;//第三个通道return0;}
7.6合并通道merge( , , )
将单通道矩阵存储在Mat中:
#include<opencv2/core/core.hpp>#include<iostream>usingnamespace std;usingnamespace cv;intmain(){
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;return0;}
存储在vector容器中,使用merge重载函数:
#include<opencv2/core/core.hpp>#include<iostream>usingnamespace std;usingnamespace cv;intmain(){
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;return0;}