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

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()
主站蜘蛛池模板: 霍林郭勒市| 麻江县| 陈巴尔虎旗| 台安县| 石城县| 长寿区| 确山县| 东阳市| 和龙市| 莲花县| 永泰县| 招远市| 特克斯县| 桐梓县| 伊川县| 绵阳市| 连云港市| 洪泽县| 安多县| 郧西县| 马关县| 徐汇区| 大足县| 湟源县| 方山县| 陇川县| 宿州市| 静安区| 清徐县| 故城县| 东城区| 苍南县| 竹溪县| 政和县| 诏安县| 南陵县| 会昌县| 东乌珠穆沁旗| 濮阳县| 龙井市| 香河县|