Skip to content

Instantly share code, notes, and snippets.

@ritvikmath
Created February 24, 2020 05:31
Show Gist options
  • Save ritvikmath/069a6173e13d3788c0bca536ba59fcad to your computer and use it in GitHub Desktop.
Save ritvikmath/069a6173e13d3788c0bca536ba59fcad to your computer and use it in GitHub Desktop.
#read in the image
img = plt.imread('penguin.jpg')
#convert image to grayscale
greyImg = np.mean(img, axis=2).astype(np.uint8)
#the edge kernel, when convolved with our image, detects the edges
edge_kernel = np.array([[0,1,0],[1,-4,1],[0,1,0]])
#convolve edge kernel with image
edges_img = convolve(greyImg, edge_kernel, mode='valid')
#normalize
edges_img = (edges_img - edges_img.min())/(edges_img.max() - edges_img.min())
#only keep pixels which are a certain threshold away from the mean
tol = np.mean(edges_img) - 2.5*np.std(edges_img)
#polarize the pixels in the edges image to either 0 or 1
mask = edges_img < tol
edges_img[mask] = 0
edges_img[~mask] = 1
#show the polarized edges image
plt.imshow(edges_img, cmap='gray')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment