我有一个小问题,我想你会很容易解决的。但我仍然不是一个好的程序员。无论如何,问题是我需要访问矩阵元素 (20*2),该矩阵表示图像中 20 个特征的 x、y 位置。我需要一个参数,它可以给我所有的值作为 x,另一个为 y;例如 P =(所有 x 值)和 q=(所有 y 值),以便使用它们在图像上绘制。
创建矩阵的函数是一个opencv函数。
CvMat* mat = cvCreateMat(20,2,CV_32FC1);
该矩阵在 x,y 中具有框架特征的值。我用这段代码打印出来:
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;
这段代码运行良好,但正如我上面提到的,我想将值签名到参数以便使用它们?
最佳答案
你想要这样的东西吗:
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 );
对于列和行,你可以使用这样的东西:
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];
}
}
用法:
float* pClm[20];
GetClmn( t ,1 ,pClm);
float* pRow[2];
GetRow( t ,19 ,pRow );
关于c++ - 访问数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4505917/