def grad_gauss_energy_per_region(omega_coord, affine_omega_coord, omega_mean, omega_std, image, image_gradient):
    b = 1 / (np.power(omega_std, 2))
    aux = utils.evaluate_image(omega_coord, image) - omega_mean
    image_gradient_by_point = b * np.array([utils.evaluate_image(omega_coord, image_gradient[0]),
                                            utils.evaluate_image(omega_coord, image_gradient[1])])
    grad = gradient_gauss_energy_for_each_vertex(aux, affine_omega_coord, image_gradient_by_point)
    return grad
def get_hsi_derivatives(coordinates, image):
    '''
    Gets neighboring pixels of an array of pixels.
    :param coordinates (numpy array):
    :return returns 8 arrays of coordinate pixels:
    '''
    import utils

    x = np.zeros([len(coordinates), 3, 3])
    hsi_im = image.hsi_image[:, :, 0]
    hsi_im_border = np.pad(hsi_im, (1, 1), 'reflect')
    for i in xrange(-1, 2):
        for j in xrange(-1, 2):
            x[:, i + 1, j + 1] = directed_hue_color_distance(
                utils.evaluate_image(coordinates, hsi_im, outside_value=0.),
                utils.evaluate_image(coordinates + [i + 1, j + 1], hsi_im_border, outside_value=0.)
            )
    dx = np.array([
        [-1, -2, -1],
        [0, 0, 0],
        [1, 2, 1]
    ])
    dy = np.array([
        [-1, 0, 1],
        [-2, 0, 2],
        [-1, 0, 1]
    ])
    derivative_x = np.array([sum(sum(np.transpose(x * dx)))])
    derivative_y = np.array([sum(sum(np.transpose(x * dy)))])
    derivative = np.concatenate((derivative_x, derivative_y))
    return derivative
def mean_energy_grad_per_region(omega_coord, affine_omega_coord, image, image_gradient):
    # E_mean
    omega_mean, omega_std = get_omega_mean(omega_coord, image)
    aux = utils.evaluate_image(omega_coord, image, omega_mean) - omega_mean
    image_gradient_by_point = [utils.evaluate_image(omega_coord, image_gradient[0], 0),
                               utils.evaluate_image(omega_coord, image_gradient[1], 0)]
    mean_derivative = np.dot(image_gradient_by_point, affine_omega_coord) / float(len(omega_coord))
    grad = gradient_energy_for_each_vertex(aux, affine_omega_coord, image_gradient_by_point, mean_derivative)
    return grad  # *(1/pow(omega_std, 2)) for GAUSS
def grad_gauss_energy_per_region(omega_coord,
                                 affine_omega_coord,
                                 gmm,
                                 image,
                                 image_gradient,
                                 smallest_num=np.exp(-200)):
    means = np.array([m for m in gmm.means_])
    covars = np.array([v for v in gmm.covars_])
    weights = np.array([w for w in gmm.weights_]).T

    x_ = utils.evaluate_image(omega_coord, image)
    x_m = np.transpose(np.tile(x_, (1, 1, 1)), (1, 0, 2)) - means

    denom = np.linalg.inv(covars)
    # x_m_denom = np.dot(x_m, denom)
    # x_m_denom = np.array([x_m_denom[:, i, i, :] for i in xrange(len(weights))])
    x_m_denom = np.array(
        [np.dot(X, M) for M, X in zip(denom, np.transpose(x_m, (1, 0, 2)))])

    aux = -np.sum(np.multiply(np.transpose(x_m_denom,
                                           (1, 0, 2)), x_m), axis=2) / 2.
    exp_x_m_denom_x_m = np.exp(aux)

    k = means.shape[1]
    coeff = 1 / (np.power(np.sqrt(2 * np.pi), k) *
                 np.sqrt(np.linalg.det(covars)))
    mixt = exp_x_m_denom_x_m * coeff
    unsummed_mixture_prob = mixt * weights

    mixture_prob = np.sum(unsummed_mixture_prob, axis=1)

    mixture_prob = avoid_zero_terms(mixture_prob, smallest_num=smallest_num)

    # caluculate 1/P(x)
    coeff_ = 1 / mixture_prob

    denom_x_m = np.array(
        [np.dot(M, X) for M, X in zip(denom, np.transpose(x_m, (1, 2, 0)))])
    # x_m_denom = np.transpose(np.array([np.dot(X, M) for M, X in zip(denom, np.transpose(x_m, (1, 0, 2)))]),(0,2,1))

    image_gradient_by_point = np.array([
        utils.evaluate_image(omega_coord, image_gradient[0]),
        utils.evaluate_image(omega_coord, image_gradient[1])
    ])
    image_gradient_by_point = np.transpose(image_gradient_by_point, (0, 2, 1))

    denom_x_m_derImage = -np.array(
        [np.sum(Mx * image_gradient_by_point, axis=1)
         for Mx in denom_x_m]) / 2.

    prod_2 = coeff_ * unsummed_mixture_prob.T

    prod_3 = np.multiply(prod_2, np.transpose(denom_x_m_derImage, (1, 0, 2)))
    prod_4 = np.sum(prod_3, axis=1)
    grad = np.dot(prod_4, affine_omega_coord).T
    return grad
def grad_gauss_energy_per_region(omega_coord, affine_omega_coord, gmm, image,
                                 image_gradient):
    means = np.array([m for m in gmm.means_])
    covars = np.array([v[0] for v in gmm.covars_])
    weights = np.array([w for w in gmm.weights_])

    x_m = utils.evaluate_image(omega_coord, image).T - means
    x_m = x_m.T
    # print x_m.shape
    x_m_squared = x_m * x_m
    denom = 2 * covars.T
    exp_aux = np.exp(-x_m_squared / denom)

    coeff = 1 / (np.sqrt(2 * np.pi) * np.sqrt(covars.T))
    mixt = coeff * exp_aux

    unsummed_mixture_prob = weights * mixt

    number_of_components = len(means)
    if number_of_components > 1:
        mixture_prob = np.array([np.sum(unsummed_mixture_prob, axis=1)]).T
    else:
        mixture_prob = unsummed_mixture_prob

    # Avoid logarithm of zero
    mixture_prob = avoid_zero_terms(mixture_prob)

    energy = np.sum(np.log(mixture_prob))

    # caluculate 1/P(x)
    coeff_ = 1 / mixture_prob

    # calculate the derivative of the mixture
    unsummed_mixture_derivative = unsummed_mixture_prob * (x_m / covars.T)
    if number_of_components > 1:
        mixture_derivative = np.array(
            [np.sum(unsummed_mixture_derivative, axis=1)]).T
    else:
        mixture_derivative = unsummed_mixture_derivative

    image_gradient_by_point = np.array([
        utils.evaluate_image(omega_coord, image_gradient[0]),
        utils.evaluate_image(omega_coord, image_gradient[1])
    ])
    prod_2 = coeff_ * mixture_derivative
    grad = gradient_gauss_energy_for_each_vertex(prod_2, affine_omega_coord,
                                                 image_gradient_by_point)

    plot_grad = False
    if plot_grad:
        show_direction(image, prod_2, omega_coord, affine_omega_coord,
                       image_gradient_by_point)
    return grad, energy
def grad_gauss_energy_per_region(omega_coord, affine_omega_coord, gmm, image, image_gradient, weight=[1, 1, 1]):
    # E_mean
    image_gradient_by_point = np.array([utils.evaluate_image(omega_coord, image_gradient[0], 0),
                                        utils.evaluate_image(omega_coord, image_gradient[1], 0)])
    omega_mean, omega_std = gmm.means_, gmm.covars_
    omega_std = omega_std[0]
    sigma = np.linalg.inv(omega_std)
    aux = utils.evaluate_image(omega_coord, image, omega_mean)
    aux -= omega_mean
    sigma_aux = np.dot(sigma, aux.T)

    prod_1 = np.sum(sigma_aux.T * np.transpose(image_gradient_by_point, (0, 1, 2)), axis=2)
    grad = np.dot(prod_1, affine_omega_coord).T
    return grad
def gauss_energy_per_region(omega_coord,
                            affine_omega_coord,
                            gmm,
                            image,
                            smallest_num=np.exp(-200)):
    means = np.array([m for m in gmm.means_])
    covars = np.array([v for v in gmm.covars_])
    weights = np.array([w for w in gmm.weights_]).T

    x_ = utils.evaluate_image(omega_coord, image)
    x_m = np.transpose(np.tile(x_, (1, 1, 1)), (1, 0, 2)) - means
    # print x_m.shape
    denom = np.linalg.inv(covars)
    # x_m_denom = np.dot(x_m, denom)
    # x_m_denom = np.array([x_m_denom[:, i, i, :] for i in xrange(len(weights))])
    x_m_denom = np.array(
        [np.dot(X, M) for M, X in zip(denom, np.transpose(x_m, (1, 0, 2)))])
    aux = -np.sum(np.multiply(np.transpose(x_m_denom,
                                           (1, 0, 2)), x_m), axis=2) / 2.
    exp_x_m_denom_x_m = np.exp(aux)

    k = means.shape[1]
    coeff = 1 / (np.power(np.sqrt(2 * np.pi), k) *
                 np.sqrt(np.linalg.det(covars)))
    mixt = exp_x_m_denom_x_m * coeff
    mixture_prob = mixt * weights
    if len(means) > 1:
        mixture_prob = np.sum(mixture_prob, axis=1)
    else:
        mixture_prob = mixture_prob
    mixture_prob = avoid_zero_terms(mixture_prob, smallest_num=smallest_num)
    energy = np.sum(np.log(mixture_prob))
    return energy
def grad_gauss_energy_per_region(omega_coord, affine_omega_coord, gmm, image, image_gradient):
    means = np.array([m for m in gmm.means_])
    covars = np.array([v[0] for v in gmm.covars_])
    weights = np.array([w for w in gmm.weights_])

    x_m = utils.evaluate_image(omega_coord, image).T - means
    x_m = x_m.T
    # print x_m.shape
    x_m_squared = x_m * x_m
    denom = 2 * covars.T
    exp_aux = np.exp(-x_m_squared / denom)

    coeff = 1 / (np.sqrt(2 * np.pi) * np.sqrt(covars.T))
    mixt = coeff * exp_aux

    unsummed_mixture_prob = weights * mixt

    number_of_components = len(means)
    if number_of_components > 1:
        mixture_prob = np.array([np.sum(unsummed_mixture_prob, axis=1)]).T
    else:
        mixture_prob = unsummed_mixture_prob

    # Avoid logarithm of zero
    mixture_prob = avoid_zero_terms(mixture_prob)

    energy = np.sum(np.log(mixture_prob))

    # caluculate 1/P(x)
    coeff_ = 1 / mixture_prob

    # calculate the derivative of the mixture
    unsummed_mixture_derivative = unsummed_mixture_prob * (x_m / covars.T)
    if number_of_components > 1:
        mixture_derivative = np.array([np.sum(unsummed_mixture_derivative, axis=1)]).T
    else:
        mixture_derivative = unsummed_mixture_derivative

    image_gradient_by_point = np.array([utils.evaluate_image(omega_coord, image_gradient[0]),
                                        utils.evaluate_image(omega_coord, image_gradient[1])])
    prod_2 = coeff_ * mixture_derivative
    grad = gradient_gauss_energy_for_each_vertex(prod_2, affine_omega_coord, image_gradient_by_point)

    plot_grad = False
    if plot_grad:
        show_direction(image, prod_2, omega_coord, affine_omega_coord, image_gradient_by_point)
    return grad, energy
示例#9
0
def gauss_energy_per_region(omega_coord, affine_omega_coord, image):
    x_ = utils.evaluate_image(omega_coord, image)
    omega_mean = np.mean(x_)
    omega_std = np.std(x_)

    aux = x_ - omega_mean
    a = len(aux) * np.log(omega_std)
    b = 1 / float(2 * np.power(omega_std, 2))
    region_energy = a + np.dot(aux, np.transpose(aux)) * b
    return region_energy
def gauss_energy_per_region(omega_coord, affine_omega_coord, gmm, image):
    omega_mean, omega_std = gmm.means_, gmm.covars_
    aux = utils.evaluate_image(omega_coord, image, omega_mean) - omega_mean
    omega_std = omega_std[0]
    k = len(omega_std)
    term_1 = len(aux) * k * np.log(2 * np.pi)
    term_2 = len(aux) * np.log(np.linalg.det(omega_std))
    sigma = np.linalg.inv(omega_std)
    x_sigma = np.dot(aux, sigma)
    term_3 = np.multiply(x_sigma, aux)
    term_3 = np.sum(term_3, axis=1)
    region_energy = term_1 + term_2 + sum(term_3)
    return region_energy
示例#11
0
def gauss_energy_per_region(omega_coord, affine_omega_coord, gmm, image):
    omega_mean, omega_std = gmm.means_, gmm.covars_
    aux = utils.evaluate_image(omega_coord, image, omega_mean) - omega_mean
    omega_std = omega_std[0]
    k = len(omega_std)
    term_1 = len(aux) * k * np.log(2 * np.pi)
    term_2 = len(aux) * np.log(np.linalg.det(omega_std))
    sigma = np.linalg.inv(omega_std)
    x_sigma = np.dot(aux, sigma)
    term_3 = np.multiply(x_sigma, aux)
    term_3 = np.sum(term_3, axis=1)
    region_energy = term_1 + term_2 + sum(term_3)
    return region_energy
示例#12
0
def grad_gauss_energy_per_region(omega_coord,
                                 affine_omega_coord,
                                 gmm,
                                 image,
                                 image_gradient,
                                 weight=[1, 1, 1]):
    # E_mean
    image_gradient_by_point = np.array([
        utils.evaluate_image(omega_coord, image_gradient[0], 0),
        utils.evaluate_image(omega_coord, image_gradient[1], 0)
    ])
    omega_mean, omega_std = gmm.means_, gmm.covars_
    omega_std = omega_std[0]
    sigma = np.linalg.inv(omega_std)
    aux = utils.evaluate_image(omega_coord, image, omega_mean)
    aux -= omega_mean
    sigma_aux = np.dot(sigma, aux.T)

    prod_1 = np.sum(sigma_aux.T * np.transpose(image_gradient_by_point,
                                               (0, 1, 2)),
                    axis=2)
    grad = np.dot(prod_1, affine_omega_coord).T
    return grad
def get_hsi_derivatives(coordinates, image):
    '''
    Gets neighboring pixels of an array of pixels.
    :param coordinates (numpy array):
    :return returns 8 arrays of coordinate pixels:
    '''
    import utils

    x = np.zeros([len(coordinates), 3, 3])
    hsi_im = image.hsi_image[:, :, 0]
    hsi_im_border = np.pad(hsi_im, (1, 1), 'reflect')
    for i in xrange(-1, 2):
        for j in xrange(-1, 2):
            x[:, i + 1, j + 1] = directed_hue_color_distance(
                utils.evaluate_image(coordinates, hsi_im, outside_value=0.),
                utils.evaluate_image(coordinates + [i + 1, j + 1],
                                     hsi_im_border,
                                     outside_value=0.))
    dx = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]])
    dy = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])
    derivative_x = np.array([sum(sum(np.transpose(x * dx)))])
    derivative_y = np.array([sum(sum(np.transpose(x * dy)))])
    derivative = np.concatenate((derivative_x, derivative_y))
    return derivative
def gauss_energy_per_region(omega_coord, affine_omega_coord, gmm, image):
    means = np.array([m for m in gmm.means_])
    covars = np.array([v for v in gmm.covars_])
    weights = np.array([np.round(w) for w in gmm.weights_])

    x_m = utils.evaluate_image(omega_coord, image) - means
    x_m = x_m.T
    x_m_squared = x_m * x_m
    denom = 2 * covars
    exp_aux = np.exp(-x_m_squared / denom)
    coeff = 1 / (np.sqrt(2 * np.pi) * np.sqrt(covars))
    mixt = coeff * exp_aux
    mixture_prob = weights * mixt

    # Avoid logarithm of zero
    mixture_prob = avoid_zero_terms(mixture_prob)

    energy = np.sum(np.log(mixture_prob))
    return energy
def gauss_energy_per_region(omega_coord, affine_omega_coord, gmm, image):
    means = np.array([m for m in gmm.means_])
    covars = np.array([v for v in gmm.covars_])
    weights = np.array([np.round(w) for w in gmm.weights_])

    x_m = utils.evaluate_image(omega_coord, image) - means
    x_m = x_m.T
    x_m_squared = x_m * x_m
    denom = 2 * covars
    exp_aux = np.exp(-x_m_squared / denom)
    coeff = 1 / (np.sqrt(2 * np.pi) * np.sqrt(covars))
    mixt = coeff * exp_aux
    mixture_prob = weights * mixt

    # Avoid logarithm of zero
    mixture_prob = avoid_zero_terms(mixture_prob)

    energy = np.sum(np.log(mixture_prob))
    return energy
示例#16
0
def mean_energy_per_region(omega_coord, affine_omega_coord, image):
    omega_mean, omega_std = get_omega_mean(omega_coord, image)
    aux = utils.evaluate_image(omega_coord, image, omega_mean) - omega_mean
    return np.dot(aux, np.transpose(aux))