Ejemplo n.º 1
0
 def test_func(src1, src2, alpha=1.0, **kwargs):
     beta = 1.0 - alpha
     src1 = vt.rectify_to_float01(src1)
     src2 = vt.rectify_to_float01(src2)
     dst = np.empty(src1.shape, dtype=src1.dtype)
     cv2.addWeighted(src1=src1, src2=src2, dst=dst, alpha=alpha, beta=beta, dtype=-1, **kwargs)
     return dst
Ejemplo n.º 2
0
def blend_images_multiply(img1, img2, alpha=0.5):
    r"""
    Args:
        img1 (ndarray[uint8_t, ndim=2]):  image data
        img2 (ndarray[uint8_t, ndim=2]):  image data
        alpha (float): (default = 0.5)

    Returns:
        ndarray: imgB


    References:
        https://en.wikipedia.org/wiki/Blend_modes

    CommandLine:
        python -m vtool.blend --test-blend_images_multiply:0 --show
        python -m vtool.blend --test-blend_images_multiply:1 --show

    Example:
        >>> # ENABLE_DOCTEST
        >>> from vtool.blend import *  # NOQA
        >>> alpha = 0.8
        >>> img1, img2 = testdata_blend()
        >>> imgB = blend_images_multiply(img1, img2, alpha)
        >>> ut.quit_if_noshow()
        >>> import plottool as pt
        >>> pt.imshow(imgB)
        >>> ut.show_if_requested()

    Example2:
        >>> # GRIDSEARCH
        >>> from vtool.blend import *  # NOQA
        >>> test_func = blend_images_multiply
        >>> args = testdata_blend(scale=128)
        >>> param_info = ut.ParamInfoList('blend_params', [
        ...    ut.ParamInfo('alpha', .8, 'alpha=',
        ...                 varyvals=np.linspace(0, 1.0, 25).tolist()),
        ... ])
        >>> gridsearch_image_function(param_info, test_func, args)
        >>> ut.show_if_requested()
    """
    import vtool as vt
    # rectify type
    img1_ = vt.rectify_to_float01(img1)
    img2_ = vt.rectify_to_float01(img2)
    #assert img1_.min() >= 0 and img1_.max() <= 1
    #assert img2_.min() >= 0 and img2_.max() <= 1
    # apply transform
    if False and alpha == .5:
        imgB = img1_ * img2_
    else:
        data = [img1_, img2_]
        weights = [1.0 - alpha + .5, alpha + .5]
        #imgB = vt.weighted_geometic_mean(data, weights)
        imgB = vt.weighted_geometic_mean_unnormalized(data, weights)
    # unrectify
    #assert imgB.min() >= 0 and imgB.max() <= 1
    return imgB
Ejemplo n.º 3
0
def blend_images_multiply(img1, img2, alpha=0.5):
    r"""
    Args:
        img1 (ndarray[uint8_t, ndim=2]):  image data
        img2 (ndarray[uint8_t, ndim=2]):  image data
        alpha (float): (default = 0.5)

    Returns:
        ndarray: imgB


    References:
        https://en.wikipedia.org/wiki/Blend_modes

    CommandLine:
        python -m vtool.blend --test-blend_images_multiply:0 --show
        python -m vtool.blend --test-blend_images_multiply:1 --show

    Example:
        >>> # ENABLE_DOCTEST
        >>> from vtool.blend import *  # NOQA
        >>> alpha = 0.8
        >>> img1, img2 = testdata_blend()
        >>> imgB = blend_images_multiply(img1, img2, alpha)
        >>> ut.quit_if_noshow()
        >>> import plottool as pt
        >>> pt.imshow(imgB)
        >>> ut.show_if_requested()

    Example2:
        >>> # GRIDSEARCH
        >>> from vtool.blend import *  # NOQA
        >>> test_func = blend_images_multiply
        >>> args = testdata_blend(scale=128)
        >>> param_info = ut.ParamInfoList('blend_params', [
        ...    ut.ParamInfo('alpha', .8, 'alpha=',
        ...                 varyvals=np.linspace(0, 1.0, 25).tolist()),
        ... ])
        >>> gridsearch_image_function(param_info, test_func, args)
        >>> ut.show_if_requested()
    """
    import vtool as vt
    # rectify type
    img1_ = vt.rectify_to_float01(img1)
    img2_ = vt.rectify_to_float01(img2)
    #assert img1_.min() >= 0 and img1_.max() <= 1
    #assert img2_.min() >= 0 and img2_.max() <= 1
    # apply transform
    if False and alpha == .5:
        imgB = img1_ * img2_
    else:
        data = [img1_, img2_]
        weights = [1.0 - alpha + .5, alpha + .5]
        #imgB = vt.weighted_geometic_mean(data, weights)
        imgB = vt.weighted_geometic_mean_unnormalized(data, weights)
    # unrectify
    #assert imgB.min() >= 0 and imgB.max() <= 1
    return imgB
Ejemplo n.º 4
0
def testdata_blend(scale=128):
    import vtool as vt
    img_fpath = ut.grab_test_imgpath('lena.png')
    img1 = vt.imread(img_fpath)
    rng = np.random.RandomState(0)
    img2 = vt.perlin_noise(img1.shape[0:2], scale=scale, rng=rng)[None, :].T
    img1 = vt.rectify_to_float01(img1)
    img2 = vt.rectify_to_float01(img2)
    return img1, img2
Ejemplo n.º 5
0
def testdata_blend(scale=128):
    import vtool as vt
    img_fpath = ut.grab_test_imgpath('lena.png')
    img1 = vt.imread(img_fpath)
    rng = np.random.RandomState(0)
    img2 = vt.perlin_noise(img1.shape[0:2], scale=scale, rng=rng)[None, :].T
    img1 = vt.rectify_to_float01(img1)
    img2 = vt.rectify_to_float01(img2)
    return img1, img2
Ejemplo n.º 6
0
 def test_func(src1, src2, alpha=1.0, **kwargs):
     beta = 1.0 - alpha
     src1 = vt.rectify_to_float01(src1)
     src2 = vt.rectify_to_float01(src2)
     dst = np.empty(src1.shape, dtype=src1.dtype)
     cv2.addWeighted(src1=src1,
                     src2=src2,
                     dst=dst,
                     alpha=alpha,
                     beta=beta,
                     dtype=-1,
                     **kwargs)
     return dst
Ejemplo n.º 7
0
def testdata_augment():
    from ibeis_cnn import ingest_data, utils
    import vtool as vt
    dataset = ingest_data.grab_siam_dataset()
    cv2_data, labels = dataset.subset('valid')
    batch_size = 128
    Xb, yb = utils.random_xy_sample(cv2_data, labels, batch_size / 2, 2, seed=0)
    Xb = vt.rectify_to_float01(Xb)
    Xb_orig = Xb.copy()
    yb_orig = yb.copy()
    return Xb_orig, yb_orig, Xb, yb
Ejemplo n.º 8
0
def show_augmented_patches(Xb, Xb_, yb, yb_, data_per_label=1, shadows=None):
    """
    from ibeis_cnn.augment import *  # NOQA
    std_ = center_std
    mean_ = center_mean
    """
    import plottool as pt
    import vtool as vt
    Xb_old = vt.rectify_to_float01(Xb)
    Xb_new = vt.rectify_to_float01(Xb_)

    # only look at ones that were actually augmented
    sample1 = Xb_old[0::data_per_label]
    sample2 = Xb_new[0::data_per_label]
    diff = np.abs((sample1 - sample2))
    diff_batches = diff.sum(-1).sum(-1).sum(-1) > 0
    modified_indexes = np.where(diff_batches > 0)[0]
    print('modified_indexes = %r' % (modified_indexes,))
    #modified_indexes = np.arange(num_examples)

    Xb_old = vt.rectify_to_uint8(Xb_old)
    Xb_new = vt.rectify_to_uint8(Xb_new)

    # Group data into n-tuples
    grouped_idxs = [np.arange(n, len(Xb_), data_per_label)
                    for n in range(data_per_label)]
    data_lists_old = vt.apply_grouping(Xb_old, grouped_idxs, axis=0)
    data_lists_new = vt.apply_grouping(Xb_new, grouped_idxs, axis=0)

    import six
    #chunck_sizes = (4, 10)
    import utool
    with utool.embed_on_exception_context:
        chunk_sizes = pt.get_square_row_cols(len(modified_indexes), max_cols=10,
                                             fix=False, inclusive=False)
        _iter = ut.iter_multichunks(modified_indexes, chunk_sizes)
        multiindices = six.next(_iter)

        from ibeis_cnn import draw_results
        tup = draw_results.get_patch_multichunks(data_lists_old, yb, {},
                                                 multiindices)
        orig_stack = tup[0]
        #stacked_img, stacked_offsets, stacked_sfs = tup

        tup = draw_results.get_patch_multichunks(data_lists_new, yb_, {},
                                                 multiindices)
        warp_stack = tup[0]
    #stacked_img, stacked_offsets, stacked_sfs = tup

    #orig_stack = stacked_img_pairs(Xb_old, modified_indexes, yb)
    #warp_stack = stacked_img_pairs(Xb_new, modified_indexes, yb_)
    if shadows is not None:
        # hack
        shadow_stack = stacked_img_pairs(shadows, modified_indexes, yb_)

    fnum = None
    fnum = pt.ensure_fnum(fnum)
    pt.figure(fnum)
    #next_pnum = pt.make_pnum_nextgen(nRows=2 + (shadows is not None), nCols=1)
    next_pnum = pt.make_pnum_nextgen(nCols=2 + (shadows is not None), nRows=1)
    pt.imshow(orig_stack, pnum=next_pnum(), title='before')
    pt.imshow(warp_stack, pnum=next_pnum(), title='after')

    if shadows is not None:
        pt.imshow(shadow_stack, pnum=next_pnum(), title='shadow_stack')