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

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. 

主站蜘蛛池模板: 綦江县| 秦皇岛市| 衢州市| 新田县| 长春市| 探索| 大足县| 武定县| 凤山县| 额尔古纳市| 西贡区| 镇赉县| 姚安县| 大洼县| 定西市| 嘉定区| 岳普湖县| 当雄县| 林甸县| 东光县| 红河县| 二手房| 大冶市| 新和县| 乐亭县| 东乡族自治县| 根河市| 铜梁县| 益阳市| 锦州市| 东阿县| 五寨县| 景谷| 宣化县| 海口市| 汉中市| 信阳市| 霸州市| 西和县| 嘉定区| 台前县|