def preprocess(image, use_rgb=False, use_hsv=False, norm=True):
"""
Preprocesses a RGB image before extracting super-regions from it. Improves
the quality of the super-regions by transforming to the L*a*b colorspace
and normalizing the image.
Args:
image: numpy (H, W) or (H, W, 3) array
RGB image to be preprocessed.
use_rgb: bool
Wether to append RGB channels to the L*a*b channels.
use_hsv: bool
Wether to append HSV channels to the L*a*b channels.
norm:
Wether to standardize individual channels.
Result:
result: numpy (H * W, K) array
Where K is 3, 6 or depending on `use_rgb` and `use_hsv`. channel
specific normalization to enhance distances.
"""
if image.ndim == 2 or image.shape[2] == 1:
data = (np.squeeze(image) - image.mean()) / image.std()
return data
assert image.shape[2] == 3, 'Error: invalid image format'
result = color.rgb2lab(image).reshape(-1, 3)
if use_rgb:
result = np.column_stack(result, image.reshape(-1, 3))
if use_hsv:
result = np.column_stack(result, color.rgb2hsv(data).reshape(-1, 3))
# Standardize channels and reshape in-place
if norm:
result = (result - result.mean(0)) / result.std(0)
return result.astype(np.float32)
评论列表
文章目录