我怎么能访问一个高维矩阵的元素在OpenCV

how could I access the element of a high-dimensional matrix in OpenCV?

本文关键字:高维矩 元素 OpenCV 一个 访问 怎么能      更新时间:2023-10-16

我试图在OpenCV中使用一个4-d矩阵,初始化部分看起来像这样:

int feature_points_size[] = {bincellDim.x , bincellDim.y , bincellDim.z , 100};
feature_points.create(4 , feature_points_size , CV_64F);

但是库不允许我像这样用'at'访问feature_points的元素:

feature_points.at<double>(k , j , i , l) = stickfea_code.at<double>(l , 0);
feature_points.at<double>(k , j , i , l + 50) = countfea_code.at<double>(l , 0);

似乎Mat.at<>没有4个输入的版本访问它的元素的最佳实践是什么?

提前感谢!

cv::Mat::at<>确实有n-dim过载,您需要使用T& Mat::at(const int* idx)版本。
或者,只需为它编写您自己的(外部)包装器,您可以这样使用:

at4D<double>(feature_points,k,j,i,l);

就像@Adi-Shavit的回答:

std::vector<int> dims = {1, 3, 700, 400};
cv::Mat mat4d = cv::Mat(std::vector<int>, CV_32FC1);
int p[4];
p[0] = 0;
for (unsigned hi=0; hi<dims[3]; hi++) {
    p[3] = hi;
    for (unsigned wi=0; wi<dims[2]; wi++) {
        p[2] = wi;
        for (unsigned ci=0; ci<dims[1]; ci++) {
            p[1] = ci;
            float value = mat4d.at<float>(&p[0]);
            mat4d.at<float>(&p[0]) = some_new_value;
        }
    }
}