opencv子矩陣操作

來源:互聯網
上載者:User

作者:gnuhpc 
出處:http://www.cnblogs.com/gnuhpc/ view plain print ? #include "highgui.h"   #include "cv.h"   #include <iostream>   #include <iomanip>         using namespace std;      void main()   {   int i=0;   int j=0;   CvRect mat_rect=cvRect(1,1,3,3);//指定取得數組子集的範圍   CvMat *mat=cvCreateMat(6,6,CV_64FC1);//原數組   CvMat *submat=cvCreateMat(3,3,CV_64FC1);//按mat_rect指定取得的數組子集   CvMat *mat_rows=cvCreateMat(2,6,CV_64FC1);//按一定跨度內的行取得的數組子集   /*對這個6*6的矩陣初始化*/   for (i=0;i<6;i++)   {   for (j=0;j<6;j++)   {   CV_MAT_ELEM(*mat,double,i,j)=i*6+j;   }   }   /*列印源矩陣*/   for (i=0;i<6;i++)   {   for (j=0;j<6;j++)   {   cout<<setw(3)<<CV_MAT_ELEM(*mat,double,i,j);   }   cout<<endl;   }   cout<<endl;   /*取子數組,大小根據mat_rect來決定,然後列印*/   cvGetSubRect(mat,submat,mat_rect);   for (i=0;i<3;i++)   {   for (j=0;j<3;j++)   {   cout<<setw(3)<<CV_MAT_ELEM(*submat,double,i,j);   }   cout<<endl;   }   cout<<endl;   /*取第一行(從0開始)取到第(3-1=)2行,並列印*/   cvGetRows(mat,mat_rows,1,3,1);   for (i=0;i<2;i++)   {   for (j=0;j<6;j++)   {   cout<<setw(3)<<CV_MAT_ELEM(*mat_rows,double,i,j);   }   cout<<endl;   }   cvReleaseMat(&mat);   cvReleaseMat(&submat);   cvReleaseMat(&mat_rows);   }  

在OpenCV中一般取元素都用定義好的宏: 
取Mat結構的元素的宏為CV_MAT_ELEM,它取2D的矩陣最方便。 
取IplImage元素的宏為CV_IMAGE_ELEM,如取一個映像(x,y)點的灰階值: 
uchar *ptr=CV_IAMGE_ELEM(image,uchar,i,j*3); 
ptr[0],ptr[1],ptr[2]就是對應的B/G/R三個分量了。


               作者:gnuhpc 
               出處:http://www.cnblogs.com/gnuhpc/ 
               除非另有聲明,本網站採用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.