OpenCV Sub-matrix operations

Source: Internet
Author: User

Author: gnuhpc
Source: 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);//Specifies the range of array subsets    Cvmat *mat=cvcreatemat (6,6,CV_64FC1);//original array    Cvmat *submat=cvcreatemat (3,3,CV_64FC1);//The subset of arrays obtained by mat_rect specified    cvmat *mat_rows= Cvcreatemat (2,6,CV_64FC1);//the array subset   /* that is obtained by a line within a certain span */   for  (i=0;i<6;i++) for this 6*6 matrix initialization    {   for  (j=0;j<6;j++)    {   Cv_mat_elem (*mat,double,i,j) =i*6+j;   }  }  /* Print source Matrix */   for  (i=0;i<6;i++)    {   for  (j=0;j<6;j++)    {  COUT&LT;&LT;SETW (3) <<cv_mat_elem (*mat,double,i,j);  }   cout<<endl;   }   cout<<endl;  /* Takes a subarray, size is determined according to Mat_rect, and then prints */   cvgetsubrect (mat,submat,mat _rect);   for  (i=0;i<3;i++)    {   for  (j=0;j<3;j++)    {    COUT&LT;&LT;SETW (3) <<cv_mat_elem (*submat,double,i,j);  }   cout<< endl;  }   cout<<endl;  /* Take the first line (starting from 0) to Line 2 (3-1=) and print */   cvGetRows ( mat,mat_rows,1,3,1);   for  (i=0;i<2;i++)    {   for  (j=0;j<6;j++)    {   COUT&LT;&LT;SETW (3) <<cv_mat_elem (*mat_rows,double,i,j);  }    cout<<endl;  }   Cvreleasemat (&mat);   cvReleaseMat (&submat);    Cvreleasemat (&mat_rows);  }  

In OpenCV, it is common to take elements with defined macros:
The macro that takes the element of the MAT structure is Cv_mat_elem, it is the most convenient to take 2D matrix.
The macro that takes the Iplimage element is Cv_image_elem, such as the grayscale value of an image (X, y) point:
Uchar *ptr=cv_iamge_elem (image,uchar,i,j*3);
PTR[0],PTR[1],PTR[2] is the corresponding B/G/R three components.


Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/
Unless otherwise stated, this website uses

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.