如何在openCV中修改部分多维矩阵

How to modify part of the multi-dimensional matrix in openCV?

本文关键字:修改部 openCV      更新时间:2023-10-16

我想使用openCV修改多维矩阵的一部分。基本上我想实现与在Matlab中编写的相同:

A = zeros(5,5,25);
A(:,:,1) = some_matrix1;
A(:,:,2) = some_matrix2;

我不确定我是否应该使用具有25通道的5x5矩阵或具有单通道的5x5x25矩阵。下面是我的尝试:

int dim[3] = { 5,5,25 };
Mat A(3, dim, CV_32FC(1), Scalar::all(0));
A(Range::all(),Range::all(),0) = some_matrix;

但是我似乎只能在两个维度上使用Range。或者

Mat A(5, 5, CV_32FC(25), Scalar::all(0));
A(Range::all(),Range::all())[0] = some_matrix;

但是在这种情况下,我不知道如何访问通道。你能帮我一下吗?

OpenCV针对2D矩阵进行了优化。多维矩阵将工作,但相当低效和难以访问。

这个示例代码将向您展示如何从3D矩阵中写入和读取值:

#include <opencv2opencv.hpp>
using namespace cv;
int main()
{
    int sizes[] = { 5, 5, 25 };
    Mat data(3, sizes, CV_32F);
    Mat1f some_matrix(sizes[0], sizes[1]);
    randu(some_matrix, 0.f, 100.f); // some random  values
    // Init data with each plane a constant increasing value 
    for (int z = 0; z < data.size[2]; ++z)
    {
        // Set each z-plane to some scalar value
        Range ranges[] = { Range::all(), Range::all(), Range(z, z + 1) };
        data(ranges) = data.size[2] - z;
    }
    // Set the n-th z-plane to some_matrix
    int z = 0;
    for (int r = 0; r < sizes[0]; ++r)
    {
        for (int c = 0; c < sizes[1]; ++c)
        {
            data.at<float>(r, c, z) = some_matrix(r, c);
        }
    }
    // Access all slices along z dimension
    for (int z = 0; z < data.size[2]; ++z)
    {
        Range ranges[] = { Range::all(), Range::all(), Range(z, z + 1) };
        Mat slice3d(data(ranges).clone()); // with clone slice is continuous, but still 3d
        Mat slice(2, &data.size[0], data.type(), slice3d.data);
    }
    return 0;
}

然而,将5x5x25 3D矩阵存储为std::vector<Mat>更容易和实用,其中vector的长度为25,并且每个矩阵都是2D 5x5。

查看代码:

#include <opencv2opencv.hpp>
using namespace cv;
int main()
{
    int sizes[] = { 5, 5, 25 };
    vector<Mat> data(sizes[2]);
    // Init data with each plane a constant increasing value
    for (int z = 0; z < sizes[2]; ++z)
    {
        data[z] = Mat(sizes[0], sizes[1], CV_32F, float(sizes[2] - z));
    }
    Mat1f some_matrix(sizes[0], sizes[1]);
    randu(some_matrix, 0.f, 100.f); // some random  values
    // Set the n-th z-plane to some_matrix
    int z = 0;
    data[z] = some_matrix;
    return 0;
}

这段代码可以从通道访问像素,您可以尝试一下。

int dim[3] = { 5,5,25 };
Mat A(3, dim, CV_32FC1, Scalar::all(0));
 for (int m = 0; m < 5; m++)
{
  for (int n = 0; n < 5; n++)
  {
    for (int a = 0; a < 25; a++) // no of channels
    {
      cout << A.at<cv::Vec3f>(m,n)[a] << endl;
    }
  }
}