예제 #1
0
def convert_image(imageLoc, imageOutLoc, var):
    image = misc.imread(imageLoc)
    rows, cols = image.shape[0], image.shape[1]
    src_cols = np.linspace(0, cols, 20)
    src_rows = np.linspace(0, rows, 10)
    src_rows, src_cols = np.meshgrid(src_rows, src_cols)
    src = np.dstack([src_cols.flat, src_rows.flat])[0]

    # add sinusoidal oscillation to row coordinates
    #dst_rows = src[:, 1] - np.sin(np.linspace(0, 3 * np.pi, src.shape[0])) * 50
    #dst_cols = src[:, 0]
    #dst_rows *= 1.5
    #dst_rows -= 1.5 * 50
    #dst = np.vstack([dst_cols, dst_rows]).T

    mu, sigma = 0, var  # mean and standard deviation
    s = np.random.normal(mu, sigma, 1000)  #src.shape[0]

    # add Gausian oscillation to row coordinates
    dst_rows = src[:, 1] - np.random.normal(mu, sigma, src.shape[0]) * 50
    dst_cols = src[:, 0]
    dst_rows *= 1.5
    dst_rows -= 1.5 * 50
    dst = np.vstack([dst_cols, dst_rows]).T

    tform = PiecewiseAffineTransform()
    tform.estimate(src, dst)

    tform = PiecewiseAffineTransform()
    tform.estimate(src, dst)

    out_rows = image.shape[0] - 1.5 * 50
    out_cols = cols
    out = warp(image, tform, output_shape=(out_rows, out_cols))
    misc.imsave(imageOutLoc, out)
예제 #2
0
def main():
    args = cli()

    reference = io.imread(args.reference)
    sensed = io.imread(args.sensed)

    rows, cols = reference.shape
    ref_cols = np.linspace(0, cols, 20)
    ref_rows = np.linspace(0, rows, 10)
    ref_cols, ref_rows = np.meshgrid(ref_rows, ref_rows)
    src = np.dstack([ref_cols.flat, ref_rows.flat])[0]

    rows, cols = sensed.shape
    ref_cols = np.linspace(0, cols, 20)
    ref_rows = np.linspace(0, rows, 10)
    ref_cols, ref_rows = np.meshgrid(ref_rows, ref_rows)
    dst = np.dstack([ref_cols.flat, ref_rows.flat])[0]

    tform = PiecewiseAffineTransform()
    tform.estimate(src, dst)

    out = warp(sensed, tform)
    plt.subplot(211)
    plt.imshow(reference)

    plt.subplot(212)
    plt.imshow(out)

    plt.show()
예제 #3
0
def getMaskContour(mask_dir, atlas_img, predicted_pts, actual_pts, cwd, n, main_mask):
    """
    Gets the contour of the brain's boundaries and applies a piecewise affine transform to the brain atlas
    based on the cortical landmarks predicted in dlc_predict (and peaks of activity on the sensory map, if available).
    :param mask_dir: The path to the directory containing the U-net masks of the brain's boundaries.
    :param atlas_img: The brain atlas to be transformed.
    :param predicted_pts: The coordinates of the cortical landmarks predicted in dlc_predict (or, for the second run
    of this function, the coordinates of the peaks of activity in the sensory map).
    :param actual_pts: The fixed coordinates of the cortical landmarks on the brain atlas (or, for the second run of
    this function, the fixed coordinates of the peaks of sensory activity on the brain atlas).
    :param cwd: The path to the current working directory.
    :param n: The number of the current image in the directory.
    """
    c_landmarks = np.empty([0, 2])
    c_atlas_landmarks = np.empty([0, 2])
    mask = cv2.imread(mask_dir, cv2.IMREAD_GRAYSCALE)
    atlas_to_warp = atlas_img
    mask = np.uint8(mask)
    cnts, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[-2:]
    for cnt in cnts:
        cnt = cnt[:, 0, :]
        cnt = np.asarray(cnt).astype("float32")
        c_landmarks = np.concatenate((c_landmarks, cnt))
        c_atlas_landmarks = np.concatenate((c_atlas_landmarks, cnt))
    c_landmarks = np.concatenate((c_landmarks, predicted_pts))
    c_atlas_landmarks = np.concatenate((c_atlas_landmarks, actual_pts))
    tform = PiecewiseAffineTransform()
    tform.estimate(c_atlas_landmarks, c_landmarks)
    dst = warp(atlas_to_warp, tform, output_shape=(512, 512))
    if main_mask:
        io.imsave(os.path.join(cwd, "mask_{}.png".format(n)), mask)
    return dst
예제 #4
0
def warp_piecewise_affine(image, map_args={}, output_shape=None, order=1, mode='constant', cval=0.0, clip=True, preserve_range=False):
    #image = imageGlobal
    rows, cols = image.shape[0], image.shape[1]

    src_cols = np.linspace(0, cols, 20)
    src_rows = np.linspace(0, rows, 10)
    src_rows, src_cols = np.meshgrid(src_rows, src_cols)
    src = np.dstack([src_cols.flat, src_rows.flat])[0]

    dst_rows = src[:, 1] - np.sin(np.linspace(0, 3 * np.pi, src.shape[0])) * 50
    dst_cols = src[:, 0]
    dst_rows *= 1.5
    dst_rows -= 1.5 * 50
    dst = np.vstack([dst_cols, dst_rows]).T

    tform = PiecewiseAffineTransform()
    tform.estimate(src, dst)

    out_rows = image.shape[0] - 1.5 * 50
    out_cols = cols
    out = warp(image, tform, output_shape=(out_rows, out_cols))

    fig, ax = plt.subplots()
    ax.imshow(out)
    ax.plot(tform.inverse(src)[:, 0], tform.inverse(src)[:, 1], '.b')
    ax.axis((0, out_cols, out_rows, 0))
    plt.show()
예제 #5
0
 def PiecewiseAffine(img, mask, points=8):
     ### piecwise affine ###
     rows, cols = img.shape[0], img.shape[1]
     src_cols = np.linspace(0, cols, points)
     src_rows = np.linspace(0, rows, points)
     src_rows, src_cols = np.meshgrid(src_rows, src_cols)
     src = np.dstack([src_cols.flat, src_rows.flat])[0]
     # add offset
     dst_rows = np.zeros(src[:, 1].shape) + src[:, 1]
     for i in list(range(points))[1:-1]:
         dst_rows[i::points] += np.random.normal(
             loc=0,
             scale=rows / (points * 10),
             size=dst_rows[i::points].shape)
     dst_cols = np.zeros(src[:, 0].shape) + src[:, 0]
     dst_cols[points:-points] += np.random.normal(
         loc=0,
         scale=rows / (points * 10),
         size=dst_cols[points:-points].shape)
     dst = np.vstack([dst_cols, dst_rows]).T
     # compute transform
     tform = PiecewiseAffineTransform()
     tform.estimate(src, dst)
     # apply transform
     img = warp(img, tform, output_shape=(rows, cols))
     mask = warp(mask, tform, output_shape=(rows, cols))
     return img, mask
예제 #6
0
def whole_rdistort(im, severity=1, scop=40):
    """
    Using the affine projection method in skimg,
    Realize the picture through the corresponding coordinate projection
    Specifies the distortion effect of the form. This function will normalize 0-1
    """

    if severity == 0:
        return im

    theta = severity * scop
    rows, cols = im.shape[:2]
    colpoints = max(int(cols * severity * 0.05), 3)
    rowpoints = max(int(rows * severity * 0.05), 3)

    src_cols = np.linspace(0, cols, colpoints)
    src_rows = np.linspace(0, rows, rowpoints)
    src_rows, src_cols = np.meshgrid(src_rows, src_cols)
    src = np.dstack([src_cols.flat, src_rows.flat])[0]

    # The key location for wave distortion effect
    dst_rows = src[:, 1] - period_map(np.linspace(0, 100, src.shape[0]), 50,
                                      20)

    # dst columns
    dst_cols = src[:,
                   0] - np.sin(np.linspace(0, 3 * np.pi, src.shape[0])) * theta

    dst = np.vstack([dst_cols, dst_rows]).T
    tform = PiecewiseAffineTransform()
    tform.estimate(src, dst)
    image = warp(im, tform, mode='edge', output_shape=(rows, cols)) * 255
    return np.array(cvt_uint8(image))
예제 #7
0
    def __call__(self, sample):
        image = sample
        if np.random.rand() > self.prob:
            return image

        image = np.array(image)
        rows, cols = image.shape[0], image.shape[1]
        src_cols = np.linspace(0, cols, 4)
        src_rows = np.linspace(0, rows, 2)
        src_rows, src_cols = np.meshgrid(src_rows, src_cols)
        src = np.dstack([src_cols.flat, src_rows.flat])[0]

        dfactor = np.random.randint(10, 20)
        pfactor = (np.random.randint(0, 3), np.random.randint(2, 4))
        dst_rows = src[:, 1] - np.sin(
            np.linspace(pfactor[0] * np.pi / 2, pfactor[1] * np.pi,
                        src.shape[0])) * dfactor
        dst_cols = src[:, 0]
        dst = np.vstack([dst_cols, dst_rows]).T

        tform = PiecewiseAffineTransform()
        tform.estimate(src, dst)
        out_image = warp(image,
                         tform,
                         output_shape=(rows, cols),
                         cval=255,
                         preserve_range=True)
        out_image = Image.fromarray(np.uint8(out_image))
        return out_image
def affine_transform(img):
    rows, cols = img.shape[0], img.shape[1]

    src_cols = np.linspace(0, cols, 20)
    src_rows = np.linspace(0, rows, 20)
    src_rows, src_cols = np.meshgrid(src_rows, src_cols)
    src = np.dstack([src_cols.flat, src_rows.flat])[0]

    # add sinusoidal oscillation to row coordinates
    dst_rows = src[:, 1]  # - np.sin(np.linspace(0, 3 * np.pi, src.shape[0])) * 50
    print(src[:, 1])
    print(src[:, 0])
    dst_cols = src[:, 0] - np.sin((src[:, 0] / np.max(src[:, 0])) * np.pi) * np.max(src[:, 0])
    print(dst_cols)

    dst = np.vstack([dst_cols, dst_rows]).T

    tform = PiecewiseAffineTransform()
    tform.estimate(src, dst)

    out_rows = rows
    out_cols = cols
    out = warp(img, tform, output_shape=(out_rows, out_cols))

    fig, ax = plt.subplots()
    ax.imshow(out)
    ax.plot(tform.inverse(src)[:, 0], tform.inverse(src)[:, 1], '.b')
    ax.axis((0, out_cols, out_rows, 0))
    plt.savefig('plots/piecewise_affine.png')
    plt.show()
예제 #9
0
def affin_transform(img_frame, nrrd_frame):
    '''
    transform the shape of nrrd_frame to fit the tissue image frame
    :param img_frame:
    :param nrrd_frame:
    :return:
    '''
    hull = get_convex_hull(img_frame, False, False)
    point_img_list = calculate_intersection_point(hull, img_frame, False)
    x = int(img_frame.shape[1] * 0.5)
    y = int(img_frame.shape[0] * 0.5)
    point_img_list.append((x, y))
    point_img_list = np.float32(point_img_list)

    nrrd_frame_reference, nrrd_frame_clean = pre_calibrate_single_frame(
        img_frame, nrrd_frame, False)

    hull_nrrd = get_convex_hull(nrrd_frame_reference, False, False, 1)

    point_nrrd_list = calculate_intersection_point(hull_nrrd,
                                                   nrrd_frame_reference, False)
    x = int(img_frame.shape[1] * 0.5)
    y = int(img_frame.shape[0] * 0.5)
    point_nrrd_list.append((x, y))
    point_nrrd_list = np.float32(point_nrrd_list)

    tform = PiecewiseAffineTransform()
    tform.estimate(point_img_list, point_nrrd_list)
    out = warp(nrrd_frame_clean,
               tform,
               output_shape=nrrd_frame_reference.shape)

    return out
예제 #10
0
def piecewise_affine_transform(image, srcAnchor, tgtAnchor):
    '''  Return 0-1 range
    '''
    trans = PiecewiseAffineTransform()
    trans.estimate(srcAnchor, tgtAnchor)
    warped = warp(image, trans)
    return warped
예제 #11
0
파일: pwa.py 프로젝트: hyb1234hi/emci
def pwa(image, src, dst, shape):
    """
    :param image: aligned image or cropped image
    :param src: normalized src landmarks
    :param dst: normalized dst landmarks
    :param shape: output shape, [rows, cols]
    :return: piece-wise affined image
    """
    image = cv2.resize(image, shape)
    src[:, 0] *= shape[0]
    src[:, 1] *= shape[1]
    dst[:, 0] *= shape[0]
    dst[:, 1] *= shape[1]
    N = 10
    z = np.zeros((N, 1))
    l = np.reshape(np.linspace(0, shape[1], N), (N, 1))
    top = np.concatenate([l, z], axis=1)
    bottom = np.concatenate([l, np.ones((N, 1)) * shape[0]], axis=1)

    l = np.reshape(np.linspace(0, shape[0], N), (N, 1))
    left = np.concatenate([z, l], axis=1)
    right = np.concatenate([np.ones((N, 1)) * shape[1], l], axis=1)

    add = np.concatenate([top, bottom, left, right], axis=0)
    src = np.concatenate([src, add], axis=0)
    dst = np.concatenate([dst, add], axis=0)
    tform = PiecewiseAffineTransform()
    tform.estimate(dst, src)
    # out_rows ,out_cols = shape
    out_rows = image.shape[0]
    out_cols = image.shape[1]
    out = warp(image, tform, output_shape=(out_rows, out_cols))
    return out
예제 #12
0
def get_mesh_warped_img(img, pts, pose_idx):
    """Piecewise affine warp to template mesh"""
    src_points = mean_shapes_mesh_ex[
        pose_idx]  # mesh template size: 0 ~ mesh_h, 0 ~ mesh_w
    dst_points = np.zeros((n_points_ex, 2),
                          np.float32)  # current points with outside box
    dst_points[0:n_points, :] = pts  # current points
    warp_mat_t_template2pts = get_warp_mat_t_template2pts(
        pts, pose_idx)  # template -> pts warp matrix
    outside_pts = get_warped_pts(
        mean_shape_ex_outside, warp_mat_t_template2pts)  # warp outside points
    dst_points[n_points:, :] = outside_pts  # current points with outside box

    tform = PiecewiseAffineTransform()  # piecewise affine transform
    tform.estimate(src_points, dst_points)
    img_mesh_warped = warp(img, tform, output_shape=(mesh_h, mesh_w))

    # # draw
    # plt.figure(2)
    # plt.gcf().clear()
    # plt.subplot(1, 2, 1)
    # plt.imshow(img)
    # plt.scatter(src_points[:, 0], src_points[:, 1], c='r')
    # plt.subplot(1, 2, 2)
    # plt.imshow(img)
    # plt.scatter(dst_points[:, 0], dst_points[:, 1], c='r')
    # plt.draw()
    # plt.pause(0.001)
    # k = 1
    # k = k + 1

    return img_mesh_warped
예제 #13
0
def piecewise_affine_transform():
	image = data.astronaut()
	rows, cols = image.shape[0], image.shape[1]

	src_cols = np.linspace(0, cols, 20)
	src_rows = np.linspace(0, rows, 10)
	src_rows, src_cols = np.meshgrid(src_rows, src_cols)
	src = np.dstack([src_cols.flat, src_rows.flat])[0]

	# Add sinusoidal oscillation to row coordinates.
	dst_rows = src[:, 1] - np.sin(np.linspace(0, 3 * np.pi, src.shape[0])) * 50
	dst_cols = src[:, 0]
	dst_rows *= 1.5
	dst_rows -= 1.5 * 50
	dst = np.vstack([dst_cols, dst_rows]).T

	tform = PiecewiseAffineTransform()
	tform.estimate(src, dst)

	out_rows = image.shape[0] - 1.5 * 50
	out_cols = cols
	out = warp(image, tform, output_shape=(out_rows, out_cols))

	fig, ax = plt.subplots()
	ax.imshow(out)
	ax.plot(tform.inverse(src)[:, 0], tform.inverse(src)[:, 1], '.b')
	ax.axis((0, out_cols, out_rows, 0))
	plt.show()
예제 #14
0
def bosse(path='img.png'):
    im = Image.open(path)
    image = np.array(im)
    rows, cols = image.shape[0], image.shape[1]

    src_cols = np.linspace(0, cols, 20)
    src_rows = np.linspace(0, rows, 10)
    src_rows, src_cols = np.meshgrid(src_rows, src_cols)
    src = np.dstack([src_cols.flat, src_rows.flat])[0]

    # add sinusoidal oscillation to row coordinates
    dst_rows = src[:, 1] - np.sin(np.linspace(np.pi, 2 * np.pi,
                                              src.shape[0])) * 50
    dst_cols = src[:, 0]
    dst_rows *= 1.5
    dst_rows -= 1.5 * 50
    dst = np.vstack([dst_cols, dst_rows]).T

    tform = PiecewiseAffineTransform()
    tform.estimate(src, dst)

    out_rows = image.shape[0] - 1.5 * 50
    out_cols = cols
    out = warp(image, tform, output_shape=(out_rows, out_cols))

    im = Image.fromarray((out * 255).astype(np.uint8))
    im.save("img_modif.png")
예제 #15
0
def transform_image(stationary_image, warp_image, stationary_points,
                    warp_points):
    tform = PiecewiseAffineTransform()
    tform.estimate(stationary_points, warp_points)
    out_rows = stationary_image.shape[0]
    out_cols = stationary_image.shape[1]
    warped_image = warp(warp_image, tform, output_shape=(out_rows, out_cols))
    return warped_image
예제 #16
0
def get_transform(image, src_points, dst_points):
    src_points = np.array([[0, 0], [0, image.shape[0]], [image.shape[0], 0],
                           list(image.shape[:2])] + src_points.tolist())
    dst_points = np.array([[0, 0], [0, image.shape[0]], [image.shape[0], 0],
                           list(image.shape[:2])] + dst_points.tolist())
    tform3 = PiecewiseAffineTransform()
    tform3.estimate(dst_points, src_points)
    return tform3
예제 #17
0
def test_degenerate():
    src = dst = np.zeros((10, 2))

    tform = SimilarityTransform()
    assert not tform.estimate(src, dst)
    assert np.all(np.isnan(tform.params))

    tform = EuclideanTransform()
    assert not tform.estimate(src, dst)
    assert np.all(np.isnan(tform.params))

    tform = AffineTransform()
    assert not tform.estimate(src, dst)
    assert np.all(np.isnan(tform.params))

    tform = ProjectiveTransform()
    assert not tform.estimate(src, dst)
    assert np.all(np.isnan(tform.params))

    # See gh-3926 for discussion details
    tform = ProjectiveTransform()
    for i in range(20):
        # Some random coordinates
        src = np.random.rand(4, 2) * 100
        dst = np.random.rand(4, 2) * 100

        # Degenerate the case by arranging points on a single line
        src[:, 1] = np.random.rand()
        # Prior to gh-3926, under the above circumstances,
        # a transform could be returned with nan values.
        assert(not tform.estimate(src, dst) or np.isfinite(tform.params).all())

    src = np.array([[0, 2, 0], [0, 2, 0], [0, 4, 0]])
    dst = np.array([[0, 1, 0], [0, 1, 0], [0, 3, 0]])
    tform = AffineTransform()
    assert not tform.estimate(src, dst)
    # Prior to gh-6207, the above would set the parameters as the identity.
    assert np.all(np.isnan(tform.params))

    # The tesselation on the following points produces one degenerate affine
    # warp within PiecewiseAffineTransform.
    src = np.asarray([
        [0, 192, 256], [0, 256, 256], [5, 0, 192], [5, 64, 0], [5, 64, 64],
        [5, 64, 256], [5, 192, 192], [5, 256, 256], [0, 192, 256],
    ])

    dst = np.asarray([
        [0, 142, 206], [0, 206, 206], [5, -50, 142], [5, 14, 0], [5, 14, 64],
        [5, 14, 206], [5, 142, 142], [5, 206, 206], [0, 142, 206],
    ])
    tform = PiecewiseAffineTransform()
    assert not tform.estimate(src, dst)
    assert np.all(np.isnan(tform.affines[4].params))  # degenerate affine
    for idx, affine in enumerate(tform.affines):
        if idx != 4:
            assert not np.all(np.isnan(affine.params))
    for affine in tform.inverse_affines:
        assert not np.all(np.isnan(affine.params))
예제 #18
0
def randdistort(img):
    image = np.array(plt.imread(img))
    rows, cols = image.shape[0], image.shape[1]

    src_cols = np.linspace(0, cols, 20)
    src_rows = np.linspace(0, rows, 10)
    src_rows, src_cols = np.meshgrid(src_rows, src_cols)
    src = np.dstack([src_cols.flat, src_rows.flat])[0]
    funclist = [
        lambda x: np.sin(x), lambda x: np.cos(x), lambda x: np.arctan(x)
    ]
    numberOfFunctions = random.randint(2, 5)

    # add sinusoidal oscillation to row coordinates
    def func(x):
        newfuncs = []
        for i in range(numberOfFunctions):
            rand1 = random.randint(1, 5)
            rand2 = random.randint(0, 30)
            rand3 = random.randint(0, 10)
            newfuncs.append(lambda x: rand3 * random.choice(funclist)
                            (1 / rand1 * x + rand2))
        for function in newfuncs:
            x += function(x)
        return x

    def func2(x):
        newfuncs = []
        for i in range(numberOfFunctions):
            rand1 = random.randint(1, 5)
            rand2 = random.randint(0, 30)
            rand3 = random.randint(0, 15)
            newfuncs.append(lambda x: rand3 * random.choice(funclist)
                            (1 / rand1 * x + rand2))
        for function in newfuncs:
            x += function(x)
        return x

    dst_rows = src[:, 1] + func(np.linspace(0, 10 * np.pi, src.shape[0]))
    dst_cols = src[:, 0] + func2(np.linspace(0, 3 * np.pi, src.shape[0]))
    dst_rows *= 1.5
    dst_rows -= 1.5 * 50
    dst = np.vstack([dst_cols, dst_rows]).T

    tform = PiecewiseAffineTransform()
    tform.estimate(src, dst)

    out_rows = image.shape[0] - 1.5 * 50
    out_cols = cols
    out = warp(image, tform, output_shape=(out_rows, out_cols))

    fig, ax = plt.subplots()
    ax.imshow(out)
    ax.plot(tform.inverse(src)[:, 0], tform.inverse(src)[:, 1], '.b')
    plt.imsave('name.png', out)
    ax.axis((0, out_cols, out_rows, 0))
    return 'name.png'
예제 #19
0
 def piecewise_affine_transform(image, source_lmks, target_lmks):
     anchor = list(range(31)) + [36, 39, 42, 45, 48, 51, 54, 57]
     tgt_lmks = target_lmks[anchor, :]
     dst_lmks = source_lmks[anchor, :]
     tform = PiecewiseAffineTransform()
     tform.estimate(tgt_lmks, dst_lmks)
     dst = warp(image, tform,
                output_shape=image.shape[:2]).astype(np.float32)
     return dst
예제 #20
0
    def piecewise_shift(item):
        (k, v) = item

        frame_shift = asarray([x[k] for x in shifts])
        dst = asarray([s + x for s, x in zip(src, frame_shift)])

        tform = PiecewiseAffineTransform()
        tform.estimate(src, dst)
        return warp(v, tform)
예제 #21
0
def arap_transform(cloth, gOriginalMesh, gDeformedMesh):

    pwtform = PiecewiseAffineTransform()
    pwtform.estimate(gDeformedMesh.vertices, gOriginalMesh.vertices)
    warpedUpperClothfloat = warp(cloth, pwtform, output_shape=cloth.shape)

    # 4.4 convert type from float64 to uint8
    warpedUpperClothfloat = 255 * warpedUpperClothfloat  # Now scale by 255
    warpedUpperCloth = warpedUpperClothfloat.astype(np.uint8)

    return warpedUpperCloth
def piecewise_affine(image):
    rows, cols = image.shape[0], image.shape[1]
    src_cols = np.linspace(0, cols, 20)
    src_rows = np.linspace(0, rows, 20)
    src_rows, src_cols = np.meshgrid(src_rows, src_cols)
    src = np.dstack([src_cols.flat, src_rows.flat])[0]
    dst_rows = src[:, 1] - np.cos(np.linspace(0, 3 * np.pi, src.shape[0])) * 20
    dst_cols = src[:, 0]
    dst = np.vstack([dst_cols, dst_rows]).T
    tform = PiecewiseAffineTransform()
    tform.estimate(src, dst)
    out = warp(image, tform)
    return out
예제 #23
0
def main():
    inv_param = np.linalg.inv(
        np.array([(i, i**2, i**3, i**4, 1) for i in range(0, 17, 4)]))
    param = inv_param.dot(np.array((0, 0.04, 0.01, 0.04, 0)))
    beautify_param = np.array([(i, i**2, i**3, i**4, 1)
                               for i in range(0, 17)]).dot(param)
    beautify_param = np.tile(beautify_param, (2, 1)).transpose()
    cv2.namedWindow("frame")
    cap = cv2.VideoCapture(0)
    while True:
        ret, frame = cap.read()
        frame = cv2.flip(frame, 1)
        input_img = frame.copy() / 255.0
        height, width, depth = frame.shape
        src = np.array(
            ((0, 0), (width / 2, 0), (width - 1, 0), (0, height / 2),
             (0, height - 1), (width - 1, height - 1)))
        dst = np.array(
            ((0, 0), (width / 2, 0), (width - 1, 0), (0, height / 2),
             (0, height - 1), (width - 1, height - 1)))
        face_landmarks_list = face_recognition.face_landmarks(frame)
        transform = PiecewiseAffineTransform()
        for face_landmarks in face_landmarks_list:
            chin = face_landmarks['chin']
            nose_bridge = face_landmarks['nose_bridge']

            # draw_multiline_line(frame, chin, (0, 255, 0), 2)
            # draw_multiline_line(input_img, chin, (0, 1.0, 0), 2)
            src = np.vstack([src, np.array(chin)])
            src = np.vstack([src, np.array(nose_bridge)])

            nose_point = face_landmarks['nose_bridge'][-1]
            dst = np.vstack([
                dst, (np.array(chin) - np.tile(np.array(nose_point),
                                               (17, 1))) * beautify_param +
                np.array(chin)
            ])
            dst = np.vstack([dst, np.array(nose_bridge)])

            # draw_multiline_line(frame,nose_bridge,(255,0,0),5)
            # nose_tip = face_landmarks['nose_tip']
            # draw_multiline_line(frame,nose_tip,(0,0,255),5)
        transform.estimate(src, dst)
        out_img = warp(frame, transform)
        result = np.hstack([input_img, out_img])
        cv2.imshow('frame', result)
        cv2.imwrite('beauty.png', result)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    cap.release()
    cv2.destroyAllWindows()
예제 #24
0
def warpFace(im, oldLandmarks, newLandmarks, justFace=False, output_shape=None):
    print("warping face")
    if not justFace:
        cornerPts = np.array([(0, 0), (im.shape[1], 0), (im.shape[1], im.shape[0]), (0, im.shape[0])])

        oldLandmarks = np.append(oldLandmarks, cornerPts, axis=0)
        newLandmarks = np.append(newLandmarks, cornerPts, axis=0)

    tform = PiecewiseAffineTransform()
    tform.estimate(newLandmarks, oldLandmarks)

    warped = warp(im, tform, output_shape=output_shape)
    warped = skimage.img_as_ubyte(warped)
    return warped
    def __init__(
            self,
            phase_shift_limit=(0, 50),
            amplitude_limit=(3, 5),
            w_limit=(2, 4),
            value=255,
            always_apply=False,
            p=0.2,
    ):
        super(CustomPiecewiseAffineTransform, self).__init__(always_apply, p)

        self._tform = PiecewiseAffineTransform()

        self.phase_shift_limit = phase_shift_limit
        self.amplitude_limit = amplitude_limit
        self.w_limit = w_limit
        self.value = value
예제 #26
0
def warp(im, points, disp_min, disp_max, disp_len=None, disp_angle=None):
    h, w = im.shape[:2]

    # Include the corners
    src_pts = np.array([[0, 0], [w, 0], [0, h], [w, h]])
    dst_pts = np.array([[0, 0], [w, 0], [0, h], [w, h]])

    for i in range(points):
        rand_x = random.uniform(0, w)
        rand_y = random.uniform(0, h)
        p = np.array([rand_x, rand_y])
        if disp_len is None:
            rand_len = random.uniform(disp_min, disp_max)
        else:
            rand_len = disp_len

        if disp_angle is None:
            rand_angle = np.deg2rad(random.uniform(0, 360))
        else:
            rand_angle = disp_angle

        p2_x = rand_x + np.cos(rand_angle) * rand_len
        p2_y = rand_y + np.sin(rand_angle) * rand_len
        p2 = np.array([p2_x, p2_y])

        if src_pts is None:
            src_pts = np.array([p])
        else:
            temp = np.vstack((src_pts, p))
            src_pts = temp

        if dst_pts is None:
            dst_pts = np.array([p2])
        else:
            temp = np.vstack((dst_pts, p2))
            dst_pts = temp

    from skimage.transform import warp, PiecewiseAffineTransform
    tform = PiecewiseAffineTransform()
    tform.estimate(src_pts, dst_pts)
    warped_im = warp(im, tform)

    # Convert to OpenCV
    from skimage import img_as_ubyte
    warped_im = img_as_ubyte(warped_im)
    return warped_im
def non_linear_warp_transform(img, annotation):
    rows, cols = img.shape[0], img.shape[1]

    src_cols = np.linspace(0, cols, 6)
    src_rows = np.linspace(0, rows, 6)
    src_rows, src_cols = np.meshgrid(src_rows, src_cols)
    src = np.dstack([src_cols.flat, src_rows.flat])[0]

    dst = np.random.normal(0.0, 10, size=(36, 2)) + src

    tform = PiecewiseAffineTransform()
    tform.estimate(src, dst)

    out_rows = img.shape[0]
    out_cols = img.shape[1]
    img_out = warp(img, tform, output_shape=(out_rows, out_cols))
    annotation_out = warp(annotation, tform, output_shape=(out_rows, out_cols))
    return img_out, annotation_out
예제 #28
0
    def _estimate_transform(self):
        if self.estimated:
            return

        self.estimated = True

        # define the ending points of the transformation
        self.dl_dom = np.linspace(-self.dl, self.dl,
                                  num=self.n_dl) * self._model.pix_per_um
        self.t_dom = np.linspace(0, 2 * np.pi, num=self.n_theta)
        self.dst_rows, self.dst_cols = np.meshgrid(self.dl_dom, self.t_dom)

        # calculate the original points
        self.src_rows = self.dst_rows.copy()
        self.src_cols = self.dst_cols.copy()
        for i in range(self.src_cols.shape[0]):
            t = self.src_cols[i, 0]
            x0, y0 = self._model.f(t)
            o = self._model.normal_angle(t)
            sin_o = np.sin(o)
            cos_o = np.cos(o)

            for j in range(self.src_rows.shape[1]):
                dl = self.src_rows[i, j]
                logger.debug(
                    f"debug i={j},  j={i}, dl={dl:.2f}   src_cols[i, j]-t={self.src_cols[i, j] - t:.3f}"
                )
                self.src_cols[i, j] = x0 + dl * cos_o
                self.src_rows[i, j] = y0 + dl * sin_o

        # rescale the point of the dst mesh to match output image
        self.out_rows = self.dl_dom.size * self.pix_per_dl
        self.out_cols = self.t_dom.size * self.pix_per_theta
        self.dst_rows = np.linspace(0, self.out_rows, self.dl_dom.size)
        self.dst_cols = np.linspace(0, self.out_cols, self.t_dom.size)
        self.dst_rows, self.dst_cols = np.meshgrid(self.dst_rows,
                                                   self.dst_cols)

        # convert meshes to (N,2) vectors
        self.src = np.dstack([self.src_cols.flat, self.src_rows.flat])[0]
        self.dst = np.dstack([self.dst_cols.flat, self.dst_rows.flat])[0]

        self.transform = PiecewiseAffineTransform()
        self.transform.estimate(self.dst, self.src)
예제 #29
0
def warp_it(image):
    import numpy as np

    from skimage.transform import PiecewiseAffineTransform, warp
    from scipy import misc
    from PIL import Image
    rows, cols = image.shape[0], image.shape[1]

    src_cols = np.linspace(0, cols, 20)
    src_rows = np.linspace(0, rows, 10)
    src_rows, src_cols = np.meshgrid(src_rows, src_cols)
    src = np.dstack([src_cols.flat, src_rows.flat])[0]

    # print(str(src))
    # print("00000000000000000000000000000000000000000000000000")
    # print(str(src[:, 1]))
    # print("00000000000000000000000000000000000000000000000000")

    # print(str(src[:, 0]))

    # # add sinusoidal oscillation to coordinates
    # dst_rows = src[:, 1] - np.sin(np.linspace(0, 3 * np.pi, src.shape[0])) * 16
    # dst_cols = src[:, 0] - np.sin(np.linspace(0, 3 * np.pi, src.shape[0])) * 16
    # dst_rows += 8
    # dst = np.vstack([dst_cols, dst_rows]).T
    from random import randint
    dst = src.copy()
    for i in range(dst.shape[0]):
        x = dst[i][0]
        y = dst[i][1]

        dst[i][0] += randint(0, 15)
        dst[i][1] += randint(0, 15)

    tform = PiecewiseAffineTransform()
    tform.estimate(src, dst)

    out_rows = image.shape[0] - 1.5 * 16
    out_cols = cols
    out = warp(image, tform, output_shape=(rows, cols))
    from skimage import img_as_ubyte

    return out
예제 #30
0
def adapt_contour(img_in, mask_in, mask_out=None):

    contours_in = measure.find_contours(mask_in, 0)
    landmarks_in = equidistant_landmarks(contours_in[0], 24)

    contours_out = measure.find_contours(mask_out, 0)
    landmarks_out = equidistant_landmarks(contours_out[0], 24)

    if len(landmarks_in) > len(landmarks_out):
        landmarks_in = landmarks_in[:len(landmarks_out)]
    else:
        landmarks_out = landmarks_out[:len(landmarks_in)]

    tform = PiecewiseAffineTransform()
    tform.estimate(np.fliplr(np.array(landmarks_out)),
                   np.fliplr(np.array(landmarks_in)))
    img_out = warp(img_in, tform, output_shape=img_in.shape)

    return img_out, landmarks_in, landmarks_out