def object_modifier(fill_light, edge_light, diffuse_light, output, mask,
                    fill=1, edge=1, diffuse=1, count=-1, verbose=False):
    """
    This function calculates the per object modifier for an image.

    Arguments:
    fill_light    -- path to the fill light image
    edge_light    -- path to the edge light image
    diffuse_light -- path to the diffuse color light image
    mask          -- the mask image that identifies the object of interest
    fill          -- the weight of fill light in the object
    edge          -- the weight of edge light in the object
    diffuse       -- the weight of diffuse light in the object
    count         -- the number of imags to use for this calculation
    verbose       -- should we print debug info
    """
    fill_image = utils.read_image(fill_light, normalize=True)
    edge_image = utils.read_image(edge_light, normalize=True)
    diffuse_image = utils.read_image(diffuse_light, normalize=True)
    mask_image = utils.read_image(mask, normalize=True)

    print fill_image
    modifier = modifier_lights.ModifierLights(verbose=verbose)
    res_image = modifier.per_object(fill_image, edge_image, diffuse_image,
                                    mask_image, fill, edge, diffuse)
    cv2.imwrite(output, utils.denormalize_img(res_image))
    cv2.imshow('Object modifier', res_image)
    cv2.waitKey(0)
def main():
    global img_list
    global gmap
    global weight

    img_list = utils.read_images("../../test_data/cafe", N, downsample=3)
    full_img_list = utils.read_images("../../test_data/cafe", N)
    gray_imgs = utils.read_images("../../test_data/cafe", N, gray=True)
    x0 = np.full(N, 1.0/N)

    (gmap, weight) = gradient_map(gray_imgs)

    bnds = []
    for i in range(len(img_list)):
        bnds.append((0, 1))

    lambdas = minimize(edge_light, x0, method='TNC', jac=False,
                       bounds=bnds)

    ret_image = sum_images(lambdas.x, full_img_list)
    print lambdas.message
    print "Choice of lambdas = %s" % (lambdas.x)

    cv2.imwrite('output_edge.png', utils.denormalize_img(ret_image))
    cv2.imshow('image', ret_image)
    cv2.waitKey(0)
def avg_light(directory, output, count=-1, verbose=False):
    """
    This function calculates the fill light of the images.

    Arguments:
    directory -- the directory where the images are located
    count     -- the number of imags to use for this calculation
    verbose   -- should we print debug info
    """

    img_list = utils.read_images(directory, count)
    basis = basis_lights.BasisLights(img_list, verbose=verbose)
    res_image = basis.avg()
    cv2.imwrite(output, utils.denormalize_img(res_image))
    cv2.imshow('Average light', res_image)
    cv2.waitKey(0)
def main():
    # px1 = np.array([0, 126, 255], dtype=float)
    # px2 = np.array([0, 0, 0], dtype=float)
    global img_list
    img_list = utils.read_images("../../test_data/cafe", N, downsample=4)

    full_img_list = utils.read_images("../../test_data/cafe", N)

    # for i in range(0, N):
    #     # img_name = "../../../input_image/cafe/images/%03d.png" % (i)
    #     img_name = "../test_data/cafe/%03d.png" % (i)
    #     img = cv2.imread(img_name)
    #     img_list.append(utils.normalize_img(img))
    #     # img_list.append(img)

    # print alpha(10, 10)
    # print px_avg(400, 500)
    # print px_avg(img_list, 0, 0)
    # px1 = np.ndarray(shape=(3, 1), dtype=float,
    # buffer=np.array([0, 126, 255]))
    # px2 = np.ndarray(shape=(1, 3), dtype=float,
    #                  buffer=np.array([[255], [0], [128]]))
    # diffuse_color(1)

    x0 = np.full(N, 1.0/N)
    # print fprime(x0)
    # print x0
    # print diffuse_color(x0)
    # x0 = np.zeros(N)
    # print diffuse_color(x1)

    # x0 = np.ones(N)
    # print diffuse_color(x2)

    
    # results = pyipopt.fmin_unconstrained(diffuse_color, x0, fprime)

    # print results

    # nlp = pyipopt.create(diffuse_color, N)
    # x, zl, zu, constraint_multipliers, obj, status = nlp.solve(x0)

    # nlp.close()

    # leastsq(diffuse_color, x0)

    bnds = []
    for i in range(len(img_list)):
        bnds.append((0, 1))


    lambdas = minimize(diffuse_color, x0, method='TNC', jac=False,
                       bounds=bnds)

    # lambdas = np.array([ 0.0772488 ,  0.07700344,  0.0769977 ,  0.07699107,  0.07702644,
    #     0.07702589,  0.07702531,  0.07694187,  0.07695671,  0.07701653,
    #     0.07696243,  0.07767547,  0.07693373])

    # lambdas = np.array([ 0.03336186,  0.03335751,  0.03336186,  0.03336186,  0.03335691,
    #     0.03336186,  0.03335623,  0.03335545,  0.03336186,  0.03316012,
    #     0.03336186,  0.03332896,  0.03336186,  0.03336186,  0.03335172,
    #     0.03335502,  0.03336186,  0.03336186,  0.03335585,  0.03333409,
    #     0.03335658,  0.03335828,  0.03335722,  0.03335778,  0.03336186,
    #     0.03335804,  0.03336186,  0.03335251,  0.03332677,  0.0333048 ])
    # print("lambda: %s" % lambdas)
    ret_image = sum_images(lambdas.x, full_img_list)
    print lambdas.message
    print "Choice of lambdas = %s" % (lambdas.x)
    cv2.imwrite('output_diffuse.png', utils.denormalize_img(ret_image))
    cv2.imshow('image', ret_image)
    cv2.waitKey(0)