Skip to content

Instantly share code, notes, and snippets.

@jenncross
Created April 8, 2019 18:29
Show Gist options
  • Save jenncross/b6a0240f355e6aa3da8c4db537d2e3bf to your computer and use it in GitHub Desktop.
Save jenncross/b6a0240f355e6aa3da8c4db537d2e3bf to your computer and use it in GitHub Desktop.
Edge Detection Examples
"""
Created on Mon Apr 8 12:32:37 2019
@author: jenncross
"""
import imageio
from scipy import ndimage
import matplotlib.pyplot as plt
import numpy as np
"""SQUARE"""
im = np.zeros((128,128))
im[50:100, 50:100] = 1
plt.figure()
plt.imshow(im, cmap="gray") #
plt.show()
kern = np.array([[-1, 0, 1],[-1, 0, 1],[-1, 0, 1]])
edges = ndimage.convolve(im, kern)
plt.figure()
plt.imshow(edges, cmap="bwr")
plt.show()
kern2 = np.array([[-1, -1,-1],[0, 0, 0],[1, 1, 1]])
edges2 = ndimage.convolve(im, kern2)
plt.figure()
plt.imshow(edges2, cmap="bwr")
plt.show()
"""WIKIPEDIA"""
im = np.array(imageio.imread('http://upload.wikimedia.org/wikipedia/commons/d/de/Wikipedia_Logo_1.0.png'), dtype = "int64")
plt.figure()
plt.imshow(im, cmap="gray") #colors here: https://matplotlib.org/examples/color/colormaps_reference.html
plt.show()
edges = ndimage.convolve(im, kern)
plt.figure()
plt.imshow(edges, cmap="bwr")
plt.show()
edges2 = ndimage.convolve(im, kern2)
plt.figure()
plt.imshow(edges2, cmap="bwr")
plt.show()
"""ZEBRA"""
im = np.array(imageio.imread('http://upload.wikimedia.org/wikipedia/commons/e/ed/Zebra_in_black_and_white.jpg', as_gray=True), dtype = "int64")
plt.figure()
plt.imshow(im, cmap="gray") #colors here: https://matplotlib.org/examples/color/colormaps_reference.html
plt.show()
edges = ndimage.convolve(im, kern)
plt.figure()
plt.imshow(edges, cmap="bwr")
plt.show()
edges2 = ndimage.convolve(im, kern2)
plt.figure()
plt.imshow(edges2, cmap="bwr")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment