测试 opencv 中 Mat 的 rowRange
、colRange
:
1- Mat::rowRange(int startrow, int endrow)
为矩阵的指定行区间创建一个矩阵头
- startrow – An inclusive 0-based start index of the row span.// 从0开始的行间距索引
- endrow – An exclusive 0-based ending index of the row span.//终止索引
2- Mat::colRange(int startcol, int endcol)
为矩阵的指定列区间创建一个矩阵头
- startcol – An inclusive 0-based start index of the col span.// 从0开始的列间距索引
- endcol – An exclusive 0-based ending index of the col span.//终止索引
#include "opencv2/opencv.hpp"
#include"iostream"
using namespace std;
using namespace cv;
int main()
{
uchar a[16]={1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1};
Mat A(4,4,CV_8UC1,a);
cout<<"A="<<A<<endl;
Mat row12=A.rowRange(1,2);
Mat row13=A.rowRange(1,3);
Mat col12=A.colRange(1,2);
Mat col13=A.colRange(1,3);
cout<<"A.rowRange(1,2)="<<row12<<endl;
cout<<endl;
cout<<"A.rowRange(1,3)="<<row13<<endl;
cout<<endl;
cout<<"A.colRange(1,2)="<<col12<<endl;
cout<<endl;
cout<<"A.colRange(1,3)="<<col13<<endl;
cout<<endl;
return 0;
}
输出如下:
A=[ 1, 0, 0, 0;
0, 1, 0, 0;
0, 0, 1, 0;
0, 0, 0, 1]
A.rowRange(1,2)=[ 0, 1, 0, 0]
A.rowRange(1,3)=[ 0, 1, 0, 0;
0, 0, 1, 0]
A.colRange(1,2)=[ 0;
1;
0;
0]
A.colRange(1,3)=[ 0, 0;
1, 0;
0, 1;
0, 0]
所以,rowRange(x,y)
、colRange(x,y)
是左闭右开 [x,y)
.