Beruflich Dokumente
Kultur Dokumente
Impulse Response
Impulse response is the output of a system resulting from an impulse
function as input. And it is denoted as h[n].
be .
Let's compute manually each value of y[0], y[1], y[2], y[3], ...
Output: y[n]
Notice that y[0] has only one component, x[0]h[0], and others are
omitted, because others are all zeros at k ≠ 0. In other words, x[1]h[-
1] = x[2]h[-2] = 0. The above equations also omit the terms if they are
obviously zeros.
Notice the first sample, y[0] has only one term. Based on the pattern
that we found, y[0] is calculated as:
y[0] = x[0]·h[0] + x[-1]·h[1]. Because x[-1] is not defined, we simply
pad it to zero.
C++ Implementation for Convolution 1D
Implementing the convolution algorithm is quite simple. The code
snippet is following;
for ( i = 0; i < sampleCount; i++ )
{
y[i] = 0; // set to zero before sum
for ( j = 0; j < kernelCount; j++ )
{
y[i] += x[i - j] * h[j]; // convolve: multiply and accumulate
}
}
However, you should concern several things in the implementation.
Watch out the range of input signal. You may be out of bound of input
signal, for example, x[-1], x[-2], and so on. You can pad zeros for
those undefined samples, or simply skip the convolution at the
boundary. The results at the both the beginning and end edges cannot be
accurate anyway.
Second, you need rounding the output values, if the output data type is
integer and impulse response is floating point number. And, the output
value may be exceeded the maximum or minimum value.
If you have unsigned 8-bit integer data type for output signal, the
range of output should be between 0 and 255. You must check the value
is greater than the minimum and less than the maximum value.
Convolution in 2D
2D convolution is just extension of previous 1D convolution by
convolving both horizontal and vertical directions in 2 dimensional
spatial domain. Convolution is frequently used for image processing,
such as smoothing, sharpening, and edge detection of images.
Let's pick a simplest sample and compute convolution, for instance, the
output at (1, 1) will be;
Example of 2D Convolution
Here is a simple example of convolution of 3x3 input signal and impulse
response (kernel) in 2D spatial. The definition of 2D convolution and
the method how to convolve in 2D are explained here.
In general, the size of output signal is getting bigger than input
signal, but we compute only same area as input has been defined.
Because we forced to pad zeros where inputs are not defined, such
as x[-1,-1], the results around the edge cannot be accurate. Plus, the
size of output is fixed as same as input size in most image processing.
Note that the matrices are referenced here as [column, row], not [row,
column]. M is horizontal (column) direction and N is vertical (row)
direction.
By the way, the kernel in this example is called Sobel filter, which is
used to detect the horizontal edge lines in an image. See more details
in the window filters.
Separable Convolution 2D
In convolution 2D with M×N kernel, it requires M×N multiplications for
each sample. For example, if the kernel size is 3x3, then, 9
multiplications and accumulations are necessary for each sample. Thus,
convolution 2D is very expensive to perform multiply and accumulate
operation.
it is convolving with input and h1, then convolve once again with the
result of previous convolution and h2. Therefore, the separable 2D
convolution is performing twice of 1D convolution in horizontal and
vertical direction.
And, convolution is associative, it does not matter which direction
perform first. Therefore, you can convolve with h2[n] first then
convolve with h1[m] later.
Example
Compute y[1,1] by using separable convolution method and compare the
result with the output of normal convolution 2D.
i) Using convolution 2D
Take a look at a real example; convolution with 256x256 image and 5x5 Gaussian filter.
For the pixels on the border of image matrix, some elements of the
kernel might stands out of the image matrix and therefore does not have
any corresponding element from the image matrix. In this case, you can
eliminate the convolution operation for these position which end up an
output matrix smaller than the input (image matrix) or we can apply
padding to the input matrix (based on the size of the kernel we might
need one or more pixels padding, in our example we just need 1 pixel
padding):
Figure 5: Convolution calculation on borders
As you can see in Figure 5, the output of convolution might violate the
input range of [0-255]. Even though the python packages would take care
of it by considering the maximum value of the image as the pure white
(correspond to 255 in [0-255] scale) and the minimum value as the pure
black (correspond to 0 in [0-255] scale), the values of the convolution
output (filtered image) specially along the edges of the image (which
are calculated based on the added zero padding) can cause a low
contrast filtered image. In this post, to overcome this loss of
contrast issue, we use Histogram Equalization technique. However, you
might be able to end up with a better contrast neglecting the zero
padding. The following python code convolves an image with the sharpen
kernel and plots the result:
A separable filter in image processing can be written as product of two more simple filters.
Typically a 2-dimensional convolution operation is separated into two 1-dimensional filters. This
reduces the cost of computing the operator.
Examples