# PIL
from PIL import ImageFilter
# Filters the images using EDGE_ENHANCE filter
img_gray = img_gray.filter(ImageFilter.EDGE_ENHANCE)
# Filters the images using FIND_EDGES filter
img_gray = img_gray.filter(ImageFilter.FIND_EDGES)
# OpenCV
import cv2
# Detect edge using thresholding : Returns ret which is the threshold used and outs which is the image
ret, outs = cv2.threshold(src = image, thresh = 0, maxval = 255, type = cv2.THRESH_OTSU+cv2.THRESH_BINARY_INV)
# ----------------------
# Detect edge using sobel
# Smoothen using GaussianBlur to reduce noise
img_gray = cv2.GaussianBlur(img_gray,(3,3),sigmaX=0.1,sigmaY=0.1)
# Renders the filtered image
plt.imshow(img_gray ,cmap='gray')
# Applys the filter on the image in the X direction
grad_x = cv2.Sobel(src=img_gray, ddepth=cv2.CV_16S, dx=1, dy=0, ksize=3)
# Applys the filter on the image in the Y direction
grad_y = cv2.Sobel(src=img_gray, ddepth=cv2.CV_16S, dx=0, dy=1, ksize=3)
# Converts the values back to a number between 0 and 255
abs_grad_x = cv2.convertScaleAbs(grad_x)
abs_grad_y = cv2.convertScaleAbs(grad_y)
# Adds the derivative in the X and Y direction
final_img = cv2.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0)