def test_gray2rgb():
    image_path = '../lena.jpg'
    img = Image.open(image_path).convert('L')
    img = np.array(img)
    print('input grayscale image has dimension of: '),
    print(img.shape)
    assert isgrayimage(img), 'the input image is not a gray image'
    visualize_image(img, vis=True)

    img_rgb = gray2rgb(img, with_color=True)
    print('converted rgb image has dimension of: '),
    print(img_rgb.shape)
    assert iscolorimage(img_rgb), 'the converted image is not a color image'
    visualize_image(img_rgb, vis=True)

    # test when input image is float image
    test_floatimage = (img.astype('float32')) / 255.
    img_rgb = gray2rgb(test_floatimage, with_color=True)
    assert iscolorimage(img_rgb), 'the converted image is not a color image'
    visualize_image(img_rgb, vis=True)

    # test when input image is PIL image
    test_pil_format_image = Image.fromarray(img)
    img_rgb = gray2rgb(test_pil_format_image, with_color=True)
    assert iscolorimage(img_rgb), 'the converted image is not a color image'

    print('\n\nDONE! SUCCESSFUL!!\n')
def image_clahe(input_image, clip_limit=2.0, grid_size=8, warning=True, debug=True):
	'''
	do contrast limited adative histogram equalization for an image: could be a color image or gray image
	the color space used for histogram equalization is LAB
	
	parameters:
		input_image:		a pil or numpy image

	outputs:
		clahe_image:		an uint8 numpy image (rgb or gray)
	'''
	np_image, _ = safe_image(input_image, warning=warning, debug=debug)
	if isfloatimage(np_image): np_image = (np_image.astype('float32') * 255.).astype('uint8')
	if debug: assert isuintimage(np_image), 'the input image should be a uint8 image'

	clahe = cv2.createCLAHE(clipLimit=clip_limit, tileGridSize=(grid_size, grid_size))

	if iscolorimage(np_image):
		lab_image = rgb2lab(np_image, warning=warning, debug=debug)
		input_data = lab_image[:, :, 0]			# extract the value channel
		clahe_lab_image = clahe.apply(input_data)
		lab_image[:, :, 0] = clahe_lab_image
		clahe_image = lab2rgb(lab_image, warning=warning, debug=debug)
	elif isgrayimage(np_image):
		clahe_image = clahe.apply(np_image)
	else: assert False, 'the input image is neither a color image or a grayscale image'

	return clahe_image
def image_hist_equalization_hsv(input_image, warning=True, debug=True):
	'''
	do histogram equalization for an image: could be a color image or gray image
	the color space used for histogram equalization is HSV
	
	parameters:
		input_image:		a pil or numpy image

	outputs:
		equalized_image:	an uint8 numpy image (rgb or gray)
	'''
	np_image, _ = safe_image(input_image, warning=warning, debug=debug)
	if isuintimage(np_image): np_image = np_image.astype('float32') / 255.
	if debug: assert isfloatimage(np_image), 'the input image should be a float image'

	if iscolorimage(np_image):
		hsv_image = rgb2hsv(np_image, warning=warning, debug=debug)
		input_data = hsv_image[:, :, 2]			# extract the value channel
		equalized_hsv_image = (hist_equalization(input_data, num_bins=256, debug=debug) * 255.).astype('uint8')
		hsv_image[:, :, 2] = equalized_hsv_image
		equalized_image = hsv2rgb(hsv_image, warning=warning, debug=debug)
	elif isgrayimage(np_image):
		equalized_image = (hist_equalization(np_image, num_bins=256, debug=debug) * 255.).astype('uint8')
	else: assert False, 'the input image is neither a color image or a grayscale image'

	return equalized_image
def image_rgb2bgr(input_image, warning=True, debug=True):
	'''
	this function converts a rgb image to a bgr image

	parameters:
		input_image:	a pil or numpy rgb image

	outputs:
		np_image:		a numpy bgr image
	'''
	np_image, _ = safe_image(input_image, warning=warning, debug=debug)
	if debug: assert iscolorimage(np_image), 'the input image is not a color image'
	
	np_image = np_image[:, :, ::-1] 			# convert RGB to BGR
	return np_image
def hsv2rgb(input_image, warning=True, debug=True):
	'''
	convert a hsv image to a rgb image using opencv package

	parameters:
		input_image:	an pil or numpy image

	output:
		rgb_img: 		an uint8 rgb numpy image
	'''
	np_image, _ = safe_image(input_image, warning=warning, debug=debug)
	if isfloatimage(np_image): np_image = (np_image * 255.).astype('uint8')	

	if debug:
		assert iscolorimage(np_image), 'the input image should be a rgb image'
		assert isuintimage(np_image), 'the input numpy image should be uint8 image in order to use opencv'

	rgb_img = cv2.cvtColor(np_image, cv2.COLOR_HSV2RGB)
	return rgb_img
def rgb2hsv_v2(input_image, warning=True, debug=True):
	'''
	convert a rgb image to a hsv image, using PIL package, not compatible with opencv package

	parameters:
		input_image:	an pil or numpy image

	output:
		hsv_image: 		an uint8 hsv numpy image
	'''
	np_image, _ = safe_image(input_image, warning=warning, debug=debug)
	if isfloatimage(np_image): np_image = (np_image * 255.).astype('uint8')	

	if debug:
		assert iscolorimage(np_image), 'the input image should be a rgb image'
		assert isuintimage(np_image), 'the input numpy image should be uint8 image in order to use PIL'

	pil_rgb_img = Image.fromarray(np_image)
	pil_hsv_img = pil_rgb_img.convert('HSV')
	hsv_img = np.array(pil_hsv_img)
	return hsv_img