Colorfulness using scikit-image's regionprops
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
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
stefanv commentedJun 26, 2017