text: # -*- coding: utf-8 -*-
  """
  Created on Tue Sep 29 21:41:13 2020
  
  @author: HGARG
  """
  
  
  import numpy as np
  import cv2
  import matplotlib.pyplot as plt
  import matplotlib.image as mpimg
  import pickle
  
  
  # Read in an image
  image = mpimg.imread('signs_vehicles_xygrad.png')
  
  # Define a function that applies Sobel x and y, 
  # then computes the direction of the gradient
  # and applies a threshold.
  def dir_threshold(img, sobel_kernel=3, thresh=(0, np.pi/2)):
      
      # Apply the following steps to img
      # 1) Convert to grayscale
      gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
      # 2) Take the gradient in x and y separately
      sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0 , ksize = sobel_kernel)
      sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1 , ksize = sobel_kernel)
      # 3) Take the absolute value of the x and y gradients
      abs_sobelx = np.absolute(sobelx)
      abs_sobely = np.absolute(sobely)
      # 4) Use np.arctan2(abs_sobely, abs_sobelx) to calculate the direction of the gradient
      dir_grad = np.arctan2(abs_sobely, abs_sobelx)
      # 5) Create a binary mask where direction thresholds are met
      binary_output = np.zeros_like(dir_grad)
      binary_output[(dir_grad > thresh[0])&(dir_grad < thresh[1])] = 1
      # 6) Return this mask as your binary_output image
      #binary_output = np.copy(img) # Remove this line
      return binary_output
      
  # Run the function
  dir_binary = dir_threshold(image, sobel_kernel=15, thresh=(0.7, 1.3))
  # Plot the result
  f, (ax1, ax2) = plt.subplots(1, 2, figsize=(24, 9))
  f.tight_layout()
  ax1.imshow(image)
  ax1.set_title('Original Image', fontsize=50)
  ax2.imshow(dir_binary, cmap='gray')
  ax2.set_title('Thresholded Grad. Dir.', fontsize=50)
  plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.)
  
  