Esempio n. 1
0
def contraharmonic_mean(img, size, Q):
    """Smooth the given image with contraharmonic mean filter
       of given size and Q."""
    data = img_to_array(img, dtype=np.float64)
    numerator = np.power(data, Q + 1)
    denominator = np.power(data, Q)
    kernel = np.full(size, 1.0)
    result = filter2d(numerator, kernel) / filter2d(denominator, kernel)
    return array_to_img(result, img.mode)
Esempio n. 2
0
def arithmetic_mean(img, size, raw=False):
    """Smooth the given image with arithmetic mean filter of given size."""
    m, n = size
    kernel = np.full((m, n), float(1) / (m * n))  # denominator
    data = img if raw else img_to_array(img)

    if raw:
        return filter2d(data, kernel)
    else:
        return array_to_img(filter2d(data, kernel), img.mode)
def gauss_noise(img, mean, var):
    """Add guassian noise to an image.

    Parameters
    -----------
    img: the input image
    mean: the mean of the guassian noise
    var: the standard variance of the gaussian noise
    """
    data = img_to_array(img)
    grand = Gauss(mean, var)  # the guassian random number generator

    def additive(z):  # generate f(x, y) + eta(x, y)
        return z + grand.next()

    vf = np.vectorize(additive)
    return array_to_img(vf(data), img.mode)
Esempio n. 4
0
def gauss_noise(img, mean, var):
    """Add guassian noise to an image.

    Parameters
    -----------
    img: the input image
    mean: the mean of the guassian noise
    var: the standard variance of the gaussian noise
    """
    data = img_to_array(img)
    grand = Gauss(mean, var)  # the guassian random number generator

    def additive(z):  # generate f(x, y) + eta(x, y)
        return z + grand.next()

    vf = np.vectorize(additive)
    return array_to_img(vf(data), img.mode)
Esempio n. 5
0
def geometric_mean(input_img, size):
    """Apply geometric mean filter to a 2-d image."""
    data = img_to_array(input_img, dtype=np.float64)
    M, N = data.shape  # M is height, N is width
    m, n = size  # m is height, n is width
    a, b = m / 2, n / 2

    def get_gmean(x, y):
        z = np.full(n * m, data[x, y])  # pad with border duplicates
        # fill in available neighborhood
        for i in xrange(x - a, x + a + 1):
            for j in xrange(y - b, y + b + 1):
                if i >= 0 and i < M and j >= 0 and j < N:
                    z[(i - x + a) * n + j - y + b] = data[i, j]
        # calculate power first to avoid overflow
        return np.prod(np.power(z, 1.0 / (m * n)))

    # apply to each pixel
    xx, yy = np.meshgrid(xrange(M), xrange(N), indexing='ij')
    vf = np.vectorize(get_gmean)
    return array_to_img(vf(xx, yy), input_img.mode)
def sap_noise(img, level=L, ps=None, pp=None):
    """Add salt-and/or-pepper noise to an image.

    Parameters
    -----------
    img: the input image
    level: intensity level, default to 256
    ps: probability of salt noise
    pp: probability of pepper noise
    """
    data = img_to_array(img)

    def salt(z, ps):
        return level - 1 if random() < ps else z

    def pepper(z, pp):
        return 0 if random() < pp else z

    def sap(z, ps, pp):
        p = random()
        if p < ps:
            return level - 1
        elif p > (1 - pp):
            return 0
        else:
            return z

    if ps and not pp:  # salt noise
        vf = np.vectorize(salt)
        return array_to_img(vf(data, ps), img.mode)
    elif pp and not pp:  # pepper noise
        vf = np.vectorize(pepper)
        return array_to_img(vf(data, pp), img.mode)
    elif ps and pp:  # salt-and-pepper noise
        vf = np.vectorize(sap)
        return array_to_img(vf(data, ps, pp), img.mode)
    else:
        raise Exception("No probability specified")
Esempio n. 7
0
def sap_noise(img, level=L, ps=None, pp=None):
    """Add salt-and/or-pepper noise to an image.

    Parameters
    -----------
    img: the input image
    level: intensity level, default to 256
    ps: probability of salt noise
    pp: probability of pepper noise
    """
    data = img_to_array(img)

    def salt(z, ps):
        return level - 1 if random() < ps else z

    def pepper(z, pp):
        return 0 if random() < pp else z

    def sap(z, ps, pp):
        p = random()
        if p < ps:
            return level - 1
        elif p > (1 - pp):
            return 0
        else:
            return z

    if ps and not pp:  # salt noise
        vf = np.vectorize(salt)
        return array_to_img(vf(data, ps), img.mode)
    elif pp and not pp:  # pepper noise
        vf = np.vectorize(pepper)
        return array_to_img(vf(data, pp), img.mode)
    elif ps and pp:  # salt-and-pepper noise
        vf = np.vectorize(sap)
        return array_to_img(vf(data, ps, pp), img.mode)
    else:
        raise Exception("No probability specified")
Esempio n. 8
0
def harmonic_mean(img, size):
    """Smooth the given image with harmonic mean filter of given size."""
    data = img_to_array(img, dtype=np.float64)
    inverse = np.reciprocal(data)
    result = np.reciprocal(arithmetic_mean(inverse, size, True))
    return array_to_img(result, img.mode)
Esempio n. 9
0
def min_filter(img, size):
    """Apply a min filter to a 2-d image."""
    data = img_to_array(img)
    result = stat_filter2d(data, size, 0)
    return array_to_img(result, img.mode)