def adjust_gamma(image, gamma=1, gain=1): """Performs Gamma Correction on the input image. Also known as Power Law Transform. This function transforms the input image pixelwise according to the equation ``O = I**gamma`` after scaling each pixel to the range 0 to 1. Parameters ---------- image : ndarray Input image. gamma : float Non negative real number. Default value is 1. gain : float The constant multiplier. Default value is 1. Returns ------- out : ndarray Gamma corrected output image. See Also -------- adjust_log Notes ----- For gamma greater than 1, the histogram will shift towards left and the output image will be darker than the input image. For gamma less than 1, the histogram will shift towards right and the output image will be brighter than the input image. References ---------- .. [1] http://en.wikipedia.org/wiki/Gamma_correction Examples -------- >>> from skimage import data, exposure, img_as_float >>> image = img_as_float(data.moon()) >>> gamma_corrected = exposure.adjust_gamma(image, 2) >>> # Output is darker for gamma > 1 >>> image.mean() > gamma_corrected.mean() True """ _assert_non_negative(image) dtype = image.dtype.type if gamma < 0: raise ValueError("Gamma should be a non-negative real number.") scale = float(dtype_limits(image, True)[1] - dtype_limits(image, True)[0]) out = ((image / scale)**gamma) * scale * gain return dtype(out)
def adjust_gamma(image, gamma=1, gain=1): """Performs Gamma Correction on the input image. Also known as Power Law Transform. This function transforms the input image pixelwise according to the equation ``O = I**gamma`` after scaling each pixel to the range 0 to 1. Parameters ---------- image : ndarray Input image. gamma : float Non negative real number. Default value is 1. gain : float The constant multiplier. Default value is 1. Returns ------- out : ndarray Gamma corrected output image. See Also -------- adjust_log Notes ----- For gamma greater than 1, the histogram will shift towards left and the output image will be darker than the input image. For gamma less than 1, the histogram will shift towards right and the output image will be brighter than the input image. References ---------- .. [1] http://en.wikipedia.org/wiki/Gamma_correction Examples -------- >>> from skimage import data, exposure, img_as_float >>> image = img_as_float(data.moon()) >>> gamma_corrected = exposure.adjust_gamma(image, 2) >>> # Output is darker for gamma > 1 >>> image.mean() > gamma_corrected.mean() True """ _assert_non_negative(image) dtype = image.dtype.type if gamma < 0: raise ValueError("Gamma should be a non-negative real number.") scale = float(dtype_limits(image, True)[1] - dtype_limits(image, True)[0]) out = ((image / scale) ** gamma) * scale * gain return dtype(out)
def adjust_sigmoid(image, cutoff=0.5, gain=10, inv=False): """Performs Sigmoid Correction on the input image. Also known as Contrast Adjustment. This function transforms the input image pixelwise according to the equation ``O = 1/(1 + exp*(gain*(cutoff - I)))`` after scaling each pixel to the range 0 to 1. Parameters ---------- image : ndarray Input image. cutoff : float Cutoff of the sigmoid function that shifts the characteristic curve in horizontal direction. Default value is 0.5. gain : float The constant multiplier in exponential's power of sigmoid function. Default value is 10. inv : bool If True, returns the negative sigmoid correction. Defaults to False. Returns ------- out : ndarray Sigmoid corrected output image. See Also -------- adjust_gamma References ---------- .. [1] Gustav J. Braun, "Image Lightness Rescaling Using Sigmoidal Contrast Enhancement Functions", http://www.cis.rit.edu/fairchild/PDFs/PAP07.pdf """ _assert_non_negative(image) dtype = image.dtype.type scale = float(dtype_limits(image, True)[1] - dtype_limits(image, True)[0]) if inv: out = (1 - 1 / (1 + np.exp(gain * (cutoff - image / scale)))) * scale return dtype(out) out = (1 / (1 + np.exp(gain * (cutoff - image / scale)))) * scale return dtype(out)
def adjust_gamma(image, gamma=1, gain=1): """Performs Gamma Correction on the input image. Also known as Power Law Transform. This function transforms the input image pixelwise according to the equation ``O = I**gamma`` after scaling each pixel to the range 0 to 1. Parameters ---------- image : ndarray Input image. gamma : float Non negative real number. Default value is 1. gain : float The constant multiplier. Default value is 1. Returns ------- out : ndarray Gamma corrected output image. Notes ----- For gamma greater than 1, the histogram will shift towards left and the output image will be darker than the input image. For gamma less than 1, the histogram will shift towards right and the output image will be brighter than the input image. References ---------- .. [1] http://en.wikipedia.org/wiki/Gamma_correction """ _assert_non_negative(image) dtype = image.dtype.type if gamma < 0: return "Gamma should be a non-negative real number" scale = float(dtype_limits(image, True)[1] - dtype_limits(image, True)[0]) out = ((image / scale) ** gamma) * scale * gain return dtype(out)
def adjust_gamma(image, gamma=1, gain=1): """Performs Gamma Correction on the input image. Also known as Power Law Transform. This function transforms the input image pixelwise according to the equation ``O = I**gamma`` after scaling each pixel to the range 0 to 1. Parameters ---------- image : ndarray Input image. gamma : float Non negative real number. Default value is 1. gain : float The constant multiplier. Default value is 1. Returns ------- out : ndarray Gamma corrected output image. Notes ----- For gamma greater than 1, the histogram will shift towards left and the output image will be darker than the input image. For gamma less than 1, the histogram will shift towards right and the output image will be brighter than the input image. References ---------- .. [1] http://en.wikipedia.org/wiki/Gamma_correction """ _assert_non_negative(image) dtype = image.dtype.type if gamma < 0: return "Gamma should be a non-negative real number" scale = float(dtype_limits(image, True)[1] - dtype_limits(image, True)[0]) out = ((image / scale)**gamma) * scale * gain return dtype(out)
def adjust_log(image, gain=1, inv=False): """Performs Logarithmic correction on the input image. This function transforms the input image pixelwise according to the equation ``O = gain*log(1 + I)`` after scaling each pixel to the range 0 to 1. For inverse logarithmic correction, the equation is ``O = gain*(2**I - 1)``. Parameters ---------- image : ndarray Input image. gain : float The constant multiplier. Default value is 1. inv : float If True, it performs inverse logarithmic correction, else correction will be logarithmic. Defaults to False. Returns ------- out : ndarray Logarithm corrected output image. See Also -------- adjust_gamma References ---------- .. [1] http://www.ece.ucsb.edu/Faculty/Manjunath/courses/ece178W03/EnhancePart1.pdf """ _assert_non_negative(image) dtype = image.dtype.type scale = float(dtype_limits(image, True)[1] - dtype_limits(image, True)[0]) if inv: out = (2**(image / scale) - 1) * scale * gain return dtype(out) out = np.log2(1 + image / scale) * scale * gain return dtype(out)
def adjust_log(image, gain=1, inv=False): """Performs Logarithmic correction on the input image. This function transforms the input image pixelwise according to the equation ``O = gain*log(1 + I)`` after scaling each pixel to the range 0 to 1. For inverse logarithmic correction, the equation is ``O = gain*(2**I - 1)``. Parameters ---------- image : ndarray Input image. gain : float The constant multiplier. Default value is 1. inv : float If True, it performs inverse logarithmic correction, else correction will be logarithmic. Defaults to False. Returns ------- out : ndarray Logarithm corrected output image. See Also -------- adjust_gamma References ---------- .. [1] http://www.ece.ucsb.edu/Faculty/Manjunath/courses/ece178W03/EnhancePart1.pdf """ _assert_non_negative(image) dtype = image.dtype.type scale = float(dtype_limits(image, True)[1] - dtype_limits(image, True)[0]) if inv: out = (2 ** (image / scale) - 1) * scale * gain return dtype(out) out = np.log2(1 + image / scale) * scale * gain return dtype(out)
def check_contrast(image, fraction_threshold=0.20, lower_percentile=1, upper_percentile=99, method='linear'): """Detemine if an image is low contrast. Parameters ---------- image : array-like The image under test. fraction_threshold : float, optional The low contrast fraction threshold. An image is considered low- contrast when its range of brightness spans less than this fraction of its data type's full range. [1]_ lower_percentile : float, optional Disregard values below this percentile when computing image contrast. upper_percentile : float, optional Disregard values above this percentile when computing image contrast. method : str, optional The contrast determination method. Right now the only available option is "linear". Returns ------- out : bool True when the image is determined to be low contrast. References ---------- .. [1] http://scikit-image.org/docs/dev/user_guide/data_types.html Examples -------- >>> image = np.linspace(0, 0.04, 100) >>> is_low_contrast(image) True >>> image[-1] = 1 >>> is_low_contrast(image) True >>> is_low_contrast(image, upper_percentile=100) False """ image = np.asanyarray(image) if image.ndim == 3 and image.shape[2] in [3, 4]: image = rgb2gray(image) dlimits = dtype_limits(image, clip_negative=False) limits = np.percentile(image, [lower_percentile, upper_percentile]) ratio = (limits[1] - limits[0]) / (dlimits[1] - dlimits[0]) return ratio, ratio < fraction_threshold
def imshow_all(*images, **kwargs): """ Plot a series of images side-by-side. Convert all images to float so that images have a common intensity range. Parameters ---------- limits : str Control the intensity limits. By default, 'image' is used set the min/max intensities to the min/max of all images. Setting `limits` to 'dtype' can also be used if you want to preserve the image exposure. titles : list of str Titles for subplots. If the length of titles is less than the number of images, empty strings are appended. kwargs : dict Additional keyword-arguments passed to `imshow`. """ images = [img_as_float(img) for img in images] hide_ticks = kwargs.pop('titles', []) titles = kwargs.pop('titles', []) if len(titles) != len(images): titles = list(titles) + [''] * (len(images) - len(titles)) limits = kwargs.pop('limits', 'image') if limits == 'image': kwargs.setdefault('vmin', min(img.min() for img in images)) kwargs.setdefault('vmax', max(img.max() for img in images)) elif limits == 'dtype': vmin, vmax = dtype_limits(images[0]) kwargs.setdefault('vmin', vmin) kwargs.setdefault('vmax', vmax) nrows, ncols = kwargs.get('shape', (1, len(images))) size = nrows * kwargs.pop('size', 5) width = size * len(images) if nrows > 1: width /= nrows * 1.33 fig, axes = plt.subplots(nrows=nrows, ncols=ncols, figsize=(width, size)) for ax, img, label in zip(axes.ravel(), images, titles): ax.imshow(img, **kwargs) # bare = kwargs.pop('bare', []) # if bare == True: # ax.xaxis.set_visible(False) # ax.yaxis.set_visible(False) ax.set_title(label)
def imshow_all(*images, **kwargs): """ Plot a series of images side-by-side. Convert all images to float so that images have a common intensity range. Parameters ---------- limits : str Control the intensity limits. By default, 'image' is used set the min/max intensities to the min/max of all images. Setting `limits` to 'dtype' can also be used if you want to preserve the image exposure. titles : list of str Titles for subplots. If the length of titles is less than the number of images, empty strings are appended. kwargs : dict Additional keyword-arguments passed to `imshow`. """ images = [img_as_float(img) for img in images] titles = kwargs.pop('titles', []) if len(titles) != len(images): titles = list(titles) + [''] * (len(images) - len(titles)) limits = kwargs.pop('limits', 'image') if limits == 'image': kwargs.setdefault('vmin', min(img.min() for img in images)) kwargs.setdefault('vmax', max(img.max() for img in images)) elif limits == 'dtype': vmin, vmax = dtype_limits(images[0]) kwargs.setdefault('vmin', vmin) kwargs.setdefault('vmax', vmax) nrows, ncols = kwargs.get('shape', (1, len(images))) size = nrows * kwargs.pop('size', 5) width = size * len(images) if nrows > 1: width /= nrows * 1.33 fig, axes = plt.subplots(nrows=nrows, ncols=ncols, figsize=(width, size)) for ax, img, label in zip(axes.ravel(), images, titles): ax.imshow(img, **kwargs) ax.set_title(label) ax.set_axis_off()