Matt Young
Matt Young

Reputation: 73

C++ Image kernel wrapping convolution

I am trying to implement an image filtering kernel to an image, in which I use a filtering matrix to filter the image. However, I don't really understand how boundary pixels are dealt with. I assumed that any filter pixels falling out of bounds in the image are reflected over their respective edge. Can anyone explain this more clearly for me?

Thanks!

Upvotes: 1

Views: 1524

Answers (2)

Valy
Valy

Reputation: 583

Well, there are various ways to deal with the boundary pixels in the image. A common one is to pad the image matrix with an additional line and column in order to have neighbors for those boundary pixels (neighbors that are needed in the kernel convolution operation).

For example, you have this n x n image matrix:

a[0][0]   a[0][1] ...   a[0][n-1]
a[1][0]   a[1][1] ...   a[1][n-1]
    .         .             .
    .         .             .
    .         .             .
a[n-1][0] a[n-1][1] ... a[n-1][n-1]

You should pad it by putting the first line below the nth line and the nth line above the first line. Also, the first column is copied after the last column and the last column before the first one.

a[n-1][n-1]   a[n-1][0] a[n-1][1] ...   a[n-1][n-1]   a[n-1][0]

a[0][n-1]     a[0][0]   a[0][1]   ...   a[0][n-1]     a[0][0]
a[1][n-1]     a[1][0]   a[1][1]   ...   a[1][n-1]     a[1][0]
    .             .         .               .             .
    .             .         .               .             .
    .             .         .               .             .
a[n-1][n-1]   a[n-1][0] a[n-1][1] ...   a[n-1][n-1]   a[n-1][0]

a[0][n-1]     a[0][0]   a[0][1]   ...   a[0][n-1]     a[0][0]

As you can see, the corners are reflected in a symmetric way. The top left corner of the new border takes the value of the bottom right element in the initial matrix.

You end up with a (n+2) x (n+2) matrix on which you apply the kernel convolution operation.

Upvotes: 1

Malcolm McLean
Malcolm McLean

Reputation: 6406

There's no answer. You can reflect boundary pixel, which is usually the best option, or you can wrap, which is easier and sometimes correct if the image is torus-like. Or you can pad with grey. But you will get an edge effect whatever you do.

Generally it's easiest to add the padding pixels rather than try to guard for edges in the convolution code itself.

Upvotes: 0

Related Questions