Skip to content

add gaussian filter algorithm and lena.jpg #955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions digital_image_processing/filters/gaussian_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
Implementation of gaussian filter algorithm
"""
from cv2 import imread, cvtColor, COLOR_BGR2GRAY, imshow, waitKey
from numpy import pi, mgrid, exp, square, zeros, ravel, dot, uint8


def gen_gaussian_kernel(k_size, sigma):
center = k_size // 2
x, y = mgrid[0-center:k_size-center, 0-center:k_size-center]
g = 1/(2*pi*sigma) * exp(-(square(x) + square(y))/(2*square(sigma)))
return g


def gaussian_filter(image, k_size, sigma):
height, width = image.shape[0], image.shape[1]
# dst image height and width
dst_height = height-k_size+1
dst_width = width-k_size+1

# im2col, turn the k_size*k_size pixels into a row and np.vstack all rows
image_array = zeros((dst_height*dst_width, k_size*k_size))
row = 0
for i in range(0, dst_height):
for j in range(0, dst_width):
window = ravel(image[i:i + k_size, j:j + k_size])
image_array[row, :] = window
row += 1

# turn the kernel into shape(k*k, 1)
gaussian_kernel = gen_gaussian_kernel(k_size, sigma)
filter_array = ravel(gaussian_kernel)

# reshape and get the dst image
dst = dot(image_array, filter_array).reshape(dst_height, dst_width).astype(uint8)

return dst


if __name__ == '__main__':
# read original image
img = imread(r'../image_data/lena.jpg')
# turn image in gray scale value
gray = cvtColor(img, COLOR_BGR2GRAY)

# get values with two different mask size
gaussian3x3 = gaussian_filter(gray, 3, sigma=1)
gaussian5x5 = gaussian_filter(gray, 5, sigma=0.8)

# show result images
imshow('gaussian filter with 3x3 mask', gaussian3x3)
imshow('gaussian filter with 5x5 mask', gaussian5x5)
waitKey()
2 changes: 1 addition & 1 deletion digital_image_processing/filters/median_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def median_filter(gray_img, mask=3):

if __name__ == '__main__':
# read original image
img = imread('lena.jpg')
img = imread('../image_data/lena.jpg')
# turn image in gray scale value
gray = cvtColor(img, COLOR_BGR2GRAY)

Expand Down
Binary file added digital_image_processing/image_data/lena.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.