opencv筆記三(矩陣卷積操做)

Matlab中filter2與opencv中filter2D的比較ide

比較一:橫向卷積spa

Matlab中filter2:3d

 

opencv中filter2D:code

Mat C =(Mat_<float>(3,4)<<1,2,3,4,5,6,7,8,9,10,11,12);
    float *data = NULL;
    for (int row = 0; row < C.rows; row++)
    {
        data = (float *)C.ptr<float>(row);
        cout << "[";
        for (int col = 0; col < C.cols; col++)
        {
            cout << data[col] << " ";
        }
        cout << "]"  << endl;
    }
    Mat D;
    copyMakeBorder(C, D, 2, 2, 2, 2, BORDER_REPLICATE);
    for (int row = 0; row < D.rows; row++)
    {
        data = (float *)D.ptr<float>(row);
        cout << "[";
        for (int col = 0; col < D.cols; col++)
        {
            cout << data[col] << " ";
        }
        cout << "]"  << endl;
    }
    cout << endl;

    D = (Mat_<float>(1,3) << 0.1, 0.2 ,0.3);
    Mat E;
    filter2D(C, E, -1, D,Point(-1,-1),0, BORDER_REPLICATE);
    for (int row = 0; row < E.rows; row++)
    {
        data = (float *)E.ptr<float>(row);
        cout << "[";
        for (int col = 0; col < E.cols; col++)
        {
            cout << data[col] << " ";
        }
        cout << "]"  << endl;
    }
    cout << endl;
View Code

結果blog

 比較二:縱向卷積opencv

Matlab中filter2:event

 

 opencv中filter2D:class

Mat C =(Mat_<float>(3,4)<<1,2,3,4,5,6,7,8,9,10,11,12);
    float *data = NULL;
    for (int row = 0; row < C.rows; row++)
    {
        data = (float *)C.ptr<float>(row);
        cout << "[";
        for (int col = 0; col < C.cols; col++)
        {
            cout << data[col] << " ";
        }
        cout << "]"  << endl;
    }
    Mat D;
    copyMakeBorder(C, D, 2, 2, 2, 2, BORDER_REPLICATE);
    for (int row = 0; row < D.rows; row++)
    {
        data = (float *)D.ptr<float>(row);
        cout << "[";
        for (int col = 0; col < D.cols; col++)
        {
            cout << data[col] << " ";
        }
        cout << "]"  << endl;
    }
    cout << endl;

    D = (Mat_<float>(3,1) << 0.1, 0.2 ,0.3);
    Mat E;
    filter2D(C, E, -1, D,Point(-1,-1),0, BORDER_REPLICATE);
    for (int row = 0; row < E.rows; row++)
    {
        data = (float *)E.ptr<float>(row);
        cout << "[";
        for (int col = 0; col < E.cols; col++)
        {
            cout << data[col] << " ";
        }
        cout << "]"  << endl;
    }
    cout << endl;
View Code

結果cli

  結論:Matlab中filter2與opencv中filter2D能夠獲得徹底同樣的結果。sed

相關文章
相關標籤/搜索