c++ - 访问数组元素

标签 c++ opencv

我有一个小问题,我想你会很容易解决的。但我仍然不是一个好的程序员。无论如何,问题是我需要访问矩阵元素 (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/

相关文章:

python - opencv中的单应性和图像缩放

opencv - 动态图像大小调整的 GPU 与 CPU 端到端延迟

html - 如何使用 cin 将带有空格的文本插入到 C++ 中的文本文档中?

c++ - 为什么字符串文字只能在某些情况下隐式转换为 char*?

java - 使用 android NativeActivity 时从 SoftInput 接收所有 unicode 字符

c++ - 仅在构建框架时架构 arm64 的 undefined symbol

c++ - bcc32 : strange error when specialising on `std::vector<bool>`

python - 如何使用python在图像中查找字母

python - 在 python 中支持向量机分类器的替代方法?

amazon-web-services - docker 不创建文件