date: 2024-09-20
title: CV-HW-3
status: DONE
author:
  - AllenYGY
tags:
  - CV
  - HOMEWORK
publish: TrueCV-HW-3
Using a Gaussian filter to smooth the image and then downsample the image by a factor of 2.
We can apply twice the 1D Gaussian filter to the image in the x direction and then in the y direction. then we can downsample the image by a factor of 2 in the x direction and then in the y direction.
Suppose the image is a 2D matrix.
The Gaussian filter is a 2D matrix
The Downsample matrix is a 2D matrix.
The downsampled image is
import random
import numpy as np
def generate_matrix():
    matrix = []
    random.seed(0)
    for i in range(8):
        row = []
        for j in range(8):
            row.append(random.randint(0, 9))
        matrix.append(row)
    return matrix
def downsample(matrix, G_x, G_y):
    print("Apply G_x to matrix")
    new_matrix = np.matmul(G_x, matrix)*0.0625
    print(new_matrix)
    print("Apply G_y to new_matrix")
    new_matrix = np.matmul(new_matrix, G_y)*0.0625
    print("Downsampled Matrix:")
    print(new_matrix)
    return new_matrix
G_x = [[6, 4, 1, 0, 0, 0, 0, 0], [1, 4, 6, 4, 1, 0, 0, 0],   [0, 0, 1, 4, 6, 4, 1, 0], [0, 0, 0, 0, 1, 4, 6, 4]]
G_y = [[6,1,0,0],
       [4,4,0,0],
       [1,6,1,0],
       [0,4,4,0],
       [0,1,6,1],
       [0,0,4,4],
       [0,0,1,6],
       [0,0,0,4]]
matrix = generate_matrix()
matrix = np.array(matrix)
G_x = np.array(G_x)
G_y = np.array(G_y)
print("Original Matrix:")
print(matrix)
print("G_x:")
print(G_x)
print("G_y:")
print(G_y)
downsampled_matrix = downsample(matrix, G_x, G_y)
    Original Matrix (8x8):
    [[6 6 0 4 8 7 6 4]
     [7 5 9 3 8 2 4 2]
     [1 9 4 8 9 2 4 1]
     [1 5 7 8 1 5 6 5]
     [9 3 8 7 7 8 4 0]
     [8 0 1 6 0 9 7 5]
     [3 5 1 3 9 3 3 2]
     [8 7 1 1 5 8 7 1]]
    
    G_x Filter:
    [[6 4 1 0 0 0 0 0]
     [1 4 6 4 1 0 0 0]
     [0 0 1 4 6 4 1 0]
     [0 0 0 0 1 4 6 4]]
    
    G_y Filter:
    [[6 1 0 0]
     [4 4 0 0]
     [1 6 1 0]
     [0 4 4 0]
     [0 1 6 1]
     [0 0 4 4]
     [0 0 1 6]
     [0 0 0 4]]
    Apply G_x to matrix
    [[4.0625 4.0625 2.5    2.75   5.5625 3.25   3.5    2.0625]
     [3.3125 6.4375 6.     6.4375 6.5625 3.4375 4.625  2.375 ]
     [5.875  3.25   5.3125 6.8125 4.     6.8125 5.1875 2.6875]
     [5.6875 3.8125 1.375  3.3125 5.0625 5.875  4.875  2.25  ]]
    Apply G_y to new_matrix
    Downsampled Matrix:
    [[2.6953125  3.2421875  3.9609375  2.98828125]
     [3.2265625  6.0859375  5.59375    3.59765625]
     [3.34765625 5.125      5.5625     4.5703125 ]
     [3.171875   2.96875    4.5859375  4.17578125]]
Firstly, fill the zeros in the image by the nearest neighbor interpolation.
Then apply the 2D Gaussian filter to the image.
Suppose the image is a 2D matrix 
Insert zeros in the image to get a new image 
The Gaussian filter is a 2D matrix
import numpy as np
from scipy.ndimage import convolve
def upsample(matrix, G):
    print("Original Matrix (4x4):")
    print(matrix)
    
    new_matrix = np.zeros((8, 8))    
    new_matrix[::2, ::2] = matrix
    print("\nUpsampled Matrix (Insert Zeros):")
    print(new_matrix)
    
    new_matrix = convolve(new_matrix, G, mode='reflect')
    
    print("\nUpsampled Matrix (After Applying Gaussian Filter):")
    print(new_matrix)
    
    return new_matrix
matrix = generate_matrix(4)
matrix = np.array(matrix)
G = [[1, 2, 1], [2, 4, 2], [1, 2, 1]]
G = np.array(G)
upsampled_matrix = upsample(matrix, G)
    Original Matrix (4x4):
    [[6 6 0 4]
     [8 7 6 4]
     [7 5 9 3]
     [8 2 4 2]]
    
    Upsampled Matrix (Insert Zeros):
    [[6. 0. 6. 0. 0. 0. 4. 0.]
     [0. 0. 0. 0. 0. 0. 0. 0.]
     [8. 0. 7. 0. 6. 0. 4. 0.]
     [0. 0. 0. 0. 0. 0. 0. 0.]
     [7. 0. 5. 0. 9. 0. 3. 0.]
     [0. 0. 0. 0. 0. 0. 0. 0.]
     [8. 0. 2. 0. 4. 0. 2. 0.]
     [0. 0. 0. 0. 0. 0. 0. 0.]]
    
    Upsampled Matrix (After Applying Gaussian Filter):
    [[54. 36. 36. 18.  0. 12. 24. 12.]
     [42. 27. 26. 19. 12. 14. 16.  8.]
     [48. 30. 28. 26. 24. 20. 16.  8.]
     [45. 27. 24. 27. 30. 22. 14.  7.]
     [42. 24. 20. 28. 36. 24. 12.  6.]
     [45. 22. 14. 20. 26. 18. 10.  5.]
     [48. 20.  8. 12. 16. 12.  8.  4.]
     [24. 10.  4.  6.  8.  6.  4.  2.]]
Downsample the image by a factor of 2 and then upsample the image by a factor of 2.
The Laplacian filter can be get by using the original image minus the upsampled image.
matrix = generate_matrix(8)
matrix = np.array(matrix)
downsampled_matrix = downsample(matrix, G_x, G_y)
upsampled_matrix = upsample(downsampled_matrix, G)
laplacian_matrix = matrix - upsampled_matrix
print("\nLaplacian Matrix:")
print(laplacian_matrix)
    Downsampled Matrix:
    [[2.6953125  3.2421875  3.9609375  2.98828125]
     [3.2265625  6.0859375  5.59375    3.59765625]
     [3.34765625 5.125      5.5625     4.5703125 ]
     [3.171875   2.96875    4.5859375  4.17578125]]
    Upsampled Matrix (Insert Zeros):
    [[2.6953125  0.         3.2421875  0.         3.9609375  0.
      2.98828125 0.        ]
     [0.         0.         0.         0.         0.         0.
      0.         0.        ]
     [3.2265625  0.         6.0859375  0.         5.59375    0.
      3.59765625 0.        ]
     [0.         0.         0.         0.         0.         0.
      0.         0.        ]
     [3.34765625 0.         5.125      0.         5.5625     0.
      4.5703125  0.        ]
     [0.         0.         0.         0.         0.         0.
      0.         0.        ]
     [3.171875   0.         2.96875    0.         4.5859375  0.
      4.17578125 0.        ]
     [0.         0.         0.         0.         0.         0.
      0.         0.        ]]
    
    Upsampled Matrix (After Applying Gaussian Filter):
    [[24.2578125  17.8125     19.453125   21.609375   23.765625   20.84765625
      17.9296875   8.96484375]
     [17.765625   15.25       18.65625    18.8828125  19.109375   16.140625
      13.171875    6.5859375 ]
     [19.359375   18.625      24.34375    23.359375   22.375      18.3828125
      14.390625    7.1953125 ]
     [19.72265625 17.78515625 22.421875   22.3671875  22.3125     19.32421875
      16.3359375   8.16796875]
     [20.0859375  16.9453125  20.5        21.375      22.25       20.265625
      18.28125     9.140625  ]
     [19.55859375 14.61328125 16.1875     18.2421875  20.296875   18.89453125
      17.4921875   8.74609375]
     [19.03125    12.28125    11.875      15.109375   18.34375    17.5234375
      16.703125    8.3515625 ]
     [ 9.515625    6.140625    5.9375      7.5546875   9.171875    8.76171875
       8.3515625   4.17578125]]
    
    Laplacian Matrix:
    [[-18.2578125  -11.8125     -19.453125   -17.609375   -15.765625
      -13.84765625 -11.9296875   -4.96484375]
     [-10.765625   -10.25        -9.65625    -15.8828125  -11.109375
      -14.140625    -9.171875    -4.5859375 ]
     [-18.359375    -9.625      -20.34375    -15.359375   -13.375
      -16.3828125  -10.390625    -6.1953125 ]
     [-18.72265625 -12.78515625 -15.421875   -14.3671875  -21.3125
      -14.32421875 -10.3359375   -3.16796875]
     [-11.0859375  -13.9453125  -12.5        -14.375      -15.25
      -12.265625   -14.28125     -9.140625  ]
     [-11.55859375 -14.61328125 -15.1875     -12.2421875  -20.296875
       -9.89453125 -10.4921875   -3.74609375]
     [-16.03125     -7.28125    -10.875      -12.109375    -9.34375
      -14.5234375  -13.703125    -6.3515625 ]
     [ -1.515625     0.859375    -4.9375      -6.5546875   -4.171875
       -0.76171875  -1.3515625   -3.17578125]]