diff --git a/scikits/image/color/colorconv.py b/scikits/image/color/colorconv.py index cd3c3b63..c12791cf 100644 --- a/scikits/image/color/colorconv.py +++ b/scikits/image/color/colorconv.py @@ -14,9 +14,31 @@ __docformat__ = "restructuredtext en" import numpy as np def rgb2hsv(rgb): + """RGB to HSV color space conversion. - """ - RGB to HSV color space conversion + Parameters + ---------- + rgb : ndarray + The image in RGB format, in a 3-D array of shape (.., .., 3). + + Returns + ------- + out : ndarray + The image in HSV format, in a 3-D array of shape (.., .., 3). + + Raises + ------ + ValueError + If `rgb` is not a 3-D array of shape (.., .., 3). + + Examples + -------- + >>> import os + >>> from scikits.image import data_dir + >>> from scikits.image.io import imread + + >>> lena = imread(os.path.join(data_dir, 'lena.png')) + >>> lena_hsv = color.rgb2hsv(lena) """ if type(rgb) != np.ndarray: @@ -25,10 +47,10 @@ def rgb2hsv(rgb): if rgb.ndim != 3 or rgb.shape[2] != 3: msg = "the input array 'rgb' must be have a shape == (.,.,3))" raise ValueError, msg - + arr = rgb.astype("float32") out = np.empty_like(arr) - + # -- V channel out_v = arr.max(-1) @@ -39,15 +61,15 @@ def rgb2hsv(rgb): # -- H channel # red is max - idx = (arr[:,:,0] == out_v) + idx = (arr[:,:,0] == out_v) out[idx, 0] = (arr[idx, 1] - arr[idx, 2]) / delta[idx] # green is max - idx = (arr[:,:,1] == out_v) + idx = (arr[:,:,1] == out_v) out[idx, 0] = 2. + (arr[idx, 2] - arr[idx, 0] ) / delta[idx] # blue is max - idx = (arr[:,:,2] == out_v) + idx = (arr[:,:,2] == out_v) out[idx, 0] = 4. + (arr[idx, 0] - arr[idx, 1] ) / delta[idx] out_h = (out[:,:,0] / 6.) % 1.