Skip to content

Instantly share code, notes, and snippets.

@ritvikmath
Last active May 10, 2021 13:47
Show Gist options
  • Save ritvikmath/36d914522f844b70358d1438efe45d86 to your computer and use it in GitHub Desktop.
Save ritvikmath/36d914522f844b70358d1438efe45d86 to your computer and use it in GitHub Desktop.
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
#define the vertical filter
vertical_filter = [[-1,-2,-1], [0,0,0], [1,2,1]]
#define the horizontal filter
horizontal_filter = [[-1,0,1], [-2,0,2], [-1,0,1]]
#read in the pinwheel image
img = plt.imread('pinwheel.jpg')
#get the dimensions of the image
n,m,d = img.shape
#initialize the edges image
edges_img = img.copy()
#loop over all pixels in the image
for row in range(3, n-2):
for col in range(3, m-2):
#create little local 3x3 box
local_pixels = img[row-1:row+2, col-1:col+2, 0]
#apply the vertical filter
vertical_transformed_pixels = vertical_filter*local_pixels
#remap the vertical score
vertical_score = vertical_transformed_pixels.sum()/4
#apply the horizontal filter
horizontal_transformed_pixels = horizontal_filter*local_pixels
#remap the horizontal score
horizontal_score = horizontal_transformed_pixels.sum()/4
#combine the horizontal and vertical scores into a total edge score
edge_score = (vertical_score**2 + horizontal_score**2)**.5
#insert this edge score into the edges image
edges_img[row, col] = [edge_score]*3
#remap the values in the 0-1 range in case they went out of bounds
edges_img = edges_img/edges_img.max()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment