Mat类中的rowRange和colRange

rowRange:创建一个新的矩阵头,取得指定行区间元素
colRange:创建一个新的矩阵头,取得指定列区间元素

Mat.rowRange(int x,int y)Mat.rowRange(range(int x,int y)得到的结果一样,函数取的实际行数y-x,只取到范围的左边界(x),而不取右边界(y)。

简单理解:
rowRange(int x, int y) (其中y应小于等于行数,例如一个矩阵最大为5行,那么y最大为4) 的创建矩阵范围为从x行为首行开始,往后数y-x行。
**例如:**rowRange(0,3) 位 从第0行开始,往后数3行。即 0 、1、2行。
colRange(int x,int y)同理。

代码验证:

#include<stdio.h>
#include<opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main()
{
 //初始化一个3*3的矩阵
 Mat examples=(Mat_<float>(3,3)<<1,0,0,0,1,0,0,0,1);
 //取example中中特定范围的行列构成矩阵
 Mat temp_row1=examples.rowRange(Range(1,2));  //取特定行
 Mat temp_row2=examples.rowRange(1,2);
 Mat temp_col=examples.colRange(1,3);         //取特定列
 //n,p,m,q计数,用于控制矩阵元素的输出格式
 int n=0;
 int p=0;
 int m=0;
 int q=0;
 //输出初始矩阵的元素
 cout<<"examples:"<<endl;
 for (int i=0;i<examples.rows;i++){
  for(int j=0;j<examples.cols;j++)
  {
   cout<<examples.at<float>(i,j) ;
   p++;
   if(p%3==0)cout<<endl;  
  }
 }
 cout<<endl;
 //测试rowRange(Range(int x,int y))
 cout<<"temp_row1:"<<endl;
 for (int i=0;i<temp_row1.rows;i++)
 {
  for(int j=0;j<temp_row1.cols;j++){
   cout<<temp_row1.at<float>(i,j);
   n++;
   if(n%3==0) 
    cout<<endl;
  }
 }
 cout<<endl;
 //测试rowRange(int x,int y)函数
 cout<<"temp_row2:"<<endl;
 for(int i=0;i<temp_row2.rows;i++)
 { 
  for(int j=0;j<temp_row2.cols;j++){
   cout<<temp_row2.at<float>(i,j);
   m++;
   if(m%3==0) 
    cout<<endl;
  }
 }
 cout<<endl;
 //测试colRange(int x,int y)函数
 cout<<"temp_col:"<<endl;
 for (int i=0;i<temp_col.rows;i++)
 {
  for(int j=0;j<temp_col.cols;j++){
   cout<<temp_col.at<float>(i,j);
   q++;
   if(q%2==0) 
    cout<<endl;
  }
 }
 return 0;
}

结果:
在这里插入图片描述参考:
Mat类中的rowRange和colRange的用法

关于学习Mat类中rowRange和colRange的初步认识

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值