Simple python demos of Data-driven Color Manifolds [Nguyen et al. 2015].
They used Self-Organizing Maps (SOM) to construct data-driven color manifolds.
Overview of their approach:
- Color samples from image datasets.
- Dimensionality reduction via SOM.
- Applications: color editing, palettes, stylization, etc.
In this demo package, I implemented color sampling part and dimensionality reduction part for observing the color manifolds of image datasets.
- Hist3D class:
som_cm/core/hist_3d.py
.- Color sampling with 3D color histograms.
- SOM class:
som_cm/core/som.py
- SOM dimensionality reduction code.
In the following demo, I plotted 1D and 2D color manifolds by changing the SOMParam
.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from som_cm.io_util.image import loadRGB
from som_cm.core.hist_3d import Hist3D
from som_cm.core.som import SOMParam, SOM, SOMPlot
# Load image.
image = loadRGB(image_file)
# Color samples from 3D color histograms.
hist3D = Hist3D(image, num_bins=16)
color_samples = hist3D.colorCoordinates()
# Generate random data samples from color samples.
random_seed=100
num_samples=1000
random_ids = np.random.randint(len(color_samples) - 1, size=num_samples)
samples = color_samples[random_ids]
# 2D SOM: 32 x 32 map size.
param2D = SOMParam(h=32, dimension=2)
som2D = SOM(samples, param2D)
# Compute training process.
som2D.trainAll()
# SOM plotter.
som2D_plot = SOMPlot(som2D)
fig = plt.figure()
# Plot image.
fig.add_subplot(131)
plt.imshow(image)
plt.axis('off')
# Plot 2D SOM.
fig.add_subplot(132)
som2D_plot.updateImage()
plt.axis('off')
# Plot 2D SOM result in 3D RGB color space.
ax = fig.add_subplot(133, projection='3d')
som2D_plot.plot3D(ax)
plt.show()
Complete example code is available: som_cm/results/single_image.py
Complete example code is available: som_cm/results/animation.py
In the following demo, I plotted 1D and 2D color manifolds for multi-images.
Complete example code is available: som_cm/results/multi_images.py
Note: This program was only tested on Windows with Python2.7. Linux and Mac OS are not officially supported, but the following instructions might be helpful for installing on those environments.
Please install the following required python modules.
- NumPy
- SciPy
- matplotlib
- OpenCV
As these modules are heavily dependent on NumPy modules, please install appropriate packages for your development environment (Python versions, 32-bit or 64-bit). For 64-bit Windows, you can download the binaries from Unofficial Windows Binaries for Python Extension Packages.
You can use pip command for installing main modules. Please run the following command from the shell.
> pip install git+https://github.com/tody411/SOM-ColorManifolds.git
You can test the SOM demo with the following command from som_cm
directory.
> python main.py
This command will start downloading test images via Google Image API then run the demo module to generate result images.
som_cm/results
: You can find example codes to generate result images.
The MIT License 2015 (c) tody