Created
June 5, 2017 23:01
-
-
Save theodoregoetz/3ddfa7aa2666a6b95dc6e41805e8d85f to your computer and use it in GitHub Desktop.
colormap defined in cielab space using scikit-image
This file contains 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 matplotlib as mpl | |
mpl.use('wxAgg') | |
from collections import Iterable | |
from copy import copy | |
from matplotlib import pyplot | |
import numpy as np | |
from scipy import interpolate | |
from skimage import color | |
from matplotlib.colors import LinearSegmentedColormap | |
class SplineND(object): | |
def __init__(self, x, y, k=3, s=0, **kwargs): | |
y = np.asarray(y) | |
if not isinstance(k, Iterable): | |
k = [k] * len(y.T) | |
if not isinstance(s, Iterable): | |
s = [s] * len(y.T) | |
s = [si * len(x) for si in s] | |
self.splines = [] | |
for yi, ki, si in zip(y.T, k, s): | |
self.splines.append(interpolate.UnivariateSpline( | |
x, yi, k=ki, s=si, **kwargs)) | |
def __call__(self, x): | |
return np.array([s(x) for s in self.splines]).T | |
class LabColorMap(object): | |
def __init__(self, npoints=100): | |
xdim, ydim = npoints, 256 | |
self.x = np.linspace(0, 1, xdim) | |
self.ll = np.linspace( 0, 100, ydim).reshape((-1,1)) | |
self.aa = np.linspace(-128, 128, ydim).reshape((-1,1)) | |
self.bb = np.linspace(-128, 128, ydim).reshape((-1,1)) | |
self.spline_orders = [2, 3, 3] | |
self.smoothing = [50, 10, 10] | |
self.control_points = np.array([ | |
[ 1.22500263e+01, -1.11877281e-04, -2.08501245e-03], | |
[ 5.78254174e+01, -3.71519150e+01, -2.95909928e+00], | |
[ 4.62778614e+01, -3.24822705e+01, 4.89168388e+01], | |
[ 6.51147179e+01, 2.55361420e+01, 7.04871038e+01], | |
[ 7.29456546e+01, 1.81239956e+01, 7.66668101e+01], | |
[ 8.11730475e+01, -6.86684464e-01, -2.48256768e-01], | |
[ 9.99999845e+01, -4.59389408e-04, -8.56145792e-03]]) | |
self.control_x = np.linspace(0, 1, len(self.control_points)) | |
self._imdata = np.empty((ydim, xdim, 3)) | |
self._alpha = np.empty((ydim, xdim)) | |
self.update_data() | |
def alpha(self, data, alpha_at_limits=1.0): | |
self._alpha[...] = 1 | |
for channel in np.rollaxis(data, axis=-1): | |
self._alpha[channel<0.001] = alpha_at_limits | |
self._alpha[channel>0.999] = alpha_at_limits | |
return self._alpha | |
def imdata(self): | |
l, a, b = self.cielab_colors.T | |
extent = [self.x.min(), self.x.max(), self.ll.min(), self.ll.max()] | |
self._imdata[:,:,0] = self.ll | |
self._imdata[:,:,1] = a | |
self._imdata[:,:,2] = b | |
data = color.lab2rgb(self._imdata) | |
ret = [(np.dstack([data, self.alpha(data)]), copy(extent))] | |
extent[-2] = self.aa.min() | |
extent[-1] = self.aa.max() | |
self._imdata[:,:,0] = l | |
self._imdata[:,:,1] = self.aa | |
data = color.lab2rgb(self._imdata) | |
ret += [(np.dstack([data, self.alpha(data)]), copy(extent))] | |
extent[-2] = self.bb.min() | |
extent[-1] = self.bb.max() | |
self._imdata[:,:,1] = a | |
self._imdata[:,:,2] = self.bb | |
data = color.lab2rgb(self._imdata) | |
ret += [(np.dstack([data, self.alpha(data)]), copy(extent))] | |
return ret | |
def update_data(self): | |
self.spline = SplineND(self.control_x, self.control_points, | |
self.spline_orders, self.smoothing) | |
self.cielab_colors = self.spline(self.x) | |
def plot(self): | |
self.figure, self.axes = pyplot.subplots(4) | |
kw = dict(origin='lower', aspect='auto') | |
self.im = [] | |
for ax, (data, extent) in zip(self.axes, cmap.imdata()): | |
self.im.append(ax.imshow(data, extent=extent, **kw)) | |
ax.autoscale(False) | |
self.lines = [] | |
for ax, pts in zip(self.axes, np.asarray(self.control_points).T): | |
self.lines.append(ax.plot(self.control_x, pts, marker='o', lw=1, c='black')) | |
return self.figure, self.axes, self.im | |
def update_plot(self): | |
self.update_data() | |
for im, (data, extent) in zip(self.im, self.imdata()): | |
im.set_array(data) | |
im.set_extent(extent) | |
def mpl_cmap(self, name): | |
rgb = color.lab2rgb(self.cielab_colors.reshape(-1,1,3)) | |
return LinearSegmentedColormap.from_list(name, rgb.squeeze()) | |
def plot_comb(self): | |
nperiods, npoints, amplitude = 80, 50, 70 | |
x = np.linspace(0, nperiods * 2 * np.pi, npoints * nperiods) | |
y = np.linspace(0, amplitude, npoints * 10) | |
X, Y = np.meshgrid(x, y) | |
img = X + Y * np.sin(X) * (Y**2 / Y.max()**2) | |
ax = self.axes[3] | |
cmap = self.mpl_cmap('cmap') | |
ax.imshow(img, cmap=cmap, aspect='auto', origin='lower', vmin=x[0], vmax=x[-1]) | |
ax.set_title(cmap.name) | |
ax.set_axis_off() | |
cmap = LabColorMap() | |
cmap.a = lambda x: (80 - 15) * x + 15 | |
cmap.b = lambda x: -50 * x - 50 | |
fig, ax, im = cmap.plot() | |
cmap.a = lambda x: (80 - 15) * x + 15 | |
cmap.b = lambda x: 50 * x | |
cmap.update_plot() | |
cmap.plot_comb() | |
pyplot.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment