Mat類中的rowRange和colRange的用法

來源:互聯網
上載者:User

最近一直在看一個演算法,其中有用到opencv MAt類的rowRange函數,因為對opencv函數還不熟悉,於是測試了一下。

測試代碼如下:

#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(int x,int y)和Mat.rowRange(range(int x,int y)得到的結果一樣,函數取的實際行數y-x,只取到範圍的右邊界,而不取左邊界,Mat.colRange(int x,int y)有類似的用法。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.