【问题标题】:Matrix manipulation on MATLAB to C++ or OpenCV在 MATLAB 上对 C++ 或 OpenCV 进行矩阵操作
【发布时间】:2016-11-16 06:50:18
【问题描述】:

我正在尝试在 C++ 中执行此操作,但我无法理解它。 我尝试在http://mathworks.com/help/matlab/ref/image.html 中查找,但还是不明白。

im 是 Matlab 中的矩阵。宽度为 640

im(:,Width+(1:2),:) = im(:,1:2,:);

OpenCVMatrix 或C++中有没有类似这个操作的东西

【问题讨论】:

    标签: c++ matlab opencv matrix


    【解决方案1】:

    解决方案 1

    你可以使用 colRange 函数:

    mat.colRange(0, 2).copyTo(mat.colRange(w, 2 + w)); 
    

    例子:

    //initilizes data
    float data[2][4] = { { 1, 2, 3, 4}, { 5, 6, 7, 8 } };
    Mat mat(2, 4, CV_32FC1, &data);
    int w = 2; //w is equivelant to Width in your script, in this case I chose it to be 2
    
    std::cout << "mat before: \n" << mat << std::endl;
    
    mat.colRange(0, 2).copyTo(mat.colRange(w, 2 + w)); 
    
    std::cout << "mat after: \n" << mat << std::endl;
    

    结果:

    mat before:
    [1, 2, 3, 4;
     5, 6, 7, 8]
    mat after:
    [1, 2, 1, 2;
     5, 6, 5, 6]
    

    解决方案 2

    或者,使用 cv::Rect 对象,如下:

    cv::Mat roi = mat(cv::Rect(w, 0, 2, mat.rows));
    mat(cv::Rect(0, 0, 2, mat.rows)).copyTo(roi);
    

    有几种方法可以初始化一个 Rect,在我的例子中,我选择了以下 c-tor:

    cv::Rect(int x, int y, int width, int height);
    

    结果与解决方案 1 中的结果相同。

    【讨论】:

      【解决方案2】:

      也许您也可以使用可以满足需求的 Eigen。它有Block operations

      调整链接下提供的示例,您需要类似:

      Eigen::MatrixXf m(4, 4);
      m << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16;
      cout <<"original matrix \n" << m << endl;
      m.block<2, 2>(1, 1) = m.block<2, 2>(2, 2);
      cout <<"modified matrix \n" << m << endl;
      

      输出:

      original matrix 
      1  2  3  4
      5  6  7  8
      9 10 11 12
      13 14 15 16
      
      modified matrix 
      1  2  3  4
      5 11 12  8
      9 15 16 12
      13 14 15 16
      

      【讨论】:

        猜你喜欢
        • 2018-01-23
        • 1970-01-01
        • 1970-01-01
        • 2013-03-04
        • 2016-12-21
        • 1970-01-01
        • 2019-03-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多