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

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. 

主站蜘蛛池模板: 吐鲁番市| 连江县| 渝中区| 贡觉县| 淮南市| 泰安市| 武城县| 兖州市| 甘孜县| 文昌市| 于都县| 如东县| 澄江县| 都匀市| 都安| 安塞县| 陕西省| 长宁区| 阜康市| 怀集县| 繁峙县| 方山县| 航空| 应城市| 澜沧| 象山县| 西昌市| 清苑县| 长泰县| 全椒县| 勐海县| 定兴县| 五河县| 登封市| 水城县| 鲁甸县| 留坝县| 和平区| 突泉县| 观塘区| 库尔勒市|