Skip to content

Instantly share code, notes, and snippets.

@ulmangt
Created March 27, 2012 03:32
Show Gist options
  • Save ulmangt/2212279 to your computer and use it in GitHub Desktop.
Save ulmangt/2212279 to your computer and use it in GitHub Desktop.
Bumpfilter
# CSI 709 - Homework 4 - Stefan Novak
import numpy as np
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
# Configuration.
config = {'azimuth': np.radians(0.0),
'elevation': np.radians(35.0),
'ambient_factor': 0.30,
'constant_nz': 0.023}
config['compensation'] = np.sin(config['elevation'])
# Create our light vector from configuration.
light = np.array([np.cos(config['azimuth']) * np.cos(config['elevation']),
np.sin(config['azimuth']) * np.cos(config['elevation']),
np.sin(config['elevation'])])
# Read our data.
img = mpimg.imread('car.png')
# Determine luminosity at each pixel.
luminosity = img.dot([0.2126, 0.7152, 0.0722])
# Calculate the luminosity gradient.
normals = np.array(np.gradient(luminosity)).transpose(1,2,0)
# Dot those normals with the light vector.
shade = np.clip(normals.dot(light[:2])+light[-1]*config['constant_nz'], 0.0, 1.0) / np.sqrt(np.sum(normals**2, axis=2) + config['constant_nz']**2)
shade += np.clip(config['compensation'] - shade, 0.0, 1.0) * config['ambient_factor'];
# Apply shade and save!
plt.imshow(img*np.array(3*[shade]).transpose(1,2,0))
plt.savefig('car21.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment