GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — add gaussian filter algorithm and lena.jpg (#955) · jusing-es/Python@408c5de

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • digital_image_processing
    • filters
      • gaussian_filter.py
      • median_filter.py
    • image_data
      • lena.jpg
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎digital_image_processing/filters/gaussian_filter.py‎

Copy file name to clipboard
+53Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
Implementation of gaussian filter algorithm
3+
"""
4+
from cv2 import imread, cvtColor, COLOR_BGR2GRAY, imshow, waitKey
5+
from numpy import pi, mgrid, exp, square, zeros, ravel, dot, uint8
6+
7+
8+
def gen_gaussian_kernel(k_size, sigma):
9+
center = k_size // 2
10+
x, y = mgrid[0-center:k_size-center, 0-center:k_size-center]
11+
g = 1/(2*pi*sigma) * exp(-(square(x) + square(y))/(2*square(sigma)))
12+
return g
13+
14+
15+
def gaussian_filter(image, k_size, sigma):
16+
height, width = image.shape[0], image.shape[1]
17+
# dst image height and width
18+
dst_height = height-k_size+1
19+
dst_width = width-k_size+1
20+
21+
# im2col, turn the k_size*k_size pixels into a row and np.vstack all rows
22+
image_array = zeros((dst_height*dst_width, k_size*k_size))
23+
row = 0
24+
for i in range(0, dst_height):
25+
for j in range(0, dst_width):
26+
window = ravel(image[i:i + k_size, j:j + k_size])
27+
image_array[row, :] = window
28+
row += 1
29+
30+
# turn the kernel into shape(k*k, 1)
31+
gaussian_kernel = gen_gaussian_kernel(k_size, sigma)
32+
filter_array = ravel(gaussian_kernel)
33+
34+
# reshape and get the dst image
35+
dst = dot(image_array, filter_array).reshape(dst_height, dst_width).astype(uint8)
36+
37+
return dst
38+
39+
40+
if __name__ == '__main__':
41+
# read original image
42+
img = imread(r'../image_data/lena.jpg')
43+
# turn image in gray scale value
44+
gray = cvtColor(img, COLOR_BGR2GRAY)
45+
46+
# get values with two different mask size
47+
gaussian3x3 = gaussian_filter(gray, 3, sigma=1)
48+
gaussian5x5 = gaussian_filter(gray, 5, sigma=0.8)
49+
50+
# show result images
51+
imshow('gaussian filter with 3x3 mask', gaussian3x3)
52+
imshow('gaussian filter with 5x5 mask', gaussian5x5)
53+
waitKey()
Collapse file

‎digital_image_processing/filters/median_filter.py‎

Copy file name to clipboardExpand all lines: digital_image_processing/filters/median_filter.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def median_filter(gray_img, mask=3):
2828

2929
if __name__ == '__main__':
3030
# read original image
31-
img = imread('lena.jpg')
31+
img = imread('../image_data/lena.jpg')
3232
# turn image in gray scale value
3333
gray = cvtColor(img, COLOR_BGR2GRAY)
3434

Collapse file

‎digital_image_processing/image_data/lena.jpg‎

Copy file name to clipboard
102 KB
  • Display the source diff
  • Display the rich diff
Loading

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.