Practical Computer Vision
上QQ阅读APP看书,第一时间看更新

Linear filters

To begin with, the simplest kind of filter is a point operator, where each pixel value is multiplied by a scalar value. This operation can be written as follows:

  

Here:

  • The input image is F and the value of pixel at (i,j) is denoted as f(i,j)
  • The output image is G and the value of pixel at (i,j) is denoted as g(i,j)
  • K is scalar constant

Such an operation on an image is termed a linear filter. There are many more kinds of linear filters which you will be reading about further in this section. In addition to multiplication by a scalar value, each pixel can also be increased or decreased by a constant value. So overall point operation can be written as follows:

  

This operation can be applied both to grayscale images and RGB images. For RGB images, each channel will be modified with this operation separately. The following is the result of varying both K and L. The first image is input on the left. In the second image, K=0.5 and L=0.0, while in the third image, K is set to 1.0 and L is 10. For the final image on the right, K=0.7 and L=25. As you can see, varying K changes the brightness of the image and varying L changes the contrast of the image:

This image can be generated with the following code:

import numpy as np 
import matplotlib.pyplot as plt
import cv2

def point_operation(img, K, L):
"""
Applies point operation to given grayscale image
"""
img = np.asarray(img, dtype=np.float)
img = img*K + L
# clip pixel values
img[img > 255] = 255
img[img < 0] = 0
return np.asarray(img, dtype = np.int)

def main():
# read an image
img = cv2.imread('../figures/flower.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# k = 0.5, l = 0
out1 = point_operation(gray, 0.5, 0)

# k = 1., l = 10
out2 = point_operation(gray, 1., 10)

# k = 0.8, l = 15
out3 = point_operation(gray, 0.7, 25)

res = np.hstack([gray,out1, out2, out3])
plt.imshow(res, cmap='gray')
plt.axis('off')

plt.show()


if __name__ == '__main__':
main()