From the course: Hands-On AI: Image Processing with Python
Convolution filters - Python Tutorial
From the course: Hands-On AI: Image Processing with Python
Convolution filters
- [Instructor] Let me tell you about convolution filters, a special category of operations that can be applied to gray scale and color images. A convolution filter consists in taking a kernel, which is a small square matrix of odd dimensions, and we calculate the value of a corresponding pixel in a new resulting image exactly where the center of the kernel overlaps with the original picture. This new pixel is a function of the kernel and the overlapping pixels. In this example, the new pixel is calculated with a weighted sum of the overlapping values. You can think of the kernel as the set of weights. You'll multiply by the values in the overlapping pixels of the original picture. The kernel is overlapped with every pixel in the original picture to produce the resulting picture. Let me show you an example. Let's suppose we have the gray scale image at the left, and we want to run the three by three kernel at the right throughout this picture to produce a new one. If we start with this overlap, the resulting value in the new image will be the result of adding all the products of the overlapped values. That's one times four plus two times zero, plus one times two, and so on. That comes to 56. At the neighboring pixel to the right, we get 66, at the next one, 76, and so on and so forth. Now, you may be wondering what happens with the pixels at the edges. In this example, we're talking about one level of missing pixels outwards because our kernel is three by three. If our kernel was five by five, then we'd be missing two levels of pixels outwards, so we have a problem with corner pixels and also with border pixels at the sites. Luckily, this is a solved problem. There are several methods to handle ledges, and they all involved extending the picture prior to running the filter. In our case, we could make the picture grow outward by a thickness of one pixel, and these pixels may be black, some level of gray, or an extension of the edge pixels like this. Notice that the extended pixels are the same as the edge pixels and corner pixels extend diagonally. This way we can freely run the kernel through the picture at the corners and at the sides. Lastly, let me tell you a few details about convolution filters. First, they take long to compute because we must traverse every pixel in the original picture, each time performing as many multiplication as the kernel's elements, plus one summation. However, convolution filters are highly parallelizable because as you may have noticed, each pixel in the resulting image is a function of the original picture and the kernel. In other words, all pixels in the new image are independent from each other. This means that computers with a graphics processor with hundreds or thousands, of course, can do a great job at running convolution filters. Virtually every software tool you use for image processing or computer vision takes advantage of this fact. OpenCV is no exception. Also, convolution filters are capable of highlighting certain features of pictures, as you will see up ahead. Now, you might be wondering how this works with a color picture. Well, for color images, the process is applied to each channel, which is equivalent to having a 3D kernel with the same shape for all channels. However, you may use a 3D kernel with different contents for each channel.