使用 OpenCV 在 3D 矩阵中导入 2D 矩阵

Import 2D matrix in 3D matrix using OpenCV

本文关键字:导入 2D 矩阵 OpenCV 3D 使用      更新时间:2023-10-16

此函数:

 filtro.kernel(n, mat)

返回大小为 15x15 的 2D 矩阵,有没有办法将从 for 循环计算的所有 12 个矩阵添加到大小为 12,15,15 的 3D 矩阵中?

#include <opencv2/core/core.hpp>
#include <opencv2/core/core.hpp>
#include "filter.h"
#include <iostream>

int main(){
    using namespace cv;
    using namespace std;
    cv::Mat mat = cv::Mat::zeros(15, 15, CV_32S);
    filter filtro;
    for (int n = 0; n < 12; n++){
        filtro.kernel(n, mat);
        cout<<"Angle Matrix"<<endl;
        cout<< n*15 <<endl;
        cout<< mat <<endl;
    }
return 0;
}
您可以使用

cv::merge创建multi-channels matrix。但请注意,通道维度是最后一个。12 (15,15) => (15,15,12)

试试这个:

#include <opencv2/core/core.hpp>
#include <opencv2/core/core.hpp>
#include "filter.h"
#include <iostream>

int main(){
    using namespace cv;
    using namespace std;
    filter filtro;
    // Create a vector of Mat 
    vector<Mat> mats;
    for (int n = 0; n < 12; n++){  
        cv::Mat mat = cv::Mat::zeros(15, 15, CV_32S);
        filtro.kernel(n, mat);
        cout<<"Angle Matrix"<<endl;
        cout<< n*15 <<endl;
        cout<< mat <<endl;   
        mats.push_back(mat);
    }
    // Merge the Mats
    Mat dst;
    merge(mats, dst);
    return 0;
}