Skip to content

Instantly share code, notes, and snippets.

@stefanv
Created June 26, 2017 20:56
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefanv/05c57153a544bad6f3dae06c2c69abd2 to your computer and use it in GitHub Desktop.
Save stefanv/05c57153a544bad6f3dae06c2c69abd2 to your computer and use it in GitHub Desktop.
Colorfulness using scikit-image's regionprops
from skimage import data, segmentation, measure, color, img_as_float
import numpy as np
import matplotlib.pyplot as plt
image = img_as_float(data.chelsea())
# or use io.imread(filename) to read your own
segments = segmentation.slic(image, slic_zero=True)
regions = measure.regionprops(segments)
colorfulness = np.zeros(image.shape[:2])
for region in regions:
# Grab all the pixels from this region
# Return an (N, 3) array
coords = tuple(region.coords.T)
values = image[coords]
R, G, B = values.T
# Calculate colorfulness
rg = np.abs(R - G)
yb = np.abs(0.5 * (R + G) - B)
std_root = np.sqrt(np.std(rg) ** 2 + np.std(yb) ** 2)
mean_root = np.sqrt(np.mean(rg) ** 2 + np.mean(yb) ** 2)
colorfulness[coords] = std_root + (0.3 * mean_root)
hsv = color.rgb2hsv(image)
hsv[..., 2] *= colorfulness / colorfulness.max()
plt.imshow(color.hsv2rgb(hsv))
plt.show()
@stefanv
Copy link
Author

stefanv commented Jun 26, 2017

chelsea_colorfulness

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment