Access to array elements

Hey guys, I have a little problem, and I think it will be easy for you to understand. But still I'm not a good programmer. Anyway, the problem is that I need to access the matrix element (20 * 2), this matrix represents x, y locations for 20 functions in the image. I need to have a parameter that can give me the value of all of them as x and one more for y; for example, P = (all x values) and q = (all y values) to use them for drawing on the image.

The function to create the matrix is ​​an opencv function.

CvMat* mat = cvCreateMat(20,2,CV_32FC1); 

for which this matrix has the values ​​of the frame functions in x, y. I used this code to print it:

  float t[20][2]; for (int k1=0; k1<20; k1++) { for (int k2=0; k2<2; k2++) { t[k1][k2] = cvmGet(mat,k1,k2); std::cout<< t[k1][k2]<<"\t"; } } std::cout <<" "<< std::endl; std::cout <<" "<< std::endl; std::cout <<" "<< std::endl; } 

This code works well, but as I said above, guys, I want to sign values ​​to parameters in order to use them?

Thanks.

+4
source share
1 answer

You want something like this:

 void GetMatrixElem( float t [][2] ,int x ,int y ,float** val ) { if (val) // && (x >= 0) && (x < 20) && (y>=0) && (y<2) *val = &t[x][y]; } // ... float t [20][2]; float* pElem = NULL; GetMatrixElem( t ,10 ,1 ,&pElem ); 

for columns and rows you can use something like this:

 void GetClmn( float t[][2] ,int y ,float* pClmn[] ) { for( int x = 0; x < 20; x++ ) { pClmn[x] = &t[x][y]; } } void GetRow( float t[][2] ,int x ,float* pRow[] ) { for( int y = 0; y < 2; y++ ) { pRow[y] = &t[x][y]; } } 

Using:

 float* pClm[20]; GetClmn( t ,1 ,pClm); float* pRow[2]; GetRow( t ,19 ,pRow ); 
+1
source

Source: https://habr.com/ru/post/1332991/


All Articles