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_dimension(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_dimension(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
Beispiel #2
0
    def convolve(self, input_image):
        '''
		convolve the kernel with the input image, whatever the input image format is. If the input image 
		is a color image, the filter is expanded to a 3D shape
	
		parameters:
			input_image:		an pil or numpy, gray or color image

		outputs:
			filtered_image:		a float32 numpy image, shape is same as before
		'''
        np_image, _ = safe_image(input_image,
                                 warning=self.warning,
                                 debug=self.debug)
        if isuintimage(np_image): np_image = np_image.astype('float32') / 255.

        if self.debug:
            assert isfloatimage(
                np_image), 'the input image should be a float image'
            self.weights is not None, 'the kernel is not defined yet'

        if iscolorimage_dimension(np_image):
            self.weights = self.__expand_3d(
            )  # expand the filter to 3D for color image
        elif isgrayimage_dimension(np_image):
            np_image = np_image.reshape(
                np_image.shape[0],
                np_image.shape[1])  # squeeze the image dimension to 2
        else:
            assert False, 'the dimension of the image is not correct'

        filtered_image = ndimage.filters.convolve(np_image, self.weights)
        return filtered_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_dimension(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_dimension(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_dimension(
            np_image), 'the input image is not a color image'

    np_image = np_image[:, :, ::-1]  # convert RGB to BGR
    return np_image
def rgb2hsv(input_image, warning=True, debug=True):
    '''
	convert a rgb image to a hsv image using 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_dimension(
            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'

    hsv_img = cv2.cvtColor(np_image, cv2.COLOR_RGB2HSV)
    return hsv_img
def rgb2gray(input_image, warning=True, debug=True):
    '''
	convert a color image to a grayscale image (1-channel)
		
	parameters:
		input_image:	an pil or numpy image

	output:
		gray_image:		an uint8 HW gray 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_dimension(
            np_image), 'the input numpy image is not correct: {}'.format(
                np_image.shape)
        assert isuintimage(
            np_image
        ), 'the input numpy image should be uint8 image in order to use opencv'

    gray_image = cv2.cvtColor(np_image, cv2.COLOR_RGB2GRAY)
    return gray_image
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_dimension(
            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