开发者

Accessing array elements

开发者 https://www.devze.com 2023-02-01 05:02 出处:网络
I have a small problem, which I think it will be easy for you to figure out. But still I\'m not a good programmer. Anyway, the problem is that I need to access the matrix element (20*2), this matrix i

I have a small problem, which I think it will be easy for you to figure out. But still I'm not a good programmer. Anyway, the problem is that I need to access the matrix element (20*2), this matrix is representing x,y locations for 20 features in image. I need to have a parameter that can give开发者_C百科 me the value of all them as x and another one for y; for example P = (all x values) and q= (all y values) in order to use them to draw on the image.

The function for creating the matrix is an opencv function.

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

which this matrix has the values of frame features in x,y. I have used this code to print it out:

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 work out well, but as I mentioned above guys, that I want to sign the values to a parameters in order to use them?


Do 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]; 
    }
}

Usage:

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

精彩评论

暂无评论...
验证码 换一张
取 消