官术网_书友最值得收藏!

2D linear filters

While the preceding filter is a point-based filter, image pixels have information around the pixel as well. In the previous image of the flower, the pixel values in the petal are all yellow. If we choose a pixel of the petal and move around, the values will be quite close. This gives some more information about the image. To extract this information in filtering, there are several neighborhood filters. 

In neighborhood filters, there is a kernel matrix which captures local region information around a pixel. To explain these filters, let's start with an input image, as follows:

This is a simple binary image of the number 2. To get certain information from this image, we can directly use all the pixel values. But instead, to simplify, we can apply filters on this. We define a matrix smaller than the given image which operates in the neighborhood of a target pixel. This matrix is termed kernel; an example is given as follows:

The operation is defined first by superimposing the kernel matrix on the original image, then taking the product of the corresponding pixels and returning a summation of all the products. In the following figure, the lower 3 x 3 area in the original image is superimposed with the given kernel matrix and the corresponding pixel values from the kernel and image are multiplied. The resulting image is shown on the right and is the summation of all the previous pixel products: 

This operation is repeated by sliding the kernel along image rows and then image columns. This can be implemented as in following code. We will see the effects of applying this on an image in coming sections: 

# design a kernel matrix, here is uniform 5x5
kernel = np.ones((5,5),np.float32)/25

# apply on the input image, here grayscale input
dst = cv2.filter2D(gray,-1,kernel)

However, as you can see previously, the corner pixel will have a drastic impact and results in a smaller image because the kernel, while overlapping, will be outside the image region. This causes a black region, or holes, along with the boundary of an image. To rectify this, there are some common techniques used:

  • Padding the corners with constant values maybe 0 or 255, by default OpenCV will use this.
  • Mirroring the pixel along the edge to the external area 
  • Creating a pattern of pixels around the image

The choice of these will depend on the task at hand. In common cases, padding will be able to generate satisfactory results. 

The effect of the kernel is most crucial as changing these values changes the output significantly. We will first see simple kernel-based filters and also see their effects on the output when changing the size. 

主站蜘蛛池模板: 日土县| 宁晋县| 扎兰屯市| 铁岭市| 洛川县| 都江堰市| 泰来县| 天水市| 九寨沟县| 井陉县| 黄梅县| 阳原县| 鲁甸县| 黑龙江省| 达日县| 建湖县| 桂阳县| 卫辉市| 贵定县| 娱乐| 浮山县| 前郭尔| 上杭县| 噶尔县| 宜宾市| 洞头县| 科技| 安多县| 柯坪县| 福贡县| 南华县| 临清市| 文化| 岑巩县| 黄平县| 丹江口市| 二连浩特市| 上杭县| 万全县| 永福县| 厦门市|