Skip to content

Instantly share code, notes, and snippets.

@SaraM92
Created July 23, 2020 11:41
Show Gist options
  • Save SaraM92/6010553dcad1b59e0833cfd0a443f1aa to your computer and use it in GitHub Desktop.
Save SaraM92/6010553dcad1b59e0833cfd0a443f1aa to your computer and use it in GitHub Desktop.
#Import needed libraries
import numpy as np
import matplotlib.pyplot as plt
# Create three images with different features
plain_img = np.array([np.array([100, 100]), np.array([100, 100])])
img_with_edge = np.array([np.array([100, 0]), np.array([100, 0])])
#Create a kernal to detect vertical edges (sobel, gradient edge detecting kernal)
kernel_vertical = np.array([np.array([2, -2]), np.array([2, -2])])
#Funtion to apply a kernal to an image
# elementwise multiplication followed by a sum
def apply_kernel(img, kernel):
return True if(np.sum(np.multiply(img, kernel))!=0) else False
#---------------------------------------------------------------------------------
# Plain Image (image with no edges)
plt.imshow(plain_img)
plt.axis('off')
plt.show()
#Printing results of applying kernal 0 if an edge is not found
if apply_kernel(plain_img,kernel_vertical):
print("An edge has been found")
else:
print("No edges were found")
#---------------------------------------------------------------------------------
#Image with an edge
plt.imshow(img_with_edge)
plt.axis('off')
plt.show()
#Printing results of applying kernal 0 if an edge is not found
if apply_kernel(img_with_edge,kernel_vertical):
print("An edge has been found")
else:
print("No edges were found")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment