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<<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<<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<<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