3 – Creating a Filter

So let’s see how to create our own high pass filter. First I’ll show you how to define your own custom kernel and later we’ll use openCV functions to help us build commonly used filters. Here’s an image of a building. It’s actually San Francisco City Hall. I read it in and as usual I’ve made an RGB copy for image processing. The first thing I’ll do is to convert this to grayscale using CVT color and the code RGB to gray. This way a filter will be able to detect changes in image intensity. Next, I’ll create a custom edge detection filter. I want to build one that detects vertical edges and ignores horizontal edges. I’ll do this by making a three by three matrix and using values that will calculate the difference between the left and right sides of this pixel area in an image. I’m starting with an empty three by three kernel. I’ll put negative values on the left side, weighty in the corners, less in the edges right next to the center pixel. I’ll put zeros in the center column and then positive values on the right side. This kernel one centered on an image pixel will calculate the difference between pixels on the right side and the left thereby detecting vertical edges. This is actually a really common filter called the sobel filter. The sobel filter detects abrupt intensity changes in the x and y direction separately. This one is an approximation of the gradient or change in intensity over space in the x direction and I’ll actually name it sobel X. Then, to convolve this filter with the input image, I’ll use an openCV function called Filter2D. This function takes in an input image, the type of output image, a negative one just means the output will have the same type as the input image, and finally the kernel we just created, sobel X, and it produces a filtered image. Let’s display it and see what it looks like. I see basically what I expected. All the vertical edges are highlighted and this kind of filter helps me detect the vertical edges of the building and the structures that surround it. My last step which is a useful thing to learn in computer vision is turning this image into a binary image. That is, I want to turn this into a purely black and white image with the strongest edges clearly visible. To do that I can use the openCV function threshold. This takes in our filtered image that we want to perform the threshold on, a low value for the pixel values to pass through and I’ll set this at a medium high level at 100. Then it takes in the pixel value to turn these all into which I’ll set as white or 255. And finally the type of threshold, a binary threshold and I’ll display this image. Here we can see the stronger edges clearly and if I lower this threshold to say 50 and run this code again, I’ll detect more edges. In fact too many edges to really be helpful. So let’s go back to a 100 threshold. So here I am zooming in to see the difference between this first filtered image and this second binary image. We can see that in addition to the edges of the building and windows and other structures, there are a lot of other smaller detected edges. These dots on the grass for example. These don’t provide any extra information about the building or scenery. They’re really just bloody noise. Noise is usually represented as dots or fuzzy details. By detecting edges like this a high pass filter can actually exaggerate noise in an image. So we have to be cautious when we apply these filters. Often we have to take an additional step to make sure we don’t enhance undesirable noise. Soon we’ll see that we can use a low pass filter to blur an image and reduce unwanted noise. We’ll often do this before applying a high pass filter like this and we’ll get into the applications of low pass filters after you get a little more practice applying high pass edge detection and filters.

%d 블로거가 이것을 좋아합니다: