【问题标题】:Image Convolution and Boundaries图像卷积和边界
【发布时间】:2016-05-14 05:37:25
【问题描述】:

我一直在尝试将卷积算法实现到一维数组上,但需要表示为 2D NxM 矩阵。在尝试实现类似这样的方法后:

int kCenterX = kCol / 2;
int kCenterY = kRow / 2;

for(int i=0; i < mRow; ++i) {              // rows
    for(int j=0; j < mCol; ++j) {          // columns

        for(int m=0; m < kRow; ++m) {     // kernel rows
            int mm = kRow - 1 - m;      // row index of flipped kernel
            for(int n=0; n < kCol; ++n) { // kernel columns
                int nn = kCol - 1 - n;  // column index of flipped kernel

                // index of input signal, used for checking boundary
                int ii = i + (m - kCenterY);
                int jj = j + (n - kCenterX);

                // ignore input samples which are out of bound
                if( ii >= 0 && ii < mRow && jj >= 0 && jj < mCol )
                    o[i][j] += input[ii][jj] * kern[mm][n];
            }
        }
    }
}

来源:http://www.songho.ca/dsp/convolution/convolution.html#convolution_2d

鉴于给定的两个矩阵是:

input = {1,2,3,4,5,6,7,8,9,10,11,12,15,16};
// 1   2   3   4
// 5   6   7   8
// 9   10  11  12
// 13  14  15  16

sharpen_kernel = {0,-1,0,-1,5,-1,0,-1,0};
// 0  -1  0
// -1  5 -1
// 0  -1  0

问题是开发它的编码器设置边缘之外的所有值都是 0。那么我可以使用什么方法来检查元素何时位于数组的边缘,然后推出这些值以便计算它们与内核值。本质上将矩阵表示为:

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

【问题讨论】:

  • 此外,我也对以下行感到困惑: if( ii >= 0 && ii = 0 && jj

标签: c++ c arrays matrix convolution


【解决方案1】:

这是更新的代码

int kCenterX = kCol / 2;
int kCenterY = kRow / 2;

for(int i=0; i < mRow; ++i) {              // rows
    for(int j=0; j < mCol; ++j) {          // columns

        for(int m=0; m < kRow; ++m) {     // kernel rows
            int mm = kRow - 1 - m;      // row index of flipped kernel
            for(int n=0; n < kCol; ++n) { // kernel columns
                int nn = kCol - 1 - n;  // column index of flipped kernel

                // index of input signal, used for checking boundary
                int ii = i + (m - kCenterY);
                int jj = j + (n - kCenterX);

                if(ii < 0)
                    ii=ii+1;
                if(jj < 0)
                    jj=jj+1;
                if(ii >= mRow)
                    ii=ii-1;
                if(jj >= mCol)
                    jj=jj-1;    
                if( ii >= 0 && ii < mRow && jj >= 0 && jj < mCol )
                    o[i][j] += input[ii][jj] * kern[mm][n];

            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    • 2014-04-26
    • 2020-10-18
    • 1970-01-01
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多