Created
November 2, 2016 16:45
-
-
Save franktoffel/f6a14fae5a931f046dda6a6b95cfcb59 to your computer and use it in GitHub Desktop.
A comparison between different methods to obtain out of the focus bokeh with Python (scikit image)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from skimage.morphology import disk | |
from skimage.filters.rank import mean | |
from skimage.filters import gaussian | |
import matplotlib.pyplot as plt | |
n = 20 | |
l = 256 | |
im = np.zeros((l, l)) | |
points = l * np.random.random((2, n ** 2)) | |
# Original | |
im[(points[0]).astype(np.int), (points[1]).astype(np.int)] = 1 | |
# Gaussian filter (as in iPhone's) | |
im_gauss = gaussian(im, sigma=3) #gauss with a 3 pixels as std | |
# Mean filter (more realistic bokeh!) | |
selection_element = disk(5) # matrix of n pixels with a disk shape | |
im_mean = mean(im, selection_element) | |
# Plotting code | |
fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(10, 6)) | |
axes[0].imshow(im, cmap=plt.cm.viridis) | |
axes[0].set_title('Original synthetic image', fontsize=10) | |
axes[0].set_axis_off() | |
axes[1].imshow(im_gauss, cmap=plt.cm.viridis) | |
axes[1].set_title('Gaussian blur filter \n(iPhone\'s 7 plus)', fontsize=10) | |
axes[1].set_axis_off() | |
axes[2].imshow(im_mean, cmap=plt.cm.viridis) | |
axes[2].set_title('Simulated bokeh \n(convultion by an uniform disk)', fontsize=10) | |
axes[2].set_axis_off() | |
plt.axis('off') | |
plt.show() |
Author
franktoffel
commented
Nov 2, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment