<jupyter_start><jupyter_text>Compute the followings1.   First order derivative and visualize it just like the above Figure.2.   Second order derivative and visualize it just like the above Figure.3. Difference of Gaussian (DoG) and visualize it just like the above Figure. Assume 𝜎1 = 4 and𝜎2 = 7<jupyter_code>import math as m
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

from pylab import rcParams
rcParams['figure.figsize'] = 15, 10<jupyter_output><empty_output><jupyter_text>Visualize 3D Surface Plot<jupyter_code>def surface_plot_3d(X,Y,Z):
  filter_size=5
  fig = plt.figure()
  ax = plt.axes(projection="3d")
  ax = fig.add_subplot(111, projection='3d')

  # transforming x,y,z into 2D arrays for plotting
  xs = X.reshape((filter_size, filter_size))
  ys = Y.reshape((filter_size, filter_size))
  zs = Z.reshape((filter_size, filter_size))

  # passing x,y,z points to plot
  ax.plot_wireframe(xs, ys, zs, color='black')
  ax.set_xlabel('x' )
  ax.set_ylabel('y' )
  ax.set_zlabel('z')
  plt.show()

  # fig = plt.figure(figsize=(20,10))
  fig = plt.figure()
  ax = fig.add_subplot(111, projection='3d')
  # 3D surface plot
  ax.plot_surface(xs, ys, zs, rstride=1, cstride=1, cmap='binary_r', edgecolor='black')

  ax.set_title('Surface plot');
  plt.show()<jupyter_output><empty_output><jupyter_text>1.a First Order Partial Derivative X Gaussian Kernel<jupyter_code>def first_order_derivative_X_gaussian_kernel(filter_size,sigma):
# preferred sigma = 0.5
# preferred filter size = 5
  m = filter_size//2
  n = filter_size//2
  X=[]
  Y=[]
  Z=[]
  for x in range(-m, m+1):
          for y in range(-n, n+1):
              x1 = 2*np.pi*(sigma**4)
              x2 = np.exp(-(x**2 + y**2)/(2* sigma**2))
              derv_x = -(x/x1)*x2
              X.append(x)
              Y.append(y)
              Z.append(derv_x)
  
  surface_plot_3d(np.array(X),np.array(Y),np.array(Z))

first_order_derivative_X_gaussian_kernel(5,0.5)<jupyter_output><empty_output><jupyter_text>1.b First Order Partial Derivative Y Gaussian Kernel<jupyter_code>def first_order_derivative_Y_gaussian_kernel(filter_size,sigma):
# preferred sigma = 0.5
# preferred filter size = 5
  m = filter_size//2
  n = filter_size//2
  X=[]
  Y=[]
  Z=[]
  for x in range(-m, m+1):
          for y in range(-n, n+1):
              x1 = 2*np.pi*(sigma**4)
              x2 = np.exp(-(x**2 + y**2)/(2* sigma**2))
              derv_y = -(y/x1)*x2
              X.append(x)
              Y.append(y)
              Z.append(derv_y)
  
  surface_plot_3d(np.array(X),np.array(Y),np.array(Z))

first_order_derivative_Y_gaussian_kernel(5,0.5)<jupyter_output><empty_output><jupyter_text>2.a Second Order Partial Derivative X Gaussian Kernel<jupyter_code>def second_order_derivative_X_gaussian_kernel(filter_size,sigma):
# preferred sigma = 0.5
# preferred filter size = 5
  m = filter_size//2
  n = filter_size//2
  X=[]
  Y=[]
  Z=[]
  for x in range(-m, m+1):
          for y in range(-n, n+1):
              x1 = 2*np.pi*(sigma**6)
              x2 = np.exp(-(x**2 + y**2)/(2* sigma**2))
              num = (x**2-sigma**2)
              derv_x = (num/x1)*x2
              X.append(x)
              Y.append(y)
              Z.append(derv_x)
  surface_plot_3d(np.array(X),np.array(Y),np.array(Z))

second_order_derivative_X_gaussian_kernel(5,0.5)<jupyter_output><empty_output><jupyter_text>2.b Second Order Partial Derivative Y Gaussian Kernel<jupyter_code>def second_order_derivative_Y_gaussian_kernel(filter_size,sigma):
# preferred sigma = 0.5
# preferred filter size = 5
  m = filter_size//2
  n = filter_size//2
  X=[]
  Y=[]
  Z=[]
  for x in range(-m, m+1):
          for y in range(-n, n+1):
              x1 = 2*np.pi*(sigma**6)
              x2 = np.exp(-(x**2 + y**2)/(2* sigma**2))
              num = (y**2-sigma**2)
              derv_y = (num/x1)*x2
              X.append(x)
              Y.append(y)
              Z.append(derv_y)
  surface_plot_3d(np.array(X),np.array(Y),np.array(Z))

second_order_derivative_Y_gaussian_kernel(5,0.5)<jupyter_output><empty_output><jupyter_text>3. Difference of Gaussian Kernel Sigma1 = 4 & Sigma2 =7<jupyter_code>def difference_of_gaussian_kernel(filter_size,sigma_1,sigma_2):
# preferred filter size = 5
  m = filter_size//2
  n = filter_size//2
  X=[]
  Y=[]
  Z_1=[]
  Z_2=[]
  Z=[]
  for x in range(-m, m+1):
          for y in range(-n, n+1):
              x1 = 2*np.pi*(sigma_1**2)
              x2 = np.exp(-(x**2 + y**2)/(2* sigma_1**2))
              x3 = 2*np.pi*(sigma_2**2)
              x4 = np.exp(-(x**2 + y**2)/(2* sigma_2**2))
              val_1 = (1/x1)*x2
              val_2 = (1/x3)*x4
              X.append(x)
              Y.append(y)
              Z_1.append(val_1)
              Z_2.append(val_2)
  
  for n in range(25):
    Z.append(Z_1[n]-Z_2[n])
  
  surface_plot_3d(np.array(X),np.array(Y),np.array(Z))
  print(np.array(X))
  print(np.array(Y))
  print(np.array(Z))

value = difference_of_gaussian_kernel(5,4,7)

print(value)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15095087163232965, 'fraction_numerical': 0.03545958795562599, 'mean_word_length': 2.8958333333333335, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '548062', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 2081, 'n_tokens_neox': 2006, 'n_words': 373, 'path': 'visualize_gaussian_derivates.ipynb', 'repo_name': 'muhammadsharjeelbutt/Image-Processing-Algorithms', 'size': '5048'}