Homepage › Solution manuals › Gilbert Strang › Linear Algebra and Learning from Data › Exercise 7.2.1
Exercise 7.2.1
Answers
We obtain the 5 by 5 discrete Gaussian from wikipedia on Kernel (image processing) using Gaussian blur . We have the Gaussian matrix from wiki as
.
It’s easy to check that
See below for the different effects of this filter and the book’s filter (equation 3) on a horizontal edge and a diagonal edge.
It looks like that both Gaussian matrices have similar effects on the edges, i.e. smooth (blur) the original edge. The Gaussian matrix from equation (3) displays sharper edges after filtering than the wikipedia Gaussian.
#### Problem VII.2.1 import numpy as np # Discrete Gaussian from wikipedia gau_wiki = np.array([[1, 4, 6, 4, 1], [4, 16, 24, 16, 4], [6, 24, 36, 24, 6], [4, 16, 24, 16, 4], [1, 4, 6, 4, 1]]) print(’Gaussian␣matrix␣from␣wikipedia␣␣(before␣normalization):␣\n’, gau_wiki) u1 = np.array([[1, 4, 6, 4, 1]]).reshape(-1, 1) v1 = u1.transpose() #u, s, vh = np.linalg.svd(gau_wiki, full_matrices=True) #u.shape, s.shape, vh.shape print(’Gaussian␣5␣by␣5␣equals␣to␣its␣rank-1␣approximation:␣’, np.allclose(np.matmul(u1, v1)/256, gau_wiki/256)) # Discrete Gaussian from equation (3) gau = np.array([[1, 4, 7, 4, 1], [4, 16, 26, 16, 4], [7, 26, 41, 26, 7], [4, 16, 26, 16, 4], [1, 4, 7, 4, 1]]) print(’Gaussian␣matrix␣from␣equation␣3␣(before␣normalization):␣\n’, gau) ones = np.ones((1,5)) zeros = np.zeros((1,5)) horizontal_edge = np.vstack([ones, ones, ones, zeros, zeros]) print(’Horizontal␣edge:␣\n’, horizontal_edge) ones = np.ones((5,5)) diagonal_edge = np.tril(ones) print(’Diagonal␣edge:␣\n’, diagonal_edge) #gau_wiki = gau_wiki/256 #gau = gau/273 # Apply on Horizontal Edge gh_wiki = np.matmul(gau_wiki, horizontal_edge) gh = np.matmul(gau, horizontal_edge) print("\n----------------------␣Output␣-------------------\n") print("Note:␣To␣see␣better␣the␣effect␣of␣filtering,␣we␣don’t␣normalize␣the␣Gaussian␣matrices␣here") print(’Apply␣Wiki␣Gaussian␣to␣Horizontal␣Edge:␣\n’, gh_wiki) print(’Apply␣Equation␣3␣Gaussian␣to␣Horizontal␣Edge:␣\n’, gh) # Apply on Diagonal Edge gd_wiki = np.matmul(gau_wiki, diagonal_edge) gd = np.matmul(gau, diagonal_edge) print(’Apply␣Wiki␣Gaussian␣to␣Diagonal␣Edge:␣\n’, gd_wiki) print(’Apply␣Equation␣3␣Gaussian␣to␣Diagonal␣Edge:␣\n’, gd)
Gaussian matrix from wikipedia (before normalization):
[[ 1 4 6 4 1]
[ 4 16 24 16 4]
[ 6 24 36 24 6]
[ 4 16 24 16 4]
[ 1 4 6 4 1]]
Gaussian 5 by 5 equals to its rank-1 approximation: True
Gaussian matrix from equation 3 (before normalization):
[[ 1 4 7 4 1]
[ 4 16 26 16 4]
[ 7 26 41 26 7]
[ 4 16 26 16 4]
[ 1 4 7 4 1]]
Horizontal edge:
[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]
Diagonal edge:
[[1. 0. 0. 0. 0.]
[1. 1. 0. 0. 0.]
[1. 1. 1. 0. 0.]
[1. 1. 1. 1. 0.]
[1. 1. 1. 1. 1.]]
---------------------- Output -------------------
Note: To see better the effect of filtering, we don’t normalize the Gaussian
matrices here
Apply Wiki Gaussian to Horizontal Edge:
[[11. 11. 11. 11. 11.]
[44. 44. 44. 44. 44.]
[66. 66. 66. 66. 66.]
[44. 44. 44. 44. 44.]
[11. 11. 11. 11. 11.]]
Apply Equation 3 Gaussian to Horizontal Edge:
[[12. 12. 12. 12. 12.]
[46. 46. 46. 46. 46.]
[74. 74. 74. 74. 74.]
[46. 46. 46. 46. 46.]
[12. 12. 12. 12. 12.]]
Apply Wiki Gaussian to Diagonal Edge:
[[16. 15. 11. 5. 1.]
[64. 60. 44. 20. 4.]
[96. 90. 66. 30. 6.]
[64. 60. 44. 20. 4.]
[16. 15. 11. 5. 1.]]
Apply Equation 3 Gaussian to Diagonal Edge:
[[ 17. 16. 12. 5. 1.]
[ 66. 62. 46. 20. 4.]
[107. 100. 74. 33. 7.]
[ 66. 62. 46. 20. 4.]
[ 17. 16. 12. 5. 1.]]