zoukankan      html  css  js  c++  java
  • opencv笔记三(矩阵卷积操作)

    Matlab中filter2与opencv中filter2D的比较

    比较一:横向卷积

    Matlab中filter2:

    opencv中filter2D:

    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

    结果

     比较二:纵向卷积

    Matlab中filter2:

     opencv中filter2D:

    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

    结果

      结论:Matlab中filter2与opencv中filter2D可以得到完全一样的结果。

  • 相关阅读:
    第三周学习笔记
    质量属性
    第四周学习
    逻辑回归
    架构的概念
    第二周总结
    线性回归
    十步走-阅读笔记六
    十步走-阅读笔记五
    P2633 Count on a tree
  • 原文地址:https://www.cnblogs.com/walker-lin/p/11578940.html
Copyright © 2011-2022 走看看