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

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()
主站蜘蛛池模板: 新乡县| 瑞昌市| 平遥县| 建德市| 安陆市| 上杭县| 和林格尔县| 寻乌县| 科技| 开江县| 巴林左旗| 聊城市| 前郭尔| 当阳市| 汝南县| 漳浦县| 阳城县| 甘孜县| 梨树县| 肃南| 洞口县| 晋宁县| 佳木斯市| 盐津县| 隆子县| 明溪县| 德阳市| 肇州县| 罗定市| 鄂托克旗| 临江市| 通榆县| 平乡县| 陵水| 桃园市| 张家口市| 抚松县| 嘉义市| 龙里县| 无棣县| 新源县|