Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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